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