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