blob: 437dcdc3643e56d6ba68012cd00d3493c2e04c4e [file] [log] [blame]
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Tobin Ehlis <tobine@google.com>
19 */
20
21#include "descriptor_sets.h"
22#include "vk_enum_string_helper.h"
23#include "vk_safe_struct.h"
24#include <sstream>
25
26// Construct DescriptorSetLayout instance from given create info
27cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(debug_report_data *report_data,
28 const VkDescriptorSetLayoutCreateInfo *p_create_info,
29 const VkDescriptorSetLayout layout)
30 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
31 uint32_t global_index = 0;
32 for (uint32_t i = 0; i < binding_count_; ++i) {
33 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
34 if (!binding_to_index_map_.emplace(p_create_info->pBindings[i].binding, i).second) {
35 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
36 reinterpret_cast<uint64_t &>(layout_), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
37 "duplicated binding number in "
38 "VkDescriptorSetLayoutBinding");
39 }
40 binding_to_global_start_index_map_[p_create_info->pBindings[i].binding] = global_index;
41 global_index += p_create_info->pBindings[i].descriptorCount ? p_create_info->pBindings[i].descriptorCount - 1 : 0;
42 binding_to_global_end_index_map_[p_create_info->pBindings[i].binding] = global_index;
43 global_index++;
Tobin Ehlis664e6012016-05-05 11:04:44 -060044 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060045 // In cases where we should ignore pImmutableSamplers make sure it's NULL
46 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
47 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
48 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060049 bindings_.back().pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050 }
51 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
52 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
53 dynamic_descriptor_count_++;
54 }
55 }
56}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060057// put all bindings into the given set
58void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
59 for (auto binding_index_pair : binding_to_index_map_)
60 binding_set->insert(binding_index_pair.first);
61}
62VkDescriptorSetLayoutBinding const *
63cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060064 const auto &bi_itr = binding_to_index_map_.find(binding);
65 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060066 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -060067 }
68 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060069}
70VkDescriptorSetLayoutBinding const *
71cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
72 if (index >= bindings_.size())
73 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -060074 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060075}
76// Return descriptorCount for given binding, 0 if index is unavailable
77uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060078 const auto &bi_itr = binding_to_index_map_.find(binding);
79 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060080 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060081 }
82 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060083}
84// Return descriptorCount for given index, 0 if index is unavailable
85uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
86 if (index >= bindings_.size())
87 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -060088 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060089}
90// For the given binding, return descriptorType
91VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
92 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -060093 const auto &bi_itr = binding_to_index_map_.find(binding);
94 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060095 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060096 }
97 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060098}
99// For the given index, return descriptorType
100VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
101 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600102 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600103}
104// For the given global index, return descriptorType
105// Currently just counting up through bindings_, may improve this in future
106VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
107 uint32_t global_offset = 0;
108 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600109 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600110 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600111 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600112 }
113 assert(0); // requested global index is out of bounds
114 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
115}
116// For the given binding, return stageFlags
117VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
118 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600119 const auto &bi_itr = binding_to_index_map_.find(binding);
120 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600121 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600122 }
123 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600124}
125// For the given binding, return start index
126uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
127 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600128 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
129 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
130 return btgsi_itr->second;
131 }
132 // In error case max uint32_t so index is out of bounds to break ASAP
133 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600134}
135// For the given binding, return end index
136uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
137 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600138 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
139 if (btgei_itr != binding_to_global_end_index_map_.end()) {
140 return btgei_itr->second;
141 }
142 // In error case max uint32_t so index is out of bounds to break ASAP
143 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600144}
145// For given binding, return ptr to ImmutableSampler array
146VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
147 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600148 const auto &bi_itr = binding_to_index_map_.find(binding);
149 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600150 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600151 }
152 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600153}
154// For given index, return ptr to ImmutableSampler array
155VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
156 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600157 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600158}
159// If our layout is compatible with rh_ds_layout, return true,
160// else return false and fill in error_msg will description of what causes incompatibility
161bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
162 // Trivial case
163 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
164 return true;
165 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
166 std::stringstream error_str;
167 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
168 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
169 *error_msg = error_str.str();
170 return false; // trivial fail case
171 }
172 // Descriptor counts match so need to go through bindings one-by-one
173 // and verify that type and stageFlags match
174 for (auto binding : bindings_) {
175 // TODO : Do we also need to check immutable samplers?
176 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600177 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600178 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600179 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
180 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600181 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600182 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600183 *error_msg = error_str.str();
184 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600185 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600186 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600187 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
188 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600189 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600190 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600191 *error_msg = error_str.str();
192 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600193 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600194 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600195 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
196 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600197 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600198 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600199 *error_msg = error_str.str();
200 return false;
201 }
202 }
203 return true;
204}
205
206bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
207 if (!binding_to_index_map_.count(binding + 1))
208 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600209 auto const &bi_itr = binding_to_index_map_.find(binding);
210 if (bi_itr != binding_to_index_map_.end()) {
211 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
212 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600213 auto type = bindings_[bi_itr->second].descriptorType;
214 auto stage_flags = bindings_[bi_itr->second].stageFlags;
215 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
216 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
217 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
218 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600219 return false;
220 }
221 return true;
222 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600223 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600224 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600225}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600226// Starting at offset descriptor of given binding, parse over update_count
227// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
228// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
229// If so, return true. If not, fill in error_msg and return false
230bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
231 const char *type, const VkDescriptorSet set,
232 std::string *error_msg) const {
233 // Verify consecutive bindings match (if needed)
234 auto orig_binding = current_binding;
235 // Track count of descriptors in the current_bindings that are remaining to be updated
236 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
237 // First, it's legal to offset beyond your own binding so handle that case
238 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
239 while (offset >= binding_remaining) {
240 // Advance to next binding, decrement offset by binding size
241 offset -= binding_remaining;
242 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
243 }
244 binding_remaining -= offset;
245 while (update_count > binding_remaining) { // While our updates overstep current binding
246 // Verify next consecutive binding matches type, stage flags & immutable sampler use
247 if (!IsNextBindingConsistent(current_binding++)) {
248 std::stringstream error_str;
249 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
250 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
251 "next binding is not consistent with current binding so this update is invalid.";
252 *error_msg = error_str.str();
253 return false;
254 }
255 // For sake of this check consider the bindings updated and grab count for next binding
256 update_count -= binding_remaining;
257 binding_remaining = GetDescriptorCountFromBinding(current_binding);
258 }
259 return true;
260}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600261
262cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout,
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600263 const debug_report_data *debug_report_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600264 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map,
265 const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *memory_map,
266 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map,
267 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map,
268 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
269 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
270 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
271 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600272 : some_update_(false), set_(set), p_layout_(layout), report_data_(debug_report_data), buffer_map_(buffer_map),
273 memory_map_(memory_map), buffer_view_map_(buffer_view_map), sampler_map_(sampler_map), image_view_map_(image_view_map),
274 image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600275 // Foreach binding, create default descriptors of given type
276 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
277 auto type = p_layout_->GetTypeFromIndex(i);
278 switch (type) {
279 case VK_DESCRIPTOR_TYPE_SAMPLER: {
280 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
281 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
282 if (immut_sampler)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600283 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor(immut_sampler + di, sampler_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600284 else
Tobin Ehlis81f17852016-05-05 09:04:33 -0600285 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor(sampler_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600286 }
287 break;
288 }
289 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
290 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
291 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
292 if (immut)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600293 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor(
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600294 immut + di, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, sampler_map_)));
295 else
Tobin Ehlis81f17852016-05-05 09:04:33 -0600296 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor(
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600297 image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, sampler_map_)));
298 }
299 break;
300 }
301 // ImageDescriptors
302 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
303 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
304 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
305 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600306 descriptors_.emplace_back(std::unique_ptr<Descriptor>(
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600307 new ImageDescriptor(type, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_)));
308 break;
309 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
310 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
311 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600312 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new TexelDescriptor(type, buffer_view_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600313 break;
314 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
315 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
316 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
317 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
318 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600319 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new BufferDescriptor(type, buffer_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600320 break;
321 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600322 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600323 break;
324 }
325 }
326}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600327// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
328bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
329 return layout->IsCompatible(p_layout_, error);
330}
331// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
332// This includes validating that all descriptors in the given bindings are updated,
333// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
334// Return true if state is acceptable, or false and write an error message into error string
335bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
336 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
337 for (auto binding : bindings) {
338 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600339 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600340 // Nothing to do for strictly immutable sampler
341 } else {
342 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
343 auto dyn_offset_index = 0;
344 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600345 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600346 std::stringstream error_str;
347 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
348 << " is being used in draw but has not been updated.";
349 *error = error_str.str();
350 return false;
351 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600352 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600353 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600354 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600355 auto buffer_node = buffer_map_->find(buffer);
356 if (buffer_node == buffer_map_->end()) {
357 std::stringstream error_str;
358 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
359 << " references invalid buffer " << buffer << ".";
360 *error = error_str.str();
361 return false;
362 } else {
363 auto mem_entry = memory_map_->find(buffer_node->second.mem);
364 if (mem_entry == memory_map_->end()) {
365 std::stringstream error_str;
366 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
367 << " uses buffer " << buffer << " that references invalid memory "
368 << buffer_node->second.mem << ".";
369 *error = error_str.str();
370 return false;
371 }
372 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600373 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600374 // Validate that dynamic offsets are within the buffer
375 auto buffer_size = buffer_node->second.createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600376 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
377 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600378 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
379 if (VK_WHOLE_SIZE == range) {
380 if ((dyn_offset + desc_offset) > buffer_size) {
381 std::stringstream error_str;
382 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
383 << " uses buffer " << buffer
384 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
385 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
386 << buffer_size << ".";
387 *error = error_str.str();
388 return false;
389 }
390 } else {
391 if ((dyn_offset + desc_offset + range) > buffer_size) {
392 std::stringstream error_str;
393 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
394 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
395 << " combined with offset " << desc_offset << " and range " << range
396 << " that oversteps the buffer size of " << buffer_size << ".";
397 *error = error_str.str();
398 return false;
399 }
400 }
401 }
402 }
403 }
404 }
405 }
406 }
407 return true;
408}
409// For given bindings, place any update buffers or images into the passed-in unordered_sets
410uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
411 std::unordered_set<VkBuffer> *buffer_set,
412 std::unordered_set<VkImageView> *image_set) const {
413 auto num_updates = 0;
414 for (auto binding : bindings) {
415 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600416 if (descriptors_[start_idx]->IsStorage()) {
417 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600418 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600419 if (descriptors_[start_idx + i]->updated) {
420 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600421 num_updates++;
422 }
423 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600424 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600425 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600426 if (descriptors_[start_idx + i]->updated) {
427 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600428 const auto &buff_pair = buffer_view_map_->find(bufferview);
429 if (buff_pair != buffer_view_map_->end()) {
430 buffer_set->insert(buff_pair->second.buffer);
431 num_updates++;
432 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600433 }
434 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600435 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600436 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600437 if (descriptors_[start_idx + i]->updated) {
438 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600439 num_updates++;
440 }
441 }
442 }
443 }
444 }
445 return num_updates;
446}
447// This is a special case for compute shaders that should eventually be removed once we have proper valid binding info for compute
448// case
449uint32_t cvdescriptorset::DescriptorSet::GetAllStorageUpdates(std::unordered_set<VkBuffer> *buffer_set,
450 std::unordered_set<VkImageView> *image_set) const {
451 std::unordered_set<uint32_t> binding_set;
452 p_layout_->FillBindingSet(&binding_set);
453 return GetStorageUpdates(binding_set, buffer_set, image_set);
454}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600455// Perform write update in given update struct
456// If an error occurs, return false and fill in details in error_msg string
457bool cvdescriptorset::DescriptorSet::WriteUpdate(debug_report_data *report_data, const VkWriteDescriptorSet *update,
458 std::string *error_msg) {
459 auto num_updates = 0;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600460 // Verify idle ds
461 if (in_use.load()) {
462 std::stringstream error_str;
463 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
464 << " that is in use by a command buffer.";
465 *error_msg = error_str.str();
466 return false;
467 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600468 // Verify dst binding exists
469 if (!p_layout_->HasBinding(update->dstBinding)) {
470 std::stringstream error_str;
471 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
472 *error_msg = error_str.str();
473 return false;
474 } else {
475 // We know that binding is valid, verify update and do update on each descriptor
476 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
477 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
478 if (type != update->descriptorType) {
479 std::stringstream error_str;
480 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
481 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
482 *error_msg = error_str.str();
483 return false;
484 }
485 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
486 std::stringstream error_str;
487 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
488 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
489 << " descriptors starting at binding offset of "
490 << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
491 << " combined with update array element offset of " << update->dstArrayElement
492 << " oversteps the size of this descriptor set.";
493 *error_msg = error_str.str();
494 return false;
495 }
496 // Verify consecutive bindings match (if needed)
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600497 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount,
498 "write update to", set_, error_msg))
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600499 return false;
500 // Update is within bounds and consistent so perform update
501 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600502 // TODO : Can we break this into a set-level "Validate" followed by Descriptor updating itself
503 // if the validate passes? That saves us all the map ptrs in each descriptor instance
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600504 if (!descriptors_[start_idx + di]->WriteUpdate(update, di, error_msg)) {
505 std::stringstream error_str;
506 error_str << "Write update to descriptor at global index " << start_idx + di << " within set " << set_
507 << " binding #" << update->dstBinding << " failed with error message: " << error_msg->c_str();
508 *error_msg = error_str.str();
509 return false;
510 }
511 ++num_updates;
512 }
513 }
514 if (num_updates != 0) {
515 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600516 }
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600517 // Invalidate any bound command buffers
518 for (auto cb_node : bound_cmd_buffers_) {
519 cb_node->state = CB_INVALID;
520 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600521 return true;
522}
523// Copy update
524bool cvdescriptorset::DescriptorSet::CopyUpdate(debug_report_data *report_data, const VkCopyDescriptorSet *update,
525 const DescriptorSet *src_set, std::string *error) {
526 auto num_updates = 0;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600527 // Verify idle ds
528 if (in_use.load()) {
529 std::stringstream error_str;
530 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
531 << " that is in use by a command buffer.";
532 *error = error_str.str();
533 return false;
534 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600535 if (!p_layout_->HasBinding(update->dstBinding)) {
536 std::stringstream error_str;
537 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
538 *error = error_str.str();
539 return false;
540 }
541 if (!src_set->HasBinding(update->srcBinding)) {
542 std::stringstream error_str;
543 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
544 *error = error_str.str();
545 return false;
546 }
547 // src & dst set bindings are valid
548 // Check bounds of src & dst
549 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
550 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
551 // SRC update out of bounds
552 std::stringstream error_str;
553 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
554 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
555 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
556 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
557 *error = error_str.str();
558 return false;
559 }
560 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
561 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
562 // DST update out of bounds
563 std::stringstream error_str;
564 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
565 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
566 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
567 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
568 *error = error_str.str();
569 return false;
570 }
571 // Check that types match
572 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
573 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
574 if (src_type != dst_type) {
575 std::stringstream error_str;
576 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
577 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
578 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
579 *error = error_str.str();
580 return false;
581 }
582 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600583 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
584 "copy update from", src_set->GetSet(), error)) ||
585 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
586 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600587 return false;
588 }
589 // Update parameters all look good so perform update
590 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600591 if (!descriptors_[dst_start_idx]->CopyUpdate(src_set->descriptors_[src_start_idx].get(), error))
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600592 return false;
593 ++num_updates;
594 }
595 if (num_updates != 0) {
596 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600597 }
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600598 // Invalidate any bound command buffers
599 for (auto cb_node : bound_cmd_buffers_) {
600 cb_node->state = CB_INVALID;
601 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600602 return true;
603}
604cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
605 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
606 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
607 updated = false;
608 descriptor_class = PlainSampler;
609};
610
611cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
612 const VkSampler *immut, const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
613 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
614 updated = false;
615 descriptor_class = PlainSampler;
616 if (immut) {
617 sampler_ = *immut;
618 immutable_ = true;
619 updated = true;
620 }
621}
622bool cvdescriptorset::ValidateSampler(const VkSampler sampler,
623 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600624 return (sampler_map->count(sampler) != 0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600625}
626bool cvdescriptorset::ValidateImageUpdate(const VkImageView image_view, const VkImageLayout image_layout,
627 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
628 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
629 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
630 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
631 std::string *error) {
632 auto image_pair = image_view_map->find(image_view);
633 if (image_pair == image_view_map->end()) {
634 std::stringstream error_str;
635 error_str << "Invalid VkImageView: " << image_view;
636 *error = error_str.str();
637 return false;
638 } else {
639 // Validate that imageLayout is compatible with aspect_mask and image format
640 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
641 VkImage image = image_pair->second.image;
642 VkFormat format = VK_FORMAT_MAX_ENUM;
643 auto img_pair = image_map->find(image);
644 if (img_pair != image_map->end()) {
645 format = img_pair->second.createInfo.format;
646 } else {
647 // Also need to check the swapchains.
648 auto swapchain_pair = image_to_swapchain_map->find(image);
649 if (swapchain_pair != image_to_swapchain_map->end()) {
650 VkSwapchainKHR swapchain = swapchain_pair->second;
651 auto swapchain_pair = swapchain_map->find(swapchain);
652 if (swapchain_pair != swapchain_map->end()) {
653 format = swapchain_pair->second->createInfo.imageFormat;
654 }
655 }
656 }
657 if (format == VK_FORMAT_MAX_ENUM) {
658 std::stringstream error_str;
659 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
660 *error = error_str.str();
661 return false;
662 } else {
663 bool ds = vk_format_is_depth_or_stencil(format);
664 switch (image_layout) {
665 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
666 // Only Color bit must be set
667 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
668 std::stringstream error_str;
669 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
670 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
671 *error = error_str.str();
672 return false;
673 }
674 // format must NOT be DS
675 if (ds) {
676 std::stringstream error_str;
677 error_str << "ImageView (" << image_view
678 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
679 << string_VkFormat(format) << " which is not a color format.";
680 *error = error_str.str();
681 return false;
682 }
683 break;
684 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
685 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
686 // Depth or stencil bit must be set, but both must NOT be set
687 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
688 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
689 // both must NOT be set
690 std::stringstream error_str;
691 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
692 *error = error_str.str();
693 return false;
694 }
695 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
696 // Neither were set
697 std::stringstream error_str;
698 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
699 << " but does not have STENCIL or DEPTH aspects set";
700 *error = error_str.str();
701 return false;
702 }
703 // format must be DS
704 if (!ds) {
705 std::stringstream error_str;
706 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
707 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
708 *error = error_str.str();
709 return false;
710 }
711 break;
712 default:
713 // anything to check for other layouts?
714 break;
715 }
716 }
717 }
718 return true;
719}
720bool cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
721 if (!immutable_) {
722 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
723 std::stringstream error_str;
724 error_str << "Attempted write update to sampler descriptor with invalid sampler: " << update->pImageInfo[index].sampler
725 << ".";
726 *error = error_str.str();
727 return false;
728 }
729 sampler_ = update->pImageInfo[index].sampler;
730 } else {
731 if (!ValidateSampler(sampler_, sampler_map_)) {
732 std::stringstream error_str;
733 error_str << "Attempted write update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
734 << ".";
735 *error = error_str.str();
736 return false;
737 }
738 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
739 }
740 updated = true;
741 return true;
742}
743
744bool cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
745 if (!immutable_) {
746 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
747 if (!ValidateSampler(update_sampler, sampler_map_)) {
748 std::stringstream error_str;
749 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
750 *error = error_str.str();
751 return false;
752 }
753 sampler_ = update_sampler;
754 } else {
755 if (!ValidateSampler(sampler_, sampler_map_)) {
756 std::stringstream error_str;
757 error_str << "Attempted copy update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
758 << ".";
759 *error = error_str.str();
760 return false;
761 }
762 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
763 }
764 updated = true;
765 return true;
766}
767cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
768 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
769 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
770 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
771 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
772 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
773 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
774 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
775 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
776 updated = false;
777 descriptor_class = ImageSampler;
778}
779
780cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
781 const VkSampler *immut, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
782 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
783 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
784 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
785 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
786 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
787 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
788 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
789 updated = false;
790 descriptor_class = ImageSampler;
791 if (immut) {
792 sampler_ = *immut;
793 immutable_ = true;
794 updated = true;
795 }
796}
797bool cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index,
798 std::string *error) {
799 // First check sampler
800 if (!immutable_) {
801 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
802 std::stringstream error_str;
803 error_str << "Attempted write update to combined image sampler descriptor with invalid sampler: "
804 << update->pImageInfo[index].sampler << ".";
805 *error = error_str.str();
806 return false;
807 }
808 sampler_ = update->pImageInfo[index].sampler;
809 } else {
810 if (!ValidateSampler(sampler_, sampler_map_)) {
811 std::stringstream error_str;
812 error_str << "Attempted write update to combined image sampler descriptor with immutable sampler which has invalid "
813 "immutable sampler: "
814 << sampler_ << ".";
815 *error = error_str.str();
816 return false;
817 }
818 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
819 }
820 // Now validate images
821 auto image_view = update->pImageInfo[index].imageView;
822 auto image_layout = update->pImageInfo[index].imageLayout;
823 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
824 error)) {
825 std::stringstream error_str;
826 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
827 *error = error_str.str();
828 return false;
829 }
830 updated = true;
831 image_view_ = image_view;
832 image_layout_ = image_layout;
833 return true;
834}
835
836bool cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
837 if (!immutable_) {
838 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
839 if (!ValidateSampler(update_sampler, sampler_map_)) {
840 std::stringstream error_str;
841 error_str << "Attempted copy update to combined image sampler descriptor with invalid sampler: " << update_sampler
842 << ".";
843 *error = error_str.str();
844 return false;
845 }
846 sampler_ = update_sampler;
847 } else {
848 if (!ValidateSampler(sampler_, sampler_map_)) {
849 std::stringstream error_str;
850 error_str << "Attempted copy update to combined image sampler descriptor with immutable sampler that has invalid "
851 "immutable sampler: "
852 << sampler_ << ".";
853 *error = error_str.str();
854 return false;
855 }
856 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
857 }
858 // Now validate images
859 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
860 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
861 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
862 error)) {
863 std::stringstream error_str;
864 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
865 *error = error_str.str();
866 return false;
867 }
868 updated = true;
869 image_view_ = image_view;
870 image_layout_ = image_layout;
871 return true;
872}
873
874cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type,
875 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
876 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
877 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
878 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
879 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED), image_view_map_(image_view_map),
880 image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
881 updated = false;
882 descriptor_class = Image;
883 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
884 storage_ = true;
885};
886
887bool cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
888 // First validate that the write update is valid
889 auto image_view = update->pImageInfo[index].imageView;
890 auto image_layout = update->pImageInfo[index].imageLayout;
891 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
892 error)) {
893 std::stringstream error_str;
894 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
895 *error = error_str.str();
896 return false;
897 }
898 // Update is clean so process it
899 updated = true;
900 image_view_ = image_view;
901 image_layout_ = image_layout;
902 return true;
903}
904
905bool cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
906 // First validate update
907 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
908 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
909 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
910 error)) {
911 std::stringstream error_str;
912 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
913 *error = error_str.str();
914 return false;
915 }
916 updated = true;
917 image_view_ = image_view;
918 image_layout_ = image_layout;
919 return true;
920}
921
922cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type,
923 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map)
924 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0), buffer_map_(buffer_map) {
925 updated = false;
926 descriptor_class = GeneralBuffer;
927 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
928 dynamic_ = true;
929 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
930 storage_ = true;
931 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
932 dynamic_ = true;
933 storage_ = true;
934 }
935}
936bool cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
937 // First validate bufferinfo
938 auto buffer = update->pBufferInfo[index].buffer;
939 if (!buffer_map_->count(buffer)) {
940 std::stringstream error_str;
941 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
942 *error = error_str.str();
943 return false;
944 }
945 updated = true;
946 buffer_ = buffer;
947 offset_ = update->pBufferInfo[index].offset;
948 range_ = update->pBufferInfo[index].range;
949 return true;
950}
951
952bool cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
953 // First validate bufferinfo
954 auto buffer = static_cast<const BufferDescriptor *>(src)->buffer_;
955 if (!buffer_map_->count(buffer)) {
956 std::stringstream error_str;
957 error_str << "Attempted copy update to buffer descriptor with invalid buffer: " << buffer;
958 *error = error_str.str();
959 return false;
960 }
961 updated = true;
962 buffer_ = buffer;
963 offset_ = static_cast<const BufferDescriptor *>(src)->offset_;
964 range_ = static_cast<const BufferDescriptor *>(src)->range_;
965 return true;
966}
967
968cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type,
969 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map)
970 : buffer_view_(VK_NULL_HANDLE), storage_(false), buffer_view_map_(buffer_view_map) {
971 updated = false;
972 descriptor_class = TexelBuffer;
973 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
974 storage_ = true;
975};
976bool cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
977 // First validate buffer view
978 auto buffer_view = update->pTexelBufferView[index];
979 if (!buffer_view_map_->count(buffer_view)) {
980 std::stringstream error_str;
981 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
982 *error = error_str.str();
983 return false;
984 }
985 updated = true;
986 buffer_view_ = buffer_view;
987 return true;
988}
989
990bool cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
991 // First validate buffer view
992 auto buffer_view = static_cast<const TexelDescriptor *>(src)->buffer_view_;
993 if (!buffer_view_map_->count(buffer_view)) {
994 std::stringstream error_str;
995 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
996 *error = error_str.str();
997 return false;
998 }
999 updated = true;
1000 buffer_view_ = buffer_view;
1001 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001002}