Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 1 | /* 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 |
| 27 | cvdescriptorset::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++; |
| 44 | bindings_.push_back(new safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i])); |
| 45 | // 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))) { |
| 49 | bindings_.back()->pImmutableSamplers = nullptr; |
| 50 | } |
| 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 | } |
| 57 | cvdescriptorset::DescriptorSetLayout::~DescriptorSetLayout() { |
| 58 | for (auto binding : bindings_) |
| 59 | delete binding; |
| 60 | } |
| 61 | // put all bindings into the given set |
| 62 | void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const { |
| 63 | for (auto binding_index_pair : binding_to_index_map_) |
| 64 | binding_set->insert(binding_index_pair.first); |
| 65 | } |
| 66 | VkDescriptorSetLayoutBinding const * |
| 67 | cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const { |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 68 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 69 | if (bi_itr != binding_to_index_map_.end()) { |
| 70 | return bindings_[bi_itr->second]->ptr(); |
| 71 | } |
| 72 | return nullptr; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 73 | } |
| 74 | VkDescriptorSetLayoutBinding const * |
| 75 | cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const { |
| 76 | if (index >= bindings_.size()) |
| 77 | return nullptr; |
| 78 | return bindings_[index]->ptr(); |
| 79 | } |
| 80 | // Return descriptorCount for given binding, 0 if index is unavailable |
| 81 | uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const { |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 82 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 83 | if (bi_itr != binding_to_index_map_.end()) { |
| 84 | return bindings_[bi_itr->second]->descriptorCount; |
| 85 | } |
| 86 | return 0; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 87 | } |
| 88 | // Return descriptorCount for given index, 0 if index is unavailable |
| 89 | uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const { |
| 90 | if (index >= bindings_.size()) |
| 91 | return 0; |
| 92 | return bindings_[index]->descriptorCount; |
| 93 | } |
| 94 | // For the given binding, return descriptorType |
| 95 | VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const { |
| 96 | assert(binding_to_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 97 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 98 | if (bi_itr != binding_to_index_map_.end()) { |
| 99 | return bindings_[bi_itr->second]->descriptorType; |
| 100 | } |
| 101 | return VK_DESCRIPTOR_TYPE_MAX_ENUM; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 102 | } |
| 103 | // For the given index, return descriptorType |
| 104 | VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const { |
| 105 | assert(index < bindings_.size()); |
| 106 | return bindings_[index]->descriptorType; |
| 107 | } |
| 108 | // For the given global index, return descriptorType |
| 109 | // Currently just counting up through bindings_, may improve this in future |
| 110 | VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const { |
| 111 | uint32_t global_offset = 0; |
| 112 | for (auto binding : bindings_) { |
| 113 | global_offset += binding->descriptorCount; |
| 114 | if (index < global_offset) |
| 115 | return binding->descriptorType; |
| 116 | } |
| 117 | assert(0); // requested global index is out of bounds |
| 118 | return VK_DESCRIPTOR_TYPE_MAX_ENUM; |
| 119 | } |
| 120 | // For the given binding, return stageFlags |
| 121 | VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const { |
| 122 | assert(binding_to_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 123 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 124 | if (bi_itr != binding_to_index_map_.end()) { |
| 125 | return bindings_[bi_itr->second]->stageFlags; |
| 126 | } |
| 127 | return VkShaderStageFlags(0); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 128 | } |
| 129 | // For the given binding, return start index |
| 130 | uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const { |
| 131 | assert(binding_to_global_start_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 132 | const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding); |
| 133 | if (btgsi_itr != binding_to_global_start_index_map_.end()) { |
| 134 | return btgsi_itr->second; |
| 135 | } |
| 136 | // In error case max uint32_t so index is out of bounds to break ASAP |
| 137 | return 0xFFFFFFFF; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 138 | } |
| 139 | // For the given binding, return end index |
| 140 | uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const { |
| 141 | assert(binding_to_global_end_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 142 | const auto &btgei_itr = binding_to_global_end_index_map_.find(binding); |
| 143 | if (btgei_itr != binding_to_global_end_index_map_.end()) { |
| 144 | return btgei_itr->second; |
| 145 | } |
| 146 | // In error case max uint32_t so index is out of bounds to break ASAP |
| 147 | return 0xFFFFFFFF; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 148 | } |
| 149 | // For given binding, return ptr to ImmutableSampler array |
| 150 | VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const { |
| 151 | assert(binding_to_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 152 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 153 | if (bi_itr != binding_to_index_map_.end()) { |
| 154 | return bindings_[bi_itr->second]->pImmutableSamplers; |
| 155 | } |
| 156 | return nullptr; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 157 | } |
| 158 | // For given index, return ptr to ImmutableSampler array |
| 159 | VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const { |
| 160 | assert(index < bindings_.size()); |
| 161 | return bindings_[index]->pImmutableSamplers; |
| 162 | } |
| 163 | // If our layout is compatible with rh_ds_layout, return true, |
| 164 | // else return false and fill in error_msg will description of what causes incompatibility |
| 165 | bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const { |
| 166 | // Trivial case |
| 167 | if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) |
| 168 | return true; |
| 169 | if (descriptor_count_ != rh_ds_layout->descriptor_count_) { |
| 170 | std::stringstream error_str; |
| 171 | error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout " |
| 172 | << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors."; |
| 173 | *error_msg = error_str.str(); |
| 174 | return false; // trivial fail case |
| 175 | } |
| 176 | // Descriptor counts match so need to go through bindings one-by-one |
| 177 | // and verify that type and stageFlags match |
| 178 | for (auto binding : bindings_) { |
| 179 | // TODO : Do we also need to check immutable samplers? |
| 180 | // VkDescriptorSetLayoutBinding *rh_binding; |
| 181 | // rh_ds_layout->FillDescriptorSetLayoutBindingStructFromBinding(binding->binding, rh_binding); |
| 182 | if (binding->descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding->binding)) { |
| 183 | std::stringstream error_str; |
| 184 | error_str << "Binding " << binding->binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of " |
| 185 | << binding->descriptorCount << " but binding " << binding->binding << " for DescriptorSetLayout " |
| 186 | << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of " |
| 187 | << rh_ds_layout->GetDescriptorCountFromBinding(binding->binding); |
| 188 | *error_msg = error_str.str(); |
| 189 | return false; |
| 190 | } else if (binding->descriptorType != rh_ds_layout->GetTypeFromBinding(binding->binding)) { |
| 191 | std::stringstream error_str; |
| 192 | error_str << "Binding " << binding->binding << " for DescriptorSetLayout " << layout_ << " is type '" |
| 193 | << string_VkDescriptorType(binding->descriptorType) << "' but binding " << binding->binding |
| 194 | << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '" |
| 195 | << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding->binding)) << "'"; |
| 196 | *error_msg = error_str.str(); |
| 197 | return false; |
| 198 | } else if (binding->stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding->binding)) { |
| 199 | std::stringstream error_str; |
| 200 | error_str << "Binding " << binding->binding << " for DescriptorSetLayout " << layout_ << " has stageFlags " |
| 201 | << binding->stageFlags << " but binding " << binding->binding << " for DescriptorSetLayout " |
| 202 | << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags " |
| 203 | << rh_ds_layout->GetStageFlagsFromBinding(binding->binding); |
| 204 | *error_msg = error_str.str(); |
| 205 | return false; |
| 206 | } |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const { |
| 212 | if (!binding_to_index_map_.count(binding + 1)) |
| 213 | return false; |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 214 | auto const &bi_itr = binding_to_index_map_.find(binding); |
| 215 | if (bi_itr != binding_to_index_map_.end()) { |
| 216 | const auto &next_bi_itr = binding_to_index_map_.find(binding + 1); |
| 217 | if (next_bi_itr != binding_to_index_map_.end()) { |
| 218 | auto type = bindings_[bi_itr->second]->descriptorType; |
| 219 | auto stage_flags = bindings_[bi_itr->second]->stageFlags; |
| 220 | auto immut_samp = bindings_[bi_itr->second]->pImmutableSamplers ? true : false; |
| 221 | if ((type != bindings_[next_bi_itr->second]->descriptorType) || |
| 222 | (stage_flags != bindings_[next_bi_itr->second]->stageFlags) || |
| 223 | (immut_samp != (bindings_[next_bi_itr->second]->pImmutableSamplers ? true : false))) { |
| 224 | return false; |
| 225 | } |
| 226 | return true; |
| 227 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 228 | } |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 229 | return false; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout, |
| 233 | const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map, |
| 234 | const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *memory_map, |
| 235 | const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map, |
| 236 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map, |
| 237 | const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map, |
| 238 | const std::unordered_map<VkImage, IMAGE_NODE> *image_map, |
| 239 | const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map, |
| 240 | const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map) |
| 241 | : some_update_(false), full_update_(false), set_(set), p_layout_(layout), buffer_map_(buffer_map), memory_map_(memory_map), |
| 242 | buffer_view_map_(buffer_view_map), sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map), |
| 243 | image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) { |
| 244 | // Foreach binding, create default descriptors of given type |
| 245 | for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) { |
| 246 | auto type = p_layout_->GetTypeFromIndex(i); |
| 247 | switch (type) { |
| 248 | case VK_DESCRIPTOR_TYPE_SAMPLER: { |
| 249 | auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i); |
| 250 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) { |
| 251 | if (immut_sampler) |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 252 | descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor(immut_sampler + di, sampler_map_))); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 253 | else |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 254 | descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor(sampler_map_))); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 255 | } |
| 256 | break; |
| 257 | } |
| 258 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: { |
| 259 | auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i); |
| 260 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) { |
| 261 | if (immut) |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 262 | descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor( |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 263 | immut + di, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, sampler_map_))); |
| 264 | else |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 265 | descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor( |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 266 | image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, sampler_map_))); |
| 267 | } |
| 268 | break; |
| 269 | } |
| 270 | // ImageDescriptors |
| 271 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 272 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 273 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 274 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 275 | descriptors_.emplace_back(std::unique_ptr<Descriptor>( |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 276 | new ImageDescriptor(type, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_))); |
| 277 | break; |
| 278 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 279 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 280 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 281 | descriptors_.emplace_back(std::unique_ptr<Descriptor>(new TexelDescriptor(type, buffer_view_map_))); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 282 | break; |
| 283 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 284 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 285 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 286 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 287 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 288 | descriptors_.emplace_back(std::unique_ptr<Descriptor>(new BufferDescriptor(type, buffer_map_))); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 289 | break; |
| 290 | default: |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 291 | assert(0); // Bad descriptor type specified |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 292 | break; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | // For given global_index return bool of whether or not the underlying descriptor has been updated |
| 297 | bool cvdescriptorset::DescriptorSet::IsUpdated(const uint32_t global_index) const { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 298 | if (global_index >= descriptors_.size()) |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 299 | return false; |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 300 | return descriptors_[global_index]->updated; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 301 | } |
| 302 | // Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec? |
| 303 | bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const { |
| 304 | return layout->IsCompatible(p_layout_, error); |
| 305 | } |
| 306 | // Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time |
| 307 | // This includes validating that all descriptors in the given bindings are updated, |
| 308 | // that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers. |
| 309 | // Return true if state is acceptable, or false and write an error message into error string |
| 310 | bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings, |
| 311 | const std::vector<uint32_t> &dynamic_offsets, std::string *error) const { |
| 312 | for (auto binding : bindings) { |
| 313 | auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding); |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 314 | if (descriptors_[start_idx]->IsImmutableSampler()) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 315 | // Nothing to do for strictly immutable sampler |
| 316 | } else { |
| 317 | auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding); |
| 318 | auto dyn_offset_index = 0; |
| 319 | for (uint32_t i = start_idx; i <= end_idx; ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 320 | if (!descriptors_[i]->updated) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 321 | std::stringstream error_str; |
| 322 | error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i |
| 323 | << " is being used in draw but has not been updated."; |
| 324 | *error = error_str.str(); |
| 325 | return false; |
| 326 | } else { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 327 | if (GeneralBuffer == descriptors_[i]->GetClass()) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 328 | // Verify that buffers are valid |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 329 | auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 330 | auto buffer_node = buffer_map_->find(buffer); |
| 331 | if (buffer_node == buffer_map_->end()) { |
| 332 | std::stringstream error_str; |
| 333 | error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i |
| 334 | << " references invalid buffer " << buffer << "."; |
| 335 | *error = error_str.str(); |
| 336 | return false; |
| 337 | } else { |
| 338 | auto mem_entry = memory_map_->find(buffer_node->second.mem); |
| 339 | if (mem_entry == memory_map_->end()) { |
| 340 | std::stringstream error_str; |
| 341 | error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i |
| 342 | << " uses buffer " << buffer << " that references invalid memory " |
| 343 | << buffer_node->second.mem << "."; |
| 344 | *error = error_str.str(); |
| 345 | return false; |
| 346 | } |
| 347 | } |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 348 | if (descriptors_[i]->IsDynamic()) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 349 | // Validate that dynamic offsets are within the buffer |
| 350 | auto buffer_size = buffer_node->second.createInfo.size; |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 351 | auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange(); |
| 352 | auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 353 | auto dyn_offset = dynamic_offsets[dyn_offset_index++]; |
| 354 | if (VK_WHOLE_SIZE == range) { |
| 355 | if ((dyn_offset + desc_offset) > buffer_size) { |
| 356 | std::stringstream error_str; |
| 357 | error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i |
| 358 | << " uses buffer " << buffer |
| 359 | << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset |
| 360 | << " combined with offset " << desc_offset << " that oversteps the buffer size of " |
| 361 | << buffer_size << "."; |
| 362 | *error = error_str.str(); |
| 363 | return false; |
| 364 | } |
| 365 | } else { |
| 366 | if ((dyn_offset + desc_offset + range) > buffer_size) { |
| 367 | std::stringstream error_str; |
| 368 | error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i |
| 369 | << " uses buffer " << buffer << " with dynamic offset " << dyn_offset |
| 370 | << " combined with offset " << desc_offset << " and range " << range |
| 371 | << " that oversteps the buffer size of " << buffer_size << "."; |
| 372 | *error = error_str.str(); |
| 373 | return false; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | return true; |
| 383 | } |
| 384 | // For given bindings, place any update buffers or images into the passed-in unordered_sets |
| 385 | uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings, |
| 386 | std::unordered_set<VkBuffer> *buffer_set, |
| 387 | std::unordered_set<VkImageView> *image_set) const { |
| 388 | auto num_updates = 0; |
| 389 | for (auto binding : bindings) { |
| 390 | auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding); |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 391 | if (descriptors_[start_idx]->IsStorage()) { |
| 392 | if (Image == descriptors_[start_idx]->descriptor_class) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 393 | for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 394 | if (descriptors_[start_idx + i]->updated) { |
| 395 | image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView()); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 396 | num_updates++; |
| 397 | } |
| 398 | } |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 399 | } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 400 | for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 401 | if (descriptors_[start_idx + i]->updated) { |
| 402 | auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView(); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame^] | 403 | const auto &buff_pair = buffer_view_map_->find(bufferview); |
| 404 | if (buff_pair != buffer_view_map_->end()) { |
| 405 | buffer_set->insert(buff_pair->second.buffer); |
| 406 | num_updates++; |
| 407 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 408 | } |
| 409 | } |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 410 | } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 411 | for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 412 | if (descriptors_[start_idx + i]->updated) { |
| 413 | buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer()); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 414 | num_updates++; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | return num_updates; |
| 421 | } |
| 422 | // This is a special case for compute shaders that should eventually be removed once we have proper valid binding info for compute |
| 423 | // case |
| 424 | uint32_t cvdescriptorset::DescriptorSet::GetAllStorageUpdates(std::unordered_set<VkBuffer> *buffer_set, |
| 425 | std::unordered_set<VkImageView> *image_set) const { |
| 426 | std::unordered_set<uint32_t> binding_set; |
| 427 | p_layout_->FillBindingSet(&binding_set); |
| 428 | return GetStorageUpdates(binding_set, buffer_set, image_set); |
| 429 | } |
| 430 | // Starting at given current_binding with binding_remaining descriptors, parse over update_count |
| 431 | // descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent |
| 432 | // Consistency means that their type, stage flags, and whether or not they use immutable samplers matches |
| 433 | // If so, return true. If not, fill in error_msg and return false |
| 434 | bool cvdescriptorset::DescriptorSet::VerifyUpdateConsistency(uint32_t current_binding, uint32_t binding_remaining, |
| 435 | uint32_t update_count, const char *type, |
| 436 | std::string *error_msg) const { |
| 437 | // Verify consecutive bindings match (if needed) |
| 438 | auto orig_binding = current_binding; |
| 439 | while (update_count > binding_remaining) { // While our updates overstep current binding |
| 440 | // Verify next consecutive binding matches type, stage flags & immutable sampler use |
| 441 | if (!p_layout_->IsNextBindingConsistent(current_binding++)) { |
| 442 | std::stringstream error_str; |
| 443 | error_str << "Attempting " << type << " descriptor set " << set_ << " binding #" << orig_binding << " with #" |
| 444 | << update_count << " descriptors being updated but this update oversteps the bounds of this binding and then " |
| 445 | "next binding is not consistent with current binding so this update is invalid."; |
| 446 | *error_msg = error_str.str(); |
| 447 | return false; |
| 448 | } |
| 449 | // For sake of this check consider the bindings updated and advance to next binding |
| 450 | update_count -= binding_remaining; |
| 451 | binding_remaining = p_layout_->GetDescriptorCountFromBinding(current_binding); |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | // Perform write update in given update struct |
| 456 | // If an error occurs, return false and fill in details in error_msg string |
| 457 | bool cvdescriptorset::DescriptorSet::WriteUpdate(debug_report_data *report_data, const VkWriteDescriptorSet *update, |
| 458 | std::string *error_msg) { |
| 459 | auto num_updates = 0; |
| 460 | // Verify dst binding exists |
| 461 | if (!p_layout_->HasBinding(update->dstBinding)) { |
| 462 | std::stringstream error_str; |
| 463 | error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << "."; |
| 464 | *error_msg = error_str.str(); |
| 465 | return false; |
| 466 | } else { |
| 467 | // We know that binding is valid, verify update and do update on each descriptor |
| 468 | auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement; |
| 469 | auto type = p_layout_->GetTypeFromBinding(update->dstBinding); |
| 470 | if (type != update->descriptorType) { |
| 471 | std::stringstream error_str; |
| 472 | error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type " |
| 473 | << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType); |
| 474 | *error_msg = error_str.str(); |
| 475 | return false; |
| 476 | } |
| 477 | if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) { |
| 478 | std::stringstream error_str; |
| 479 | error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with " |
| 480 | << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount |
| 481 | << " descriptors starting at binding offset of " |
| 482 | << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) |
| 483 | << " combined with update array element offset of " << update->dstArrayElement |
| 484 | << " oversteps the size of this descriptor set."; |
| 485 | *error_msg = error_str.str(); |
| 486 | return false; |
| 487 | } |
| 488 | // Verify consecutive bindings match (if needed) |
| 489 | auto binding_remaining = p_layout_->GetDescriptorCountFromBinding(update->dstBinding) - update->dstArrayElement; |
| 490 | if (!VerifyUpdateConsistency(update->dstBinding, binding_remaining, update->descriptorCount, "write update to", error_msg)) |
| 491 | return false; |
| 492 | // Update is within bounds and consistent so perform update |
| 493 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 494 | // TODO : Can we break this into a set-level "Validate" followed by Descriptor updating itself |
| 495 | // if the validate passes? That saves us all the map ptrs in each descriptor instance |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 496 | if (!descriptors_[start_idx + di]->WriteUpdate(update, di, error_msg)) { |
| 497 | std::stringstream error_str; |
| 498 | error_str << "Write update to descriptor at global index " << start_idx + di << " within set " << set_ |
| 499 | << " binding #" << update->dstBinding << " failed with error message: " << error_msg->c_str(); |
| 500 | *error_msg = error_str.str(); |
| 501 | return false; |
| 502 | } |
| 503 | ++num_updates; |
| 504 | } |
| 505 | } |
| 506 | if (num_updates != 0) { |
| 507 | some_update_ = true; |
| 508 | full_update_ = (descriptors_.size() == num_updates); |
| 509 | } |
| 510 | return true; |
| 511 | } |
| 512 | // Copy update |
| 513 | bool cvdescriptorset::DescriptorSet::CopyUpdate(debug_report_data *report_data, const VkCopyDescriptorSet *update, |
| 514 | const DescriptorSet *src_set, std::string *error) { |
| 515 | auto num_updates = 0; |
| 516 | if (!p_layout_->HasBinding(update->dstBinding)) { |
| 517 | std::stringstream error_str; |
| 518 | error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << "."; |
| 519 | *error = error_str.str(); |
| 520 | return false; |
| 521 | } |
| 522 | if (!src_set->HasBinding(update->srcBinding)) { |
| 523 | std::stringstream error_str; |
| 524 | error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << "."; |
| 525 | *error = error_str.str(); |
| 526 | return false; |
| 527 | } |
| 528 | // src & dst set bindings are valid |
| 529 | // Check bounds of src & dst |
| 530 | auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement; |
| 531 | if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) { |
| 532 | // SRC update out of bounds |
| 533 | std::stringstream error_str; |
| 534 | error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding |
| 535 | << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding) |
| 536 | << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount |
| 537 | << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << "."; |
| 538 | *error = error_str.str(); |
| 539 | return false; |
| 540 | } |
| 541 | auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement; |
| 542 | if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) { |
| 543 | // DST update out of bounds |
| 544 | std::stringstream error_str; |
| 545 | error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding |
| 546 | << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) |
| 547 | << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount |
| 548 | << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << "."; |
| 549 | *error = error_str.str(); |
| 550 | return false; |
| 551 | } |
| 552 | // Check that types match |
| 553 | auto src_type = src_set->GetTypeFromBinding(update->srcBinding); |
| 554 | auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding); |
| 555 | if (src_type != dst_type) { |
| 556 | std::stringstream error_str; |
| 557 | error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type " |
| 558 | << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #" |
| 559 | << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match."; |
| 560 | *error = error_str.str(); |
| 561 | return false; |
| 562 | } |
| 563 | // Verify consistency of src & dst bindings if update crosses binding boundaries |
| 564 | auto src_binding_remaining = src_set->GetDescriptorCountFromBinding(update->srcBinding) - update->srcArrayElement; |
| 565 | auto dst_binding_remaining = p_layout_->GetDescriptorCountFromBinding(update->dstBinding) - update->dstArrayElement; |
| 566 | if ((!VerifyUpdateConsistency(update->srcBinding, src_binding_remaining, update->descriptorCount, "copy update from", error)) || |
| 567 | (!VerifyUpdateConsistency(update->dstBinding, dst_binding_remaining, update->descriptorCount, "copy update to", error))) { |
| 568 | return false; |
| 569 | } |
| 570 | // Update parameters all look good so perform update |
| 571 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 572 | if (!descriptors_[dst_start_idx]->CopyUpdate(src_set->descriptors_[src_start_idx].get(), error)) |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 573 | return false; |
| 574 | ++num_updates; |
| 575 | } |
| 576 | if (num_updates != 0) { |
| 577 | some_update_ = true; |
| 578 | full_update_ = (descriptors_.size() == num_updates); |
| 579 | } |
| 580 | return true; |
| 581 | } |
| 582 | cvdescriptorset::SamplerDescriptor::SamplerDescriptor( |
| 583 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) |
| 584 | : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) { |
| 585 | updated = false; |
| 586 | descriptor_class = PlainSampler; |
| 587 | }; |
| 588 | |
| 589 | cvdescriptorset::SamplerDescriptor::SamplerDescriptor( |
| 590 | const VkSampler *immut, const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) |
| 591 | : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) { |
| 592 | updated = false; |
| 593 | descriptor_class = PlainSampler; |
| 594 | if (immut) { |
| 595 | sampler_ = *immut; |
| 596 | immutable_ = true; |
| 597 | updated = true; |
| 598 | } |
| 599 | } |
| 600 | bool cvdescriptorset::ValidateSampler(const VkSampler sampler, |
| 601 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 602 | return (sampler_map->count(sampler) != 0); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 603 | } |
| 604 | bool cvdescriptorset::ValidateImageUpdate(const VkImageView image_view, const VkImageLayout image_layout, |
| 605 | const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map, |
| 606 | const std::unordered_map<VkImage, IMAGE_NODE> *image_map, |
| 607 | const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map, |
| 608 | const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map, |
| 609 | std::string *error) { |
| 610 | auto image_pair = image_view_map->find(image_view); |
| 611 | if (image_pair == image_view_map->end()) { |
| 612 | std::stringstream error_str; |
| 613 | error_str << "Invalid VkImageView: " << image_view; |
| 614 | *error = error_str.str(); |
| 615 | return false; |
| 616 | } else { |
| 617 | // Validate that imageLayout is compatible with aspect_mask and image format |
| 618 | VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask; |
| 619 | VkImage image = image_pair->second.image; |
| 620 | VkFormat format = VK_FORMAT_MAX_ENUM; |
| 621 | auto img_pair = image_map->find(image); |
| 622 | if (img_pair != image_map->end()) { |
| 623 | format = img_pair->second.createInfo.format; |
| 624 | } else { |
| 625 | // Also need to check the swapchains. |
| 626 | auto swapchain_pair = image_to_swapchain_map->find(image); |
| 627 | if (swapchain_pair != image_to_swapchain_map->end()) { |
| 628 | VkSwapchainKHR swapchain = swapchain_pair->second; |
| 629 | auto swapchain_pair = swapchain_map->find(swapchain); |
| 630 | if (swapchain_pair != swapchain_map->end()) { |
| 631 | format = swapchain_pair->second->createInfo.imageFormat; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | if (format == VK_FORMAT_MAX_ENUM) { |
| 636 | std::stringstream error_str; |
| 637 | error_str << "Invalid image (" << image << ") in imageView (" << image_view << ")."; |
| 638 | *error = error_str.str(); |
| 639 | return false; |
| 640 | } else { |
| 641 | bool ds = vk_format_is_depth_or_stencil(format); |
| 642 | switch (image_layout) { |
| 643 | case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: |
| 644 | // Only Color bit must be set |
| 645 | if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) { |
| 646 | std::stringstream error_str; |
| 647 | error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does " |
| 648 | "not have VK_IMAGE_ASPECT_COLOR_BIT set."; |
| 649 | *error = error_str.str(); |
| 650 | return false; |
| 651 | } |
| 652 | // format must NOT be DS |
| 653 | if (ds) { |
| 654 | std::stringstream error_str; |
| 655 | error_str << "ImageView (" << image_view |
| 656 | << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is " |
| 657 | << string_VkFormat(format) << " which is not a color format."; |
| 658 | *error = error_str.str(); |
| 659 | return false; |
| 660 | } |
| 661 | break; |
| 662 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: |
| 663 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: |
| 664 | // Depth or stencil bit must be set, but both must NOT be set |
| 665 | if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) { |
| 666 | if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) { |
| 667 | // both must NOT be set |
| 668 | std::stringstream error_str; |
| 669 | error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set"; |
| 670 | *error = error_str.str(); |
| 671 | return false; |
| 672 | } |
| 673 | } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) { |
| 674 | // Neither were set |
| 675 | std::stringstream error_str; |
| 676 | error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout) |
| 677 | << " but does not have STENCIL or DEPTH aspects set"; |
| 678 | *error = error_str.str(); |
| 679 | return false; |
| 680 | } |
| 681 | // format must be DS |
| 682 | if (!ds) { |
| 683 | std::stringstream error_str; |
| 684 | error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout) |
| 685 | << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format."; |
| 686 | *error = error_str.str(); |
| 687 | return false; |
| 688 | } |
| 689 | break; |
| 690 | default: |
| 691 | // anything to check for other layouts? |
| 692 | break; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | return true; |
| 697 | } |
| 698 | bool cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) { |
| 699 | if (!immutable_) { |
| 700 | if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) { |
| 701 | std::stringstream error_str; |
| 702 | error_str << "Attempted write update to sampler descriptor with invalid sampler: " << update->pImageInfo[index].sampler |
| 703 | << "."; |
| 704 | *error = error_str.str(); |
| 705 | return false; |
| 706 | } |
| 707 | sampler_ = update->pImageInfo[index].sampler; |
| 708 | } else { |
| 709 | if (!ValidateSampler(sampler_, sampler_map_)) { |
| 710 | std::stringstream error_str; |
| 711 | error_str << "Attempted write update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_ |
| 712 | << "."; |
| 713 | *error = error_str.str(); |
| 714 | return false; |
| 715 | } |
| 716 | // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)? |
| 717 | } |
| 718 | updated = true; |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | bool cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) { |
| 723 | if (!immutable_) { |
| 724 | auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_; |
| 725 | if (!ValidateSampler(update_sampler, sampler_map_)) { |
| 726 | std::stringstream error_str; |
| 727 | error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << "."; |
| 728 | *error = error_str.str(); |
| 729 | return false; |
| 730 | } |
| 731 | sampler_ = update_sampler; |
| 732 | } else { |
| 733 | if (!ValidateSampler(sampler_, sampler_map_)) { |
| 734 | std::stringstream error_str; |
| 735 | error_str << "Attempted copy update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_ |
| 736 | << "."; |
| 737 | *error = error_str.str(); |
| 738 | return false; |
| 739 | } |
| 740 | // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)? |
| 741 | } |
| 742 | updated = true; |
| 743 | return true; |
| 744 | } |
| 745 | cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor( |
| 746 | const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map, |
| 747 | const std::unordered_map<VkImage, IMAGE_NODE> *image_map, |
| 748 | const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map, |
| 749 | const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map, |
| 750 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) |
| 751 | : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED), |
| 752 | sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map), |
| 753 | image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) { |
| 754 | updated = false; |
| 755 | descriptor_class = ImageSampler; |
| 756 | } |
| 757 | |
| 758 | cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor( |
| 759 | const VkSampler *immut, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map, |
| 760 | const std::unordered_map<VkImage, IMAGE_NODE> *image_map, |
| 761 | const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map, |
| 762 | const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map, |
| 763 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) |
| 764 | : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED), |
| 765 | sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map), |
| 766 | image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) { |
| 767 | updated = false; |
| 768 | descriptor_class = ImageSampler; |
| 769 | if (immut) { |
| 770 | sampler_ = *immut; |
| 771 | immutable_ = true; |
| 772 | updated = true; |
| 773 | } |
| 774 | } |
| 775 | bool cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, |
| 776 | std::string *error) { |
| 777 | // First check sampler |
| 778 | if (!immutable_) { |
| 779 | if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) { |
| 780 | std::stringstream error_str; |
| 781 | error_str << "Attempted write update to combined image sampler descriptor with invalid sampler: " |
| 782 | << update->pImageInfo[index].sampler << "."; |
| 783 | *error = error_str.str(); |
| 784 | return false; |
| 785 | } |
| 786 | sampler_ = update->pImageInfo[index].sampler; |
| 787 | } else { |
| 788 | if (!ValidateSampler(sampler_, sampler_map_)) { |
| 789 | std::stringstream error_str; |
| 790 | error_str << "Attempted write update to combined image sampler descriptor with immutable sampler which has invalid " |
| 791 | "immutable sampler: " |
| 792 | << sampler_ << "."; |
| 793 | *error = error_str.str(); |
| 794 | return false; |
| 795 | } |
| 796 | // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)? |
| 797 | } |
| 798 | // Now validate images |
| 799 | auto image_view = update->pImageInfo[index].imageView; |
| 800 | auto image_layout = update->pImageInfo[index].imageLayout; |
| 801 | if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, |
| 802 | error)) { |
| 803 | std::stringstream error_str; |
| 804 | error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str(); |
| 805 | *error = error_str.str(); |
| 806 | return false; |
| 807 | } |
| 808 | updated = true; |
| 809 | image_view_ = image_view; |
| 810 | image_layout_ = image_layout; |
| 811 | return true; |
| 812 | } |
| 813 | |
| 814 | bool cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) { |
| 815 | if (!immutable_) { |
| 816 | auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_; |
| 817 | if (!ValidateSampler(update_sampler, sampler_map_)) { |
| 818 | std::stringstream error_str; |
| 819 | error_str << "Attempted copy update to combined image sampler descriptor with invalid sampler: " << update_sampler |
| 820 | << "."; |
| 821 | *error = error_str.str(); |
| 822 | return false; |
| 823 | } |
| 824 | sampler_ = update_sampler; |
| 825 | } else { |
| 826 | if (!ValidateSampler(sampler_, sampler_map_)) { |
| 827 | std::stringstream error_str; |
| 828 | error_str << "Attempted copy update to combined image sampler descriptor with immutable sampler that has invalid " |
| 829 | "immutable sampler: " |
| 830 | << sampler_ << "."; |
| 831 | *error = error_str.str(); |
| 832 | return false; |
| 833 | } |
| 834 | // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)? |
| 835 | } |
| 836 | // Now validate images |
| 837 | auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_; |
| 838 | auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_; |
| 839 | if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, |
| 840 | error)) { |
| 841 | std::stringstream error_str; |
| 842 | error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str(); |
| 843 | *error = error_str.str(); |
| 844 | return false; |
| 845 | } |
| 846 | updated = true; |
| 847 | image_view_ = image_view; |
| 848 | image_layout_ = image_layout; |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type, |
| 853 | const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map, |
| 854 | const std::unordered_map<VkImage, IMAGE_NODE> *image_map, |
| 855 | const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map, |
| 856 | const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map) |
| 857 | : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED), image_view_map_(image_view_map), |
| 858 | image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) { |
| 859 | updated = false; |
| 860 | descriptor_class = Image; |
| 861 | if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) |
| 862 | storage_ = true; |
| 863 | }; |
| 864 | |
| 865 | bool cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) { |
| 866 | // First validate that the write update is valid |
| 867 | auto image_view = update->pImageInfo[index].imageView; |
| 868 | auto image_layout = update->pImageInfo[index].imageLayout; |
| 869 | if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, |
| 870 | error)) { |
| 871 | std::stringstream error_str; |
| 872 | error_str << "Attempted write update to image descriptor failed due to: " << error->c_str(); |
| 873 | *error = error_str.str(); |
| 874 | return false; |
| 875 | } |
| 876 | // Update is clean so process it |
| 877 | updated = true; |
| 878 | image_view_ = image_view; |
| 879 | image_layout_ = image_layout; |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | bool cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src, std::string *error) { |
| 884 | // First validate update |
| 885 | auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_; |
| 886 | auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_; |
| 887 | if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, |
| 888 | error)) { |
| 889 | std::stringstream error_str; |
| 890 | error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str(); |
| 891 | *error = error_str.str(); |
| 892 | return false; |
| 893 | } |
| 894 | updated = true; |
| 895 | image_view_ = image_view; |
| 896 | image_layout_ = image_layout; |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type, |
| 901 | const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map) |
| 902 | : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0), buffer_map_(buffer_map) { |
| 903 | updated = false; |
| 904 | descriptor_class = GeneralBuffer; |
| 905 | if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) { |
| 906 | dynamic_ = true; |
| 907 | } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) { |
| 908 | storage_ = true; |
| 909 | } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) { |
| 910 | dynamic_ = true; |
| 911 | storage_ = true; |
| 912 | } |
| 913 | } |
| 914 | bool cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) { |
| 915 | // First validate bufferinfo |
| 916 | auto buffer = update->pBufferInfo[index].buffer; |
| 917 | if (!buffer_map_->count(buffer)) { |
| 918 | std::stringstream error_str; |
| 919 | error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer; |
| 920 | *error = error_str.str(); |
| 921 | return false; |
| 922 | } |
| 923 | updated = true; |
| 924 | buffer_ = buffer; |
| 925 | offset_ = update->pBufferInfo[index].offset; |
| 926 | range_ = update->pBufferInfo[index].range; |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | bool cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src, std::string *error) { |
| 931 | // First validate bufferinfo |
| 932 | auto buffer = static_cast<const BufferDescriptor *>(src)->buffer_; |
| 933 | if (!buffer_map_->count(buffer)) { |
| 934 | std::stringstream error_str; |
| 935 | error_str << "Attempted copy update to buffer descriptor with invalid buffer: " << buffer; |
| 936 | *error = error_str.str(); |
| 937 | return false; |
| 938 | } |
| 939 | updated = true; |
| 940 | buffer_ = buffer; |
| 941 | offset_ = static_cast<const BufferDescriptor *>(src)->offset_; |
| 942 | range_ = static_cast<const BufferDescriptor *>(src)->range_; |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type, |
| 947 | const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map) |
| 948 | : buffer_view_(VK_NULL_HANDLE), storage_(false), buffer_view_map_(buffer_view_map) { |
| 949 | updated = false; |
| 950 | descriptor_class = TexelBuffer; |
| 951 | if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) |
| 952 | storage_ = true; |
| 953 | }; |
| 954 | bool cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) { |
| 955 | // First validate buffer view |
| 956 | auto buffer_view = update->pTexelBufferView[index]; |
| 957 | if (!buffer_view_map_->count(buffer_view)) { |
| 958 | std::stringstream error_str; |
| 959 | error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view; |
| 960 | *error = error_str.str(); |
| 961 | return false; |
| 962 | } |
| 963 | updated = true; |
| 964 | buffer_view_ = buffer_view; |
| 965 | return true; |
| 966 | } |
| 967 | |
| 968 | bool cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src, std::string *error) { |
| 969 | // First validate buffer view |
| 970 | auto buffer_view = static_cast<const TexelDescriptor *>(src)->buffer_view_; |
| 971 | if (!buffer_view_map_->count(buffer_view)) { |
| 972 | std::stringstream error_str; |
| 973 | error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view; |
| 974 | *error = error_str.str(); |
| 975 | return false; |
| 976 | } |
| 977 | updated = true; |
| 978 | buffer_view_ = buffer_view; |
| 979 | return true; |
| 980 | } |