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 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 62 | |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 63 | VkDescriptorSetLayoutBinding const * |
| 64 | cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const { |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 65 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 66 | if (bi_itr != binding_to_index_map_.end()) { |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 67 | return bindings_[bi_itr->second].ptr(); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 68 | } |
| 69 | return nullptr; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 70 | } |
| 71 | VkDescriptorSetLayoutBinding const * |
| 72 | cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const { |
| 73 | if (index >= bindings_.size()) |
| 74 | return nullptr; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 75 | return bindings_[index].ptr(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 76 | } |
| 77 | // Return descriptorCount for given binding, 0 if index is unavailable |
| 78 | uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const { |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 79 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 80 | if (bi_itr != binding_to_index_map_.end()) { |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 81 | return bindings_[bi_itr->second].descriptorCount; |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 82 | } |
| 83 | return 0; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 84 | } |
| 85 | // Return descriptorCount for given index, 0 if index is unavailable |
| 86 | uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const { |
| 87 | if (index >= bindings_.size()) |
| 88 | return 0; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 89 | return bindings_[index].descriptorCount; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 90 | } |
| 91 | // For the given binding, return descriptorType |
| 92 | VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const { |
| 93 | assert(binding_to_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 94 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 95 | if (bi_itr != binding_to_index_map_.end()) { |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 96 | return bindings_[bi_itr->second].descriptorType; |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 97 | } |
| 98 | return VK_DESCRIPTOR_TYPE_MAX_ENUM; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 99 | } |
| 100 | // For the given index, return descriptorType |
| 101 | VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const { |
| 102 | assert(index < bindings_.size()); |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 103 | return bindings_[index].descriptorType; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 104 | } |
| 105 | // For the given global index, return descriptorType |
| 106 | // Currently just counting up through bindings_, may improve this in future |
| 107 | VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const { |
| 108 | uint32_t global_offset = 0; |
| 109 | for (auto binding : bindings_) { |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 110 | global_offset += binding.descriptorCount; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 111 | if (index < global_offset) |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 112 | return binding.descriptorType; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 113 | } |
| 114 | assert(0); // requested global index is out of bounds |
| 115 | return VK_DESCRIPTOR_TYPE_MAX_ENUM; |
| 116 | } |
| 117 | // For the given binding, return stageFlags |
| 118 | VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const { |
| 119 | assert(binding_to_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 120 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 121 | if (bi_itr != binding_to_index_map_.end()) { |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 122 | return bindings_[bi_itr->second].stageFlags; |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 123 | } |
| 124 | return VkShaderStageFlags(0); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 125 | } |
| 126 | // For the given binding, return start index |
| 127 | uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const { |
| 128 | assert(binding_to_global_start_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 129 | const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding); |
| 130 | if (btgsi_itr != binding_to_global_start_index_map_.end()) { |
| 131 | return btgsi_itr->second; |
| 132 | } |
| 133 | // In error case max uint32_t so index is out of bounds to break ASAP |
| 134 | return 0xFFFFFFFF; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 135 | } |
| 136 | // For the given binding, return end index |
| 137 | uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const { |
| 138 | assert(binding_to_global_end_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 139 | const auto &btgei_itr = binding_to_global_end_index_map_.find(binding); |
| 140 | if (btgei_itr != binding_to_global_end_index_map_.end()) { |
| 141 | return btgei_itr->second; |
| 142 | } |
| 143 | // In error case max uint32_t so index is out of bounds to break ASAP |
| 144 | return 0xFFFFFFFF; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 145 | } |
| 146 | // For given binding, return ptr to ImmutableSampler array |
| 147 | VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const { |
| 148 | assert(binding_to_index_map_.count(binding)); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 149 | const auto &bi_itr = binding_to_index_map_.find(binding); |
| 150 | if (bi_itr != binding_to_index_map_.end()) { |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 151 | return bindings_[bi_itr->second].pImmutableSamplers; |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 152 | } |
| 153 | return nullptr; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 154 | } |
| 155 | // For given index, return ptr to ImmutableSampler array |
| 156 | VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const { |
| 157 | assert(index < bindings_.size()); |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 158 | return bindings_[index].pImmutableSamplers; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 159 | } |
| 160 | // If our layout is compatible with rh_ds_layout, return true, |
| 161 | // else return false and fill in error_msg will description of what causes incompatibility |
| 162 | bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const { |
| 163 | // Trivial case |
| 164 | if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) |
| 165 | return true; |
| 166 | if (descriptor_count_ != rh_ds_layout->descriptor_count_) { |
| 167 | std::stringstream error_str; |
| 168 | error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout " |
| 169 | << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors."; |
| 170 | *error_msg = error_str.str(); |
| 171 | return false; // trivial fail case |
| 172 | } |
| 173 | // Descriptor counts match so need to go through bindings one-by-one |
| 174 | // and verify that type and stageFlags match |
| 175 | for (auto binding : bindings_) { |
| 176 | // TODO : Do we also need to check immutable samplers? |
| 177 | // VkDescriptorSetLayoutBinding *rh_binding; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 178 | if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 179 | std::stringstream error_str; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 180 | error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of " |
| 181 | << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout " |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 182 | << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of " |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 183 | << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 184 | *error_msg = error_str.str(); |
| 185 | return false; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 186 | } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 187 | std::stringstream error_str; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 188 | error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '" |
| 189 | << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 190 | << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '" |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 191 | << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'"; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 192 | *error_msg = error_str.str(); |
| 193 | return false; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 194 | } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 195 | std::stringstream error_str; |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 196 | error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags " |
| 197 | << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 198 | << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags " |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 199 | << rh_ds_layout->GetStageFlagsFromBinding(binding.binding); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 200 | *error_msg = error_str.str(); |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const { |
| 208 | if (!binding_to_index_map_.count(binding + 1)) |
| 209 | return false; |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 210 | auto const &bi_itr = binding_to_index_map_.find(binding); |
| 211 | if (bi_itr != binding_to_index_map_.end()) { |
| 212 | const auto &next_bi_itr = binding_to_index_map_.find(binding + 1); |
| 213 | if (next_bi_itr != binding_to_index_map_.end()) { |
Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 214 | auto type = bindings_[bi_itr->second].descriptorType; |
| 215 | auto stage_flags = bindings_[bi_itr->second].stageFlags; |
| 216 | auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false; |
| 217 | if ((type != bindings_[next_bi_itr->second].descriptorType) || |
| 218 | (stage_flags != bindings_[next_bi_itr->second].stageFlags) || |
| 219 | (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) { |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | return true; |
| 223 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 224 | } |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 225 | return false; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 226 | } |
Tobin Ehlis | 1f946f8 | 2016-05-05 12:03:44 -0600 | [diff] [blame] | 227 | // Starting at offset descriptor of given binding, parse over update_count |
| 228 | // descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent |
| 229 | // Consistency means that their type, stage flags, and whether or not they use immutable samplers matches |
| 230 | // If so, return true. If not, fill in error_msg and return false |
| 231 | bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count, |
| 232 | const char *type, const VkDescriptorSet set, |
| 233 | std::string *error_msg) const { |
| 234 | // Verify consecutive bindings match (if needed) |
| 235 | auto orig_binding = current_binding; |
| 236 | // Track count of descriptors in the current_bindings that are remaining to be updated |
| 237 | auto binding_remaining = GetDescriptorCountFromBinding(current_binding); |
| 238 | // First, it's legal to offset beyond your own binding so handle that case |
| 239 | // Really this is just searching for the binding in which the update begins and adjusting offset accordingly |
| 240 | while (offset >= binding_remaining) { |
| 241 | // Advance to next binding, decrement offset by binding size |
| 242 | offset -= binding_remaining; |
| 243 | binding_remaining = GetDescriptorCountFromBinding(++current_binding); |
| 244 | } |
| 245 | binding_remaining -= offset; |
| 246 | while (update_count > binding_remaining) { // While our updates overstep current binding |
| 247 | // Verify next consecutive binding matches type, stage flags & immutable sampler use |
| 248 | if (!IsNextBindingConsistent(current_binding++)) { |
| 249 | std::stringstream error_str; |
| 250 | error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #" |
| 251 | << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the " |
| 252 | "next binding is not consistent with current binding so this update is invalid."; |
| 253 | *error_msg = error_str.str(); |
| 254 | return false; |
| 255 | } |
| 256 | // For sake of this check consider the bindings updated and grab count for next binding |
| 257 | update_count -= binding_remaining; |
| 258 | binding_remaining = GetDescriptorCountFromBinding(current_binding); |
| 259 | } |
| 260 | return true; |
| 261 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 262 | |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 263 | cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count) |
| 264 | : required_descriptors_by_type{}, layout_nodes(count, nullptr) {} |
| 265 | |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 266 | cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout, |
| 267 | const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map, |
| 268 | const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *memory_map, |
| 269 | const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map, |
| 270 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map, |
| 271 | const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map, |
| 272 | const std::unordered_map<VkImage, IMAGE_NODE> *image_map, |
| 273 | const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map, |
| 274 | const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map) |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 275 | : some_update_(false), set_(set), p_layout_(layout), buffer_map_(buffer_map), memory_map_(memory_map), |
| 276 | buffer_view_map_(buffer_view_map), sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map), |
| 277 | image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 278 | // Foreach binding, create default descriptors of given type |
| 279 | for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) { |
| 280 | auto type = p_layout_->GetTypeFromIndex(i); |
| 281 | switch (type) { |
| 282 | case VK_DESCRIPTOR_TYPE_SAMPLER: { |
| 283 | auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i); |
| 284 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) { |
| 285 | if (immut_sampler) |
Chris Forbes | cb621ea | 2016-05-30 11:47:31 +1200 | [diff] [blame] | 286 | descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di)); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 287 | else |
Chris Forbes | cb621ea | 2016-05-30 11:47:31 +1200 | [diff] [blame] | 288 | descriptors_.emplace_back(new SamplerDescriptor()); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 289 | } |
| 290 | break; |
| 291 | } |
| 292 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: { |
| 293 | auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i); |
| 294 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) { |
| 295 | if (immut) |
Chris Forbes | cb621ea | 2016-05-30 11:47:31 +1200 | [diff] [blame] | 296 | descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di)); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 297 | else |
Chris Forbes | cb621ea | 2016-05-30 11:47:31 +1200 | [diff] [blame] | 298 | descriptors_.emplace_back(new ImageSamplerDescriptor()); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 299 | } |
| 300 | break; |
| 301 | } |
| 302 | // ImageDescriptors |
| 303 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 304 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 305 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 306 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) |
Chris Forbes | cb621ea | 2016-05-30 11:47:31 +1200 | [diff] [blame] | 307 | descriptors_.emplace_back(new ImageDescriptor(type)); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 308 | break; |
| 309 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 310 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 311 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) |
Chris Forbes | cb621ea | 2016-05-30 11:47:31 +1200 | [diff] [blame] | 312 | descriptors_.emplace_back(new TexelDescriptor(type)); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 313 | break; |
| 314 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 315 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 316 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 317 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 318 | for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) |
Chris Forbes | cb621ea | 2016-05-30 11:47:31 +1200 | [diff] [blame] | 319 | descriptors_.emplace_back(new BufferDescriptor(type)); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 320 | break; |
| 321 | default: |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 322 | assert(0); // Bad descriptor type specified |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 327 | |
Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 328 | cvdescriptorset::DescriptorSet::~DescriptorSet() { |
| 329 | InvalidateBoundCmdBuffers(); |
| 330 | // Remove link to any cmd buffers |
| 331 | for (auto cb : bound_cmd_buffers_) { |
| 332 | for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) { |
| 333 | cb->lastBound[i].uniqueBoundSets.erase(this); |
| 334 | } |
| 335 | } |
| 336 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 337 | // Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec? |
| 338 | bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const { |
| 339 | return layout->IsCompatible(p_layout_, error); |
| 340 | } |
| 341 | // Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time |
| 342 | // This includes validating that all descriptors in the given bindings are updated, |
| 343 | // that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers. |
| 344 | // Return true if state is acceptable, or false and write an error message into error string |
| 345 | bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings, |
| 346 | const std::vector<uint32_t> &dynamic_offsets, std::string *error) const { |
iostrows | 5eb9765 | 2016-06-02 15:56:26 +0200 | [diff] [blame] | 347 | auto dyn_offset_index = 0; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 348 | for (auto binding : bindings) { |
| 349 | auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding); |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 350 | if (descriptors_[start_idx]->IsImmutableSampler()) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 351 | // Nothing to do for strictly immutable sampler |
| 352 | } else { |
| 353 | auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 354 | for (uint32_t i = start_idx; i <= end_idx; ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 355 | if (!descriptors_[i]->updated) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 356 | std::stringstream error_str; |
| 357 | error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i |
| 358 | << " is being used in draw but has not been updated."; |
| 359 | *error = error_str.str(); |
| 360 | return false; |
| 361 | } else { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 362 | if (GeneralBuffer == descriptors_[i]->GetClass()) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 363 | // Verify that buffers are valid |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 364 | auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 365 | auto buffer_node = buffer_map_->find(buffer); |
| 366 | if (buffer_node == buffer_map_->end()) { |
| 367 | std::stringstream error_str; |
| 368 | error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i |
| 369 | << " references invalid buffer " << buffer << "."; |
| 370 | *error = error_str.str(); |
| 371 | return false; |
| 372 | } else { |
| 373 | auto mem_entry = memory_map_->find(buffer_node->second.mem); |
| 374 | if (mem_entry == memory_map_->end()) { |
| 375 | std::stringstream error_str; |
| 376 | error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i |
| 377 | << " uses buffer " << buffer << " that references invalid memory " |
| 378 | << buffer_node->second.mem << "."; |
| 379 | *error = error_str.str(); |
| 380 | return false; |
| 381 | } |
| 382 | } |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 383 | if (descriptors_[i]->IsDynamic()) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 384 | // Validate that dynamic offsets are within the buffer |
| 385 | auto buffer_size = buffer_node->second.createInfo.size; |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 386 | auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange(); |
| 387 | auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 388 | auto dyn_offset = dynamic_offsets[dyn_offset_index++]; |
| 389 | if (VK_WHOLE_SIZE == range) { |
| 390 | if ((dyn_offset + desc_offset) > buffer_size) { |
| 391 | std::stringstream error_str; |
| 392 | error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i |
| 393 | << " uses buffer " << buffer |
| 394 | << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset |
| 395 | << " combined with offset " << desc_offset << " that oversteps the buffer size of " |
| 396 | << buffer_size << "."; |
| 397 | *error = error_str.str(); |
| 398 | return false; |
| 399 | } |
| 400 | } else { |
| 401 | if ((dyn_offset + desc_offset + range) > buffer_size) { |
| 402 | std::stringstream error_str; |
| 403 | error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i |
| 404 | << " uses buffer " << buffer << " with dynamic offset " << dyn_offset |
| 405 | << " combined with offset " << desc_offset << " and range " << range |
| 406 | << " that oversteps the buffer size of " << buffer_size << "."; |
| 407 | *error = error_str.str(); |
| 408 | return false; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | return true; |
| 418 | } |
| 419 | // For given bindings, place any update buffers or images into the passed-in unordered_sets |
| 420 | uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings, |
| 421 | std::unordered_set<VkBuffer> *buffer_set, |
| 422 | std::unordered_set<VkImageView> *image_set) const { |
| 423 | auto num_updates = 0; |
| 424 | for (auto binding : bindings) { |
| 425 | auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding); |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 426 | if (descriptors_[start_idx]->IsStorage()) { |
| 427 | if (Image == descriptors_[start_idx]->descriptor_class) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 428 | for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 429 | if (descriptors_[start_idx + i]->updated) { |
| 430 | image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView()); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 431 | num_updates++; |
| 432 | } |
| 433 | } |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 434 | } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 435 | for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 436 | if (descriptors_[start_idx + i]->updated) { |
| 437 | auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView(); |
Tobin Ehlis | 0bc3063 | 2016-05-05 10:16:02 -0600 | [diff] [blame] | 438 | const auto &buff_pair = buffer_view_map_->find(bufferview); |
| 439 | if (buff_pair != buffer_view_map_->end()) { |
| 440 | buffer_set->insert(buff_pair->second.buffer); |
| 441 | num_updates++; |
| 442 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 443 | } |
| 444 | } |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 445 | } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 446 | for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 447 | if (descriptors_[start_idx + i]->updated) { |
| 448 | buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer()); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 449 | num_updates++; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return num_updates; |
| 456 | } |
Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 457 | // Set is being deleted or updates so invalidate all bound cmd buffers |
| 458 | void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() { |
| 459 | for (auto cb_node : bound_cmd_buffers_) { |
| 460 | cb_node->state = CB_INVALID; |
| 461 | } |
| 462 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 463 | // Perform write update in given update struct |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 464 | void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 465 | auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement; |
| 466 | // perform update |
| 467 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 468 | descriptors_[start_idx + di]->WriteUpdate(update, di); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 469 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 470 | if (update->descriptorCount) |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 471 | some_update_ = true; |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 472 | |
Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 473 | InvalidateBoundCmdBuffers(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 474 | } |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 475 | // Validate Copy update |
| 476 | bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update, |
| 477 | const DescriptorSet *src_set, std::string *error) { |
Tobin Ehlis | 03d61de | 2016-05-17 08:31:46 -0600 | [diff] [blame] | 478 | // Verify idle ds |
| 479 | if (in_use.load()) { |
| 480 | std::stringstream error_str; |
| 481 | error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_ |
| 482 | << " that is in use by a command buffer."; |
| 483 | *error = error_str.str(); |
| 484 | return false; |
| 485 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 486 | if (!p_layout_->HasBinding(update->dstBinding)) { |
| 487 | std::stringstream error_str; |
| 488 | error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << "."; |
| 489 | *error = error_str.str(); |
| 490 | return false; |
| 491 | } |
| 492 | if (!src_set->HasBinding(update->srcBinding)) { |
| 493 | std::stringstream error_str; |
| 494 | error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << "."; |
| 495 | *error = error_str.str(); |
| 496 | return false; |
| 497 | } |
| 498 | // src & dst set bindings are valid |
| 499 | // Check bounds of src & dst |
| 500 | auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement; |
| 501 | if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) { |
| 502 | // SRC update out of bounds |
| 503 | std::stringstream error_str; |
| 504 | error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding |
| 505 | << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding) |
| 506 | << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount |
| 507 | << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << "."; |
| 508 | *error = error_str.str(); |
| 509 | return false; |
| 510 | } |
| 511 | auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement; |
| 512 | if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) { |
| 513 | // DST update out of bounds |
| 514 | std::stringstream error_str; |
| 515 | error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding |
| 516 | << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) |
| 517 | << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount |
| 518 | << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << "."; |
| 519 | *error = error_str.str(); |
| 520 | return false; |
| 521 | } |
| 522 | // Check that types match |
| 523 | auto src_type = src_set->GetTypeFromBinding(update->srcBinding); |
| 524 | auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding); |
| 525 | if (src_type != dst_type) { |
| 526 | std::stringstream error_str; |
| 527 | error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type " |
| 528 | << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #" |
| 529 | << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match."; |
| 530 | *error = error_str.str(); |
| 531 | return false; |
| 532 | } |
| 533 | // Verify consistency of src & dst bindings if update crosses binding boundaries |
Tobin Ehlis | 1f946f8 | 2016-05-05 12:03:44 -0600 | [diff] [blame] | 534 | if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount, |
| 535 | "copy update from", src_set->GetSet(), error)) || |
| 536 | (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to", |
| 537 | set_, error))) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 538 | return false; |
| 539 | } |
Tobin Ehlis | d41e7b6 | 2016-05-19 07:56:18 -0600 | [diff] [blame] | 540 | // First make sure source descriptors are updated |
| 541 | for (uint32_t i = 0; i < update->descriptorCount; ++i) { |
| 542 | if (!src_set->descriptors_[src_start_idx + i]) { |
| 543 | std::stringstream error_str; |
| 544 | error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset " |
| 545 | << update->srcArrayElement + i << " has not been updated."; |
| 546 | *error = error_str.str(); |
| 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | // Update parameters all look good and descriptor updated so verify update contents |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 551 | if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error)) |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 552 | return false; |
| 553 | |
| 554 | // All checks passed so update is good |
| 555 | return true; |
| 556 | } |
| 557 | // Perform Copy update |
| 558 | void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 559 | auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement; |
| 560 | auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 561 | // Update parameters all look good so perform update |
| 562 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 563 | descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get()); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 564 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 565 | if (update->descriptorCount) |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 566 | some_update_ = true; |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 567 | |
Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 568 | InvalidateBoundCmdBuffers(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 569 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 570 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 571 | cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 572 | updated = false; |
| 573 | descriptor_class = PlainSampler; |
| 574 | }; |
| 575 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 576 | cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 577 | updated = false; |
| 578 | descriptor_class = PlainSampler; |
| 579 | if (immut) { |
| 580 | sampler_ = *immut; |
| 581 | immutable_ = true; |
| 582 | updated = true; |
| 583 | } |
| 584 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 585 | |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 586 | bool cvdescriptorset::ValidateSampler(const VkSampler sampler, |
| 587 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) { |
Tobin Ehlis | 81f1785 | 2016-05-05 09:04:33 -0600 | [diff] [blame] | 588 | return (sampler_map->count(sampler) != 0); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 589 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 590 | |
Tobin Ehlis | 554bf38 | 2016-05-24 11:14:43 -0600 | [diff] [blame] | 591 | bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type, |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 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; |
Tobin Ehlis | 1809f91 | 2016-05-25 09:24:36 -0600 | [diff] [blame] | 603 | } |
| 604 | // Validate that imageLayout is compatible with aspect_mask and image format |
| 605 | // and validate that image usage bits are correct for given usage |
| 606 | VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask; |
| 607 | VkImage image = image_pair->second.image; |
| 608 | VkFormat format = VK_FORMAT_MAX_ENUM; |
| 609 | VkImageUsageFlags usage = 0; |
| 610 | auto img_pair = image_map->find(image); |
| 611 | if (img_pair != image_map->end()) { |
| 612 | format = img_pair->second.createInfo.format; |
| 613 | usage = img_pair->second.createInfo.usage; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 614 | } else { |
Tobin Ehlis | 1809f91 | 2016-05-25 09:24:36 -0600 | [diff] [blame] | 615 | // Also need to check the swapchains. |
| 616 | auto swapchain_pair = image_to_swapchain_map->find(image); |
| 617 | if (swapchain_pair != image_to_swapchain_map->end()) { |
| 618 | VkSwapchainKHR swapchain = swapchain_pair->second; |
| 619 | auto swapchain_pair = swapchain_map->find(swapchain); |
| 620 | if (swapchain_pair != swapchain_map->end()) { |
| 621 | format = swapchain_pair->second->createInfo.imageFormat; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 622 | } |
| 623 | } |
Tobin Ehlis | 1809f91 | 2016-05-25 09:24:36 -0600 | [diff] [blame] | 624 | } |
| 625 | // First validate that format and layout are compatible |
| 626 | if (format == VK_FORMAT_MAX_ENUM) { |
| 627 | std::stringstream error_str; |
| 628 | error_str << "Invalid image (" << image << ") in imageView (" << image_view << ")."; |
| 629 | *error = error_str.str(); |
| 630 | return false; |
| 631 | } |
| 632 | bool ds = vk_format_is_depth_or_stencil(format); |
| 633 | switch (image_layout) { |
| 634 | case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: |
| 635 | // Only Color bit must be set |
| 636 | if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 637 | std::stringstream error_str; |
Tobin Ehlis | 1809f91 | 2016-05-25 09:24:36 -0600 | [diff] [blame] | 638 | error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does " |
| 639 | "not have VK_IMAGE_ASPECT_COLOR_BIT set."; |
Tobin Ehlis | 554bf38 | 2016-05-24 11:14:43 -0600 | [diff] [blame] | 640 | *error = error_str.str(); |
| 641 | return false; |
| 642 | } |
Tobin Ehlis | 1809f91 | 2016-05-25 09:24:36 -0600 | [diff] [blame] | 643 | // format must NOT be DS |
| 644 | if (ds) { |
| 645 | std::stringstream error_str; |
| 646 | error_str << "ImageView (" << image_view |
| 647 | << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is " |
| 648 | << string_VkFormat(format) << " which is not a color format."; |
| 649 | *error = error_str.str(); |
| 650 | return false; |
| 651 | } |
| 652 | break; |
| 653 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: |
| 654 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: |
| 655 | // Depth or stencil bit must be set, but both must NOT be set |
| 656 | if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) { |
| 657 | if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) { |
| 658 | // both must NOT be set |
| 659 | std::stringstream error_str; |
| 660 | error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set"; |
| 661 | *error = error_str.str(); |
| 662 | return false; |
| 663 | } |
| 664 | } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) { |
| 665 | // Neither were set |
| 666 | std::stringstream error_str; |
| 667 | error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout) |
| 668 | << " but does not have STENCIL or DEPTH aspects set"; |
| 669 | *error = error_str.str(); |
| 670 | return false; |
| 671 | } |
| 672 | // format must be DS |
| 673 | if (!ds) { |
| 674 | std::stringstream error_str; |
| 675 | error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout) |
| 676 | << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format."; |
| 677 | *error = error_str.str(); |
| 678 | return false; |
| 679 | } |
| 680 | break; |
| 681 | default: |
| 682 | // anything to check for other layouts? |
| 683 | break; |
| 684 | } |
| 685 | // Now validate that usage flags are correctly set for given type of update |
| 686 | std::string error_usage_bit; |
| 687 | switch (type) { |
| 688 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 689 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: { |
| 690 | if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) { |
| 691 | error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT"; |
| 692 | } |
| 693 | break; |
| 694 | } |
| 695 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: { |
| 696 | if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) { |
| 697 | error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT"; |
| 698 | } |
| 699 | break; |
| 700 | } |
| 701 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: { |
| 702 | if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) { |
| 703 | error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT"; |
| 704 | } |
| 705 | break; |
| 706 | } |
| 707 | default: |
| 708 | break; |
| 709 | } |
| 710 | if (!error_usage_bit.empty()) { |
| 711 | std::stringstream error_str; |
| 712 | error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage |
| 713 | << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have " |
| 714 | << error_usage_bit << " set."; |
| 715 | *error = error_str.str(); |
| 716 | return false; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 717 | } |
| 718 | return true; |
| 719 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 720 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 721 | void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) { |
| 722 | sampler_ = update->pImageInfo[index].sampler; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 723 | updated = true; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 724 | } |
| 725 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 726 | void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 727 | if (!immutable_) { |
| 728 | auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 729 | sampler_ = update_sampler; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 730 | } |
| 731 | updated = true; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 732 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 733 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 734 | cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor() |
| 735 | : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 736 | updated = false; |
| 737 | descriptor_class = ImageSampler; |
| 738 | } |
| 739 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 740 | cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut) |
| 741 | : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 742 | updated = false; |
| 743 | descriptor_class = ImageSampler; |
| 744 | if (immut) { |
| 745 | sampler_ = *immut; |
| 746 | immutable_ = true; |
| 747 | updated = true; |
| 748 | } |
| 749 | } |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 750 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 751 | void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 752 | updated = true; |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 753 | const auto &image_info = update->pImageInfo[index]; |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 754 | sampler_ = image_info.sampler; |
| 755 | image_view_ = image_info.imageView; |
| 756 | image_layout_ = image_info.imageLayout; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 757 | } |
| 758 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 759 | void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 760 | if (!immutable_) { |
| 761 | auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 762 | sampler_ = update_sampler; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 763 | } |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 764 | auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_; |
| 765 | auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 766 | updated = true; |
| 767 | image_view_ = image_view; |
| 768 | image_layout_ = image_layout; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 769 | } |
| 770 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 771 | cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type) |
| 772 | : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 773 | updated = false; |
| 774 | descriptor_class = Image; |
| 775 | if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) |
| 776 | storage_ = true; |
| 777 | }; |
| 778 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 779 | void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 780 | updated = true; |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 781 | const auto &image_info = update->pImageInfo[index]; |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 782 | image_view_ = image_info.imageView; |
| 783 | image_layout_ = image_info.imageLayout; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 784 | } |
| 785 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 786 | void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 787 | auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_; |
| 788 | auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 789 | updated = true; |
| 790 | image_view_ = image_view; |
| 791 | image_layout_ = image_layout; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 792 | } |
| 793 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 794 | cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type) |
| 795 | : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 796 | updated = false; |
| 797 | descriptor_class = GeneralBuffer; |
| 798 | if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) { |
| 799 | dynamic_ = true; |
| 800 | } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) { |
| 801 | storage_ = true; |
| 802 | } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) { |
| 803 | dynamic_ = true; |
| 804 | storage_ = true; |
| 805 | } |
| 806 | } |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 807 | void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 808 | updated = true; |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 809 | const auto &buffer_info = update->pBufferInfo[index]; |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 810 | buffer_ = buffer_info.buffer; |
| 811 | offset_ = buffer_info.offset; |
| 812 | range_ = buffer_info.range; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 813 | } |
| 814 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 815 | void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) { |
| 816 | auto buff_desc = static_cast<const BufferDescriptor *>(src); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 817 | updated = true; |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 818 | buffer_ = buff_desc->buffer_; |
| 819 | offset_ = buff_desc->offset_; |
| 820 | range_ = buff_desc->range_; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 821 | } |
| 822 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 823 | cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 824 | updated = false; |
| 825 | descriptor_class = TexelBuffer; |
| 826 | if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) |
| 827 | storage_ = true; |
| 828 | }; |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 829 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 830 | void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 831 | updated = true; |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 832 | buffer_view_ = update->pTexelBufferView[index]; |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 833 | } |
| 834 | |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 835 | void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) { |
| 836 | updated = true; |
| 837 | buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_; |
| 838 | } |
| 839 | // This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated |
| 840 | // sets, and then calls their respective Validate[Write|Copy]Update functions. |
| 841 | // If the update hits an issue for which the callback returns "true", meaning that the call down the chain should |
| 842 | // be skipped, then true is returned. |
| 843 | // If there is no issue with the update, then false is returned. |
| 844 | bool cvdescriptorset::ValidateUpdateDescriptorSets( |
| 845 | const debug_report_data *report_data, const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map, |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 846 | uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count, const VkCopyDescriptorSet *p_cds) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 847 | bool skip_call = false; |
| 848 | // Validate Write updates |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 849 | for (uint32_t i = 0; i < write_count; i++) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 850 | auto dest_set = p_wds[i].dstSet; |
| 851 | auto set_pair = set_map.find(dest_set); |
| 852 | if (set_pair == set_map.end()) { |
| 853 | skip_call |= |
| 854 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 855 | reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS", |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 856 | "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.", |
| 857 | reinterpret_cast<uint64_t &>(dest_set)); |
| 858 | } else { |
| 859 | std::string error_str; |
| 860 | if (!set_pair->second->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) { |
| 861 | skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, |
| 862 | reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS", |
| 863 | "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64 |
| 864 | " with error: %s", |
| 865 | reinterpret_cast<uint64_t &>(dest_set), error_str.c_str()); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | // Now validate copy updates |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 870 | for (uint32_t i = 0; i < copy_count; ++i) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 871 | auto dst_set = p_cds[i].dstSet; |
| 872 | auto src_set = p_cds[i].srcSet; |
| 873 | auto src_pair = set_map.find(src_set); |
| 874 | auto dst_pair = set_map.find(dst_set); |
| 875 | if (src_pair == set_map.end()) { |
| 876 | skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 877 | reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS", |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 878 | "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64 |
| 879 | " that has not been allocated.", |
| 880 | reinterpret_cast<uint64_t &>(src_set)); |
| 881 | } else if (dst_pair == set_map.end()) { |
| 882 | skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 883 | reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS", |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 884 | "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64 |
| 885 | " that has not been allocated.", |
| 886 | reinterpret_cast<uint64_t &>(dst_set)); |
| 887 | } else { |
| 888 | std::string error_str; |
| 889 | if (!dst_pair->second->ValidateCopyUpdate(report_data, &p_cds[i], src_pair->second, &error_str)) { |
| 890 | skip_call |= |
| 891 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, |
| 892 | reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS", |
| 893 | "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64 |
| 894 | " to Descriptor Set 0x%" PRIx64 " with error: %s", |
| 895 | reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str()); |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | return skip_call; |
| 900 | } |
| 901 | // This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated |
| 902 | // sets, and then calls their respective Perform[Write|Copy]Update functions. |
| 903 | // Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets() |
| 904 | // with the same set of updates. |
| 905 | // This is split from the validate code to allow validation prior to calling down the chain, and then update after |
| 906 | // calling down the chain. |
| 907 | void cvdescriptorset::PerformUpdateDescriptorSets( |
Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 908 | const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map, uint32_t write_count, |
| 909 | const VkWriteDescriptorSet *p_wds, uint32_t copy_count, const VkCopyDescriptorSet *p_cds) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 910 | // Write updates first |
| 911 | uint32_t i = 0; |
| 912 | for (i = 0; i < write_count; ++i) { |
| 913 | auto dest_set = p_wds[i].dstSet; |
| 914 | auto set_pair = set_map.find(dest_set); |
| 915 | if (set_pair != set_map.end()) { |
| 916 | set_pair->second->PerformWriteUpdate(&p_wds[i]); |
| 917 | } |
| 918 | } |
| 919 | // Now copy updates |
| 920 | for (i = 0; i < copy_count; ++i) { |
| 921 | auto dst_set = p_cds[i].dstSet; |
| 922 | auto src_set = p_cds[i].srcSet; |
| 923 | auto src_pair = set_map.find(src_set); |
| 924 | auto dst_pair = set_map.find(dst_set); |
| 925 | if (src_pair != set_map.end() && dst_pair != set_map.end()) { |
| 926 | dst_pair->second->PerformCopyUpdate(&p_cds[i], src_pair->second); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | // Validate the state for a given write update but don't actually perform the update |
| 931 | // If an error would occur for this update, return false and fill in details in error_msg string |
| 932 | bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update, |
| 933 | std::string *error_msg) { |
| 934 | // Verify idle ds |
| 935 | if (in_use.load()) { |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 936 | std::stringstream error_str; |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 937 | error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_ |
| 938 | << " that is in use by a command buffer."; |
| 939 | *error_msg = error_str.str(); |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 940 | return false; |
| 941 | } |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 942 | // Verify dst binding exists |
| 943 | if (!p_layout_->HasBinding(update->dstBinding)) { |
| 944 | std::stringstream error_str; |
| 945 | error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << "."; |
| 946 | *error_msg = error_str.str(); |
| 947 | return false; |
Tobin Ehlis | 57ae28f | 2016-05-24 12:35:57 -0600 | [diff] [blame] | 948 | } |
| 949 | // We know that binding is valid, verify update and do update on each descriptor |
| 950 | auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement; |
| 951 | auto type = p_layout_->GetTypeFromBinding(update->dstBinding); |
| 952 | if (type != update->descriptorType) { |
| 953 | std::stringstream error_str; |
| 954 | error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type " |
| 955 | << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType); |
| 956 | *error_msg = error_str.str(); |
| 957 | return false; |
| 958 | } |
| 959 | if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) { |
| 960 | std::stringstream error_str; |
| 961 | error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with " |
| 962 | << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount |
| 963 | << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) |
| 964 | << " combined with update array element offset of " << update->dstArrayElement |
| 965 | << " oversteps the size of this descriptor set."; |
| 966 | *error_msg = error_str.str(); |
| 967 | return false; |
| 968 | } |
| 969 | // Verify consecutive bindings match (if needed) |
| 970 | if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to", |
| 971 | set_, error_msg)) |
| 972 | return false; |
| 973 | // Update is within bounds and consistent so last step is to validate update contents |
| 974 | if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) { |
| 975 | std::stringstream error_str; |
| 976 | error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding |
| 977 | << " failed with error message: " << error_msg->c_str(); |
| 978 | *error_msg = error_str.str(); |
| 979 | return false; |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 980 | } |
| 981 | // All checks passed, update is clean |
Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 982 | return true; |
Tobin Ehlis | 03d61de | 2016-05-17 08:31:46 -0600 | [diff] [blame] | 983 | } |
Tobin Ehlis | 6bd2b98 | 2016-05-24 12:33:42 -0600 | [diff] [blame] | 984 | // For the given buffer, verify that its creation parameters are appropriate for the given type |
| 985 | // If there's an error, update the error string with details and return false, else return true |
| 986 | bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkBuffer buffer, VkDescriptorType type, std::string *error) const { |
| 987 | // First make sure that buffer is valid |
| 988 | auto buff_it = buffer_map_->find(buffer); |
| 989 | if (buff_it == buffer_map_->end()) { |
| 990 | std::stringstream error_str; |
| 991 | error_str << "Invalid VkBuffer: " << buffer; |
| 992 | *error = error_str.str(); |
| 993 | return false; |
| 994 | } |
| 995 | // Verify that usage bits set correctly for given type |
| 996 | auto usage = buff_it->second.createInfo.usage; |
| 997 | std::string error_usage_bit; |
| 998 | switch (type) { |
| 999 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 1000 | if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) { |
| 1001 | error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT"; |
| 1002 | } |
| 1003 | break; |
| 1004 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 1005 | if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) { |
| 1006 | error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT"; |
| 1007 | } |
| 1008 | break; |
| 1009 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 1010 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 1011 | if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) { |
| 1012 | error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT"; |
| 1013 | } |
| 1014 | break; |
| 1015 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 1016 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 1017 | if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) { |
| 1018 | error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT"; |
| 1019 | } |
| 1020 | break; |
| 1021 | default: |
| 1022 | break; |
| 1023 | } |
| 1024 | if (!error_usage_bit.empty()) { |
| 1025 | std::stringstream error_str; |
| 1026 | error_str << "Buffer (" << buffer << ") with usage mask 0x" << usage << " being used for a descriptor update of type " |
| 1027 | << string_VkDescriptorType(type) << " does not have " << error_usage_bit << " set."; |
| 1028 | *error = error_str.str(); |
| 1029 | return false; |
| 1030 | } |
| 1031 | return true; |
| 1032 | } |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1033 | // Verify that the contents of the update are ok, but don't perform actual update |
| 1034 | bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index, |
| 1035 | std::string *error) const { |
| 1036 | switch (update->descriptorType) { |
| 1037 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: { |
| 1038 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1039 | // Validate image |
| 1040 | auto image_view = update->pImageInfo[di].imageView; |
| 1041 | auto image_layout = update->pImageInfo[di].imageLayout; |
Tobin Ehlis | 554bf38 | 2016-05-24 11:14:43 -0600 | [diff] [blame] | 1042 | if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, image_view_map_, image_map_, |
| 1043 | image_to_swapchain_map_, swapchain_map_, error)) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1044 | std::stringstream error_str; |
| 1045 | error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str(); |
| 1046 | *error = error_str.str(); |
| 1047 | return false; |
| 1048 | } |
| 1049 | } |
| 1050 | // Intentional fall-through to validate sampler |
| 1051 | } |
| 1052 | case VK_DESCRIPTOR_TYPE_SAMPLER: { |
| 1053 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1054 | if (!descriptors_[index + di].get()->IsImmutableSampler()) { |
| 1055 | if (!ValidateSampler(update->pImageInfo[di].sampler, sampler_map_)) { |
| 1056 | std::stringstream error_str; |
| 1057 | error_str << "Attempted write update to sampler descriptor with invalid sampler: " |
| 1058 | << update->pImageInfo[di].sampler << "."; |
| 1059 | *error = error_str.str(); |
| 1060 | return false; |
| 1061 | } |
| 1062 | } else { |
| 1063 | // TODO : Warn here |
| 1064 | } |
| 1065 | } |
| 1066 | break; |
| 1067 | } |
| 1068 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 1069 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 1070 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: { |
| 1071 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1072 | auto image_view = update->pImageInfo[di].imageView; |
| 1073 | auto image_layout = update->pImageInfo[di].imageLayout; |
Tobin Ehlis | 554bf38 | 2016-05-24 11:14:43 -0600 | [diff] [blame] | 1074 | if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, image_view_map_, image_map_, |
| 1075 | image_to_swapchain_map_, swapchain_map_, error)) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1076 | std::stringstream error_str; |
| 1077 | error_str << "Attempted write update to image descriptor failed due to: " << error->c_str(); |
| 1078 | *error = error_str.str(); |
| 1079 | return false; |
| 1080 | } |
| 1081 | } |
| 1082 | break; |
| 1083 | } |
| 1084 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 1085 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: { |
| 1086 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1087 | auto buffer_view = update->pTexelBufferView[di]; |
Tobin Ehlis | 6bd2b98 | 2016-05-24 12:33:42 -0600 | [diff] [blame] | 1088 | auto buffer_view_it = buffer_view_map_->find(buffer_view); |
| 1089 | if (buffer_view_it == buffer_view_map_->end()) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1090 | std::stringstream error_str; |
| 1091 | error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view; |
| 1092 | *error = error_str.str(); |
| 1093 | return false; |
| 1094 | } |
Tobin Ehlis | 6bd2b98 | 2016-05-24 12:33:42 -0600 | [diff] [blame] | 1095 | auto buffer = buffer_view_it->second.buffer; |
| 1096 | if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) { |
| 1097 | std::stringstream error_str; |
| 1098 | error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str(); |
| 1099 | *error = error_str.str(); |
| 1100 | return false; |
| 1101 | } |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1102 | } |
| 1103 | break; |
| 1104 | } |
| 1105 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 1106 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 1107 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 1108 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: { |
| 1109 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1110 | auto buffer = update->pBufferInfo[di].buffer; |
Tobin Ehlis | 6bd2b98 | 2016-05-24 12:33:42 -0600 | [diff] [blame] | 1111 | if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1112 | std::stringstream error_str; |
Tobin Ehlis | 6bd2b98 | 2016-05-24 12:33:42 -0600 | [diff] [blame] | 1113 | error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str(); |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1114 | *error = error_str.str(); |
| 1115 | return false; |
| 1116 | } |
| 1117 | } |
| 1118 | break; |
| 1119 | } |
| 1120 | default: |
| 1121 | assert(0); // We've already verified update type so should never get here |
| 1122 | break; |
| 1123 | } |
| 1124 | // All checks passed so update contents are good |
| 1125 | return true; |
| 1126 | } |
| 1127 | // Verify that the contents of the update are ok, but don't perform actual update |
| 1128 | bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set, |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1129 | VkDescriptorType type, uint32_t index, std::string *error) const { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1130 | switch (src_set->descriptors_[index]->descriptor_class) { |
| 1131 | case PlainSampler: { |
| 1132 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1133 | if (!src_set->descriptors_[index + di]->IsImmutableSampler()) { |
| 1134 | auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler(); |
| 1135 | if (!ValidateSampler(update_sampler, sampler_map_)) { |
| 1136 | std::stringstream error_str; |
| 1137 | error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << "."; |
| 1138 | *error = error_str.str(); |
| 1139 | return false; |
| 1140 | } |
| 1141 | } else { |
| 1142 | // TODO : Warn here |
| 1143 | } |
| 1144 | } |
| 1145 | break; |
| 1146 | } |
| 1147 | case ImageSampler: { |
| 1148 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1149 | auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get()); |
| 1150 | // First validate sampler |
| 1151 | if (!img_samp_desc->IsImmutableSampler()) { |
| 1152 | auto update_sampler = img_samp_desc->GetSampler(); |
| 1153 | if (!ValidateSampler(update_sampler, sampler_map_)) { |
| 1154 | std::stringstream error_str; |
| 1155 | error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << "."; |
| 1156 | *error = error_str.str(); |
| 1157 | return false; |
| 1158 | } |
| 1159 | } else { |
| 1160 | // TODO : Warn here |
| 1161 | } |
| 1162 | // Validate image |
| 1163 | auto image_view = img_samp_desc->GetImageView(); |
| 1164 | auto image_layout = img_samp_desc->GetImageLayout(); |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1165 | if (!ValidateImageUpdate(image_view, image_layout, type, image_view_map_, image_map_, image_to_swapchain_map_, |
| 1166 | swapchain_map_, error)) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1167 | std::stringstream error_str; |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1168 | error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str(); |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1169 | *error = error_str.str(); |
| 1170 | return false; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | case Image: { |
| 1175 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1176 | auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get()); |
| 1177 | auto image_view = img_desc->GetImageView(); |
| 1178 | auto image_layout = img_desc->GetImageLayout(); |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1179 | if (!ValidateImageUpdate(image_view, image_layout, type, image_view_map_, image_map_, image_to_swapchain_map_, |
| 1180 | swapchain_map_, error)) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1181 | std::stringstream error_str; |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1182 | error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str(); |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1183 | *error = error_str.str(); |
| 1184 | return false; |
| 1185 | } |
| 1186 | } |
| 1187 | break; |
| 1188 | } |
| 1189 | case TexelBuffer: { |
| 1190 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1191 | auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView(); |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1192 | auto bv_it = buffer_view_map_->find(buffer_view); |
| 1193 | if (bv_it == buffer_view_map_->end()) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1194 | std::stringstream error_str; |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1195 | error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view; |
| 1196 | *error = error_str.str(); |
| 1197 | return false; |
| 1198 | } |
| 1199 | auto buffer = bv_it->second.buffer; |
| 1200 | if (!ValidateBufferUpdate(buffer, type, error)) { |
| 1201 | std::stringstream error_str; |
| 1202 | error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str(); |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1203 | *error = error_str.str(); |
| 1204 | return false; |
| 1205 | } |
| 1206 | } |
| 1207 | break; |
| 1208 | } |
| 1209 | case GeneralBuffer: { |
| 1210 | for (uint32_t di = 0; di < update->descriptorCount; ++di) { |
| 1211 | auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer(); |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1212 | if (!ValidateBufferUpdate(buffer, type, error)) { |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1213 | std::stringstream error_str; |
Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 1214 | error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str(); |
Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 1215 | *error = error_str.str(); |
| 1216 | return false; |
| 1217 | } |
| 1218 | } |
| 1219 | break; |
| 1220 | } |
| 1221 | default: |
| 1222 | assert(0); // We've already verified update type so should never get here |
| 1223 | break; |
| 1224 | } |
| 1225 | // All checks passed so update contents are good |
| 1226 | return true; |
Chris Forbes | b4e0bdb | 2016-05-31 16:34:40 +1200 | [diff] [blame] | 1227 | } |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1228 | // Verify that the state at allocate time is correct, but don't actually allocate the sets yet |
| 1229 | bool cvdescriptorset::ValidateAllocateDescriptorSets( |
| 1230 | const debug_report_data *report_data, const VkDescriptorSetAllocateInfo *p_alloc_info, |
| 1231 | const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &set_layout_map, |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1232 | const std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> &pool_map, AllocateDescriptorSetsData *ds_data) { |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1233 | bool skip_call = false; |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1234 | |
| 1235 | for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) { |
| 1236 | auto layout_it = set_layout_map.find(p_alloc_info->pSetLayouts[i]); |
| 1237 | if (layout_it == set_layout_map.end()) { |
| 1238 | skip_call |= |
| 1239 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, |
| 1240 | reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS", |
| 1241 | "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call", |
| 1242 | reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i])); |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1243 | } else { |
| 1244 | ds_data->layout_nodes[i] = layout_it->second; |
| 1245 | // Count total descriptors required per type |
| 1246 | for (uint32_t j = 0; j < layout_it->second->GetBindingCount(); ++j) { |
| 1247 | const auto &binding_layout = layout_it->second->GetDescriptorSetLayoutBindingPtrFromIndex(j); |
| 1248 | uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType); |
| 1249 | ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount; |
| 1250 | } |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | auto pool_it = pool_map.find(p_alloc_info->descriptorPool); |
| 1254 | if (pool_it == pool_map.end()) { |
| 1255 | skip_call |= |
| 1256 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT, |
| 1257 | reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool), __LINE__, DRAWSTATE_INVALID_POOL, "DS", |
| 1258 | "Unable to find pool node for pool 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call", |
| 1259 | reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool)); |
| 1260 | } else { // Make sure pool has all the available descriptors before calling down chain |
| 1261 | // Track number of descriptorSets allowable in this pool |
| 1262 | if (pool_it->second->availableSets < p_alloc_info->descriptorSetCount) { |
| 1263 | skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT, |
| 1264 | reinterpret_cast<uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, |
| 1265 | "DS", "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64 |
| 1266 | ". This pool only has %d descriptorSets remaining.", |
| 1267 | p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_it->second->pool), |
| 1268 | pool_it->second->availableSets); |
| 1269 | } |
| 1270 | // Determine whether descriptor counts are satisfiable |
| 1271 | for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) { |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1272 | if (ds_data->required_descriptors_by_type[i] > pool_it->second->availableDescriptorTypeCount[i]) { |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1273 | skip_call |= |
| 1274 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT, |
| 1275 | reinterpret_cast<const uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, |
| 1276 | "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64 |
| 1277 | ". This pool only has %d descriptors of this type remaining.", |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1278 | ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)), |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1279 | reinterpret_cast<uint64_t &>(pool_it->second->pool), pool_it->second->availableDescriptorTypeCount[i]); |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | return skip_call; |
| 1284 | } |
| 1285 | // Decrement allocated sets from the pool and insert new sets into set_map |
| 1286 | void cvdescriptorset::PerformAllocateDescriptorSets( |
| 1287 | const VkDescriptorSetAllocateInfo *p_alloc_info, const VkDescriptorSet *descriptor_sets, |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1288 | const AllocateDescriptorSetsData *ds_data, std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map, |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1289 | std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map, |
| 1290 | const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &layout_map, |
| 1291 | const std::unordered_map<VkBuffer, BUFFER_NODE> &buffer_map, |
| 1292 | const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> &mem_obj_map, |
| 1293 | const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> &buffer_view_map, |
| 1294 | const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> &sampler_map, |
| 1295 | const std::unordered_map<VkImageView, VkImageViewCreateInfo> &image_view_map, |
| 1296 | const std::unordered_map<VkImage, IMAGE_NODE> &image_map, |
| 1297 | const std::unordered_map<VkImage, VkSwapchainKHR> &image_to_swapchain_map, |
| 1298 | const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> &swapchain_map) { |
| 1299 | auto pool_state = (*pool_map)[p_alloc_info->descriptorPool]; |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1300 | /* Account for sets and individual descriptors allocated from pool */ |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1301 | pool_state->availableSets -= p_alloc_info->descriptorSetCount; |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1302 | for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) { |
| 1303 | pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i]; |
| 1304 | } |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1305 | /* Create tracking object for each descriptor set; insert into |
| 1306 | * global map and the pool's set. |
| 1307 | */ |
| 1308 | for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) { |
Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame^] | 1309 | auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], &buffer_map, &mem_obj_map, |
| 1310 | &buffer_view_map, &sampler_map, &image_view_map, &image_map, |
| 1311 | &image_to_swapchain_map, &swapchain_map); |
Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 1312 | |
| 1313 | pool_state->sets.insert(new_ds); |
| 1314 | new_ds->in_use.store(0); |
| 1315 | (*set_map)[descriptor_sets[i]] = new_ds; |
| 1316 | } |
| 1317 | } |