blob: 1d9eed64dbcddf7ba47f64209a10360d7f240015 [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 Ehlis9906d9d2016-05-17 14:23:46 -0600327cvdescriptorset::DescriptorSet::~DescriptorSet() {
328 InvalidateBoundCmdBuffers();
329 // Remove link to any cmd buffers
330 for (auto cb : bound_cmd_buffers_) {
331 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
332 cb->lastBound[i].uniqueBoundSets.erase(this);
333 }
334 }
335}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600336// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
337bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
338 return layout->IsCompatible(p_layout_, error);
339}
340// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
341// This includes validating that all descriptors in the given bindings are updated,
342// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
343// Return true if state is acceptable, or false and write an error message into error string
344bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
345 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
346 for (auto binding : bindings) {
347 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600348 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600349 // Nothing to do for strictly immutable sampler
350 } else {
351 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
352 auto dyn_offset_index = 0;
353 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600354 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600355 std::stringstream error_str;
356 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
357 << " is being used in draw but has not been updated.";
358 *error = error_str.str();
359 return false;
360 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600361 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600362 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600363 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600364 auto buffer_node = buffer_map_->find(buffer);
365 if (buffer_node == buffer_map_->end()) {
366 std::stringstream error_str;
367 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
368 << " references invalid buffer " << buffer << ".";
369 *error = error_str.str();
370 return false;
371 } else {
372 auto mem_entry = memory_map_->find(buffer_node->second.mem);
373 if (mem_entry == memory_map_->end()) {
374 std::stringstream error_str;
375 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
376 << " uses buffer " << buffer << " that references invalid memory "
377 << buffer_node->second.mem << ".";
378 *error = error_str.str();
379 return false;
380 }
381 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600382 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600383 // Validate that dynamic offsets are within the buffer
384 auto buffer_size = buffer_node->second.createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600385 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
386 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600387 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
388 if (VK_WHOLE_SIZE == range) {
389 if ((dyn_offset + desc_offset) > buffer_size) {
390 std::stringstream error_str;
391 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
392 << " uses buffer " << buffer
393 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
394 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
395 << buffer_size << ".";
396 *error = error_str.str();
397 return false;
398 }
399 } else {
400 if ((dyn_offset + desc_offset + range) > buffer_size) {
401 std::stringstream error_str;
402 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
403 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
404 << " combined with offset " << desc_offset << " and range " << range
405 << " that oversteps the buffer size of " << buffer_size << ".";
406 *error = error_str.str();
407 return false;
408 }
409 }
410 }
411 }
412 }
413 }
414 }
415 }
416 return true;
417}
418// For given bindings, place any update buffers or images into the passed-in unordered_sets
419uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
420 std::unordered_set<VkBuffer> *buffer_set,
421 std::unordered_set<VkImageView> *image_set) const {
422 auto num_updates = 0;
423 for (auto binding : bindings) {
424 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600425 if (descriptors_[start_idx]->IsStorage()) {
426 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600427 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600428 if (descriptors_[start_idx + i]->updated) {
429 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600430 num_updates++;
431 }
432 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600433 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600434 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600435 if (descriptors_[start_idx + i]->updated) {
436 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600437 const auto &buff_pair = buffer_view_map_->find(bufferview);
438 if (buff_pair != buffer_view_map_->end()) {
439 buffer_set->insert(buff_pair->second.buffer);
440 num_updates++;
441 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600442 }
443 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600444 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600445 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600446 if (descriptors_[start_idx + i]->updated) {
447 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600448 num_updates++;
449 }
450 }
451 }
452 }
453 }
454 return num_updates;
455}
456// This is a special case for compute shaders that should eventually be removed once we have proper valid binding info for compute
457// case
458uint32_t cvdescriptorset::DescriptorSet::GetAllStorageUpdates(std::unordered_set<VkBuffer> *buffer_set,
459 std::unordered_set<VkImageView> *image_set) const {
460 std::unordered_set<uint32_t> binding_set;
461 p_layout_->FillBindingSet(&binding_set);
462 return GetStorageUpdates(binding_set, buffer_set, image_set);
463}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600464// Set is being deleted or updates so invalidate all bound cmd buffers
465void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
466 for (auto cb_node : bound_cmd_buffers_) {
467 cb_node->state = CB_INVALID;
468 }
469}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600470// Perform write update in given update struct
471// If an error occurs, return false and fill in details in error_msg string
472bool cvdescriptorset::DescriptorSet::WriteUpdate(debug_report_data *report_data, const VkWriteDescriptorSet *update,
473 std::string *error_msg) {
474 auto num_updates = 0;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600475 // Verify idle ds
476 if (in_use.load()) {
477 std::stringstream error_str;
478 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
479 << " that is in use by a command buffer.";
480 *error_msg = error_str.str();
481 return false;
482 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600483 // Verify dst binding exists
484 if (!p_layout_->HasBinding(update->dstBinding)) {
485 std::stringstream error_str;
486 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
487 *error_msg = error_str.str();
488 return false;
489 } else {
490 // We know that binding is valid, verify update and do update on each descriptor
491 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
492 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
493 if (type != update->descriptorType) {
494 std::stringstream error_str;
495 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
496 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
497 *error_msg = error_str.str();
498 return false;
499 }
500 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
501 std::stringstream error_str;
502 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
503 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
504 << " descriptors starting at binding offset of "
505 << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
506 << " combined with update array element offset of " << update->dstArrayElement
507 << " oversteps the size of this descriptor set.";
508 *error_msg = error_str.str();
509 return false;
510 }
511 // Verify consecutive bindings match (if needed)
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600512 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount,
513 "write update to", set_, error_msg))
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600514 return false;
515 // Update is within bounds and consistent so perform update
516 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600517 // TODO : Can we break this into a set-level "Validate" followed by Descriptor updating itself
518 // if the validate passes? That saves us all the map ptrs in each descriptor instance
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600519 if (!descriptors_[start_idx + di]->WriteUpdate(update, di, error_msg)) {
520 std::stringstream error_str;
521 error_str << "Write update to descriptor at global index " << start_idx + di << " within set " << set_
522 << " binding #" << update->dstBinding << " failed with error message: " << error_msg->c_str();
523 *error_msg = error_str.str();
524 return false;
525 }
526 ++num_updates;
527 }
528 }
529 if (num_updates != 0) {
530 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600531 }
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600532 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600533 return true;
534}
535// Copy update
536bool cvdescriptorset::DescriptorSet::CopyUpdate(debug_report_data *report_data, const VkCopyDescriptorSet *update,
537 const DescriptorSet *src_set, std::string *error) {
538 auto num_updates = 0;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600539 // Verify idle ds
540 if (in_use.load()) {
541 std::stringstream error_str;
542 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
543 << " that is in use by a command buffer.";
544 *error = error_str.str();
545 return false;
546 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600547 if (!p_layout_->HasBinding(update->dstBinding)) {
548 std::stringstream error_str;
549 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
550 *error = error_str.str();
551 return false;
552 }
553 if (!src_set->HasBinding(update->srcBinding)) {
554 std::stringstream error_str;
555 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
556 *error = error_str.str();
557 return false;
558 }
559 // src & dst set bindings are valid
560 // Check bounds of src & dst
561 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
562 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
563 // SRC update out of bounds
564 std::stringstream error_str;
565 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
566 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
567 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
568 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
569 *error = error_str.str();
570 return false;
571 }
572 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
573 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
574 // DST update out of bounds
575 std::stringstream error_str;
576 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
577 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
578 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
579 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
580 *error = error_str.str();
581 return false;
582 }
583 // Check that types match
584 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
585 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
586 if (src_type != dst_type) {
587 std::stringstream error_str;
588 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
589 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
590 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
591 *error = error_str.str();
592 return false;
593 }
594 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600595 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
596 "copy update from", src_set->GetSet(), error)) ||
597 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
598 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600599 return false;
600 }
601 // Update parameters all look good so perform update
602 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600603 if (!descriptors_[dst_start_idx]->CopyUpdate(src_set->descriptors_[src_start_idx].get(), error))
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600604 return false;
605 ++num_updates;
606 }
607 if (num_updates != 0) {
608 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600609 }
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600610 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600611 return true;
612}
613cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
614 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
615 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
616 updated = false;
617 descriptor_class = PlainSampler;
618};
619
620cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
621 const VkSampler *immut, const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
622 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
623 updated = false;
624 descriptor_class = PlainSampler;
625 if (immut) {
626 sampler_ = *immut;
627 immutable_ = true;
628 updated = true;
629 }
630}
631bool cvdescriptorset::ValidateSampler(const VkSampler sampler,
632 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600633 return (sampler_map->count(sampler) != 0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600634}
635bool cvdescriptorset::ValidateImageUpdate(const VkImageView image_view, const VkImageLayout image_layout,
636 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
637 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
638 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
639 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
640 std::string *error) {
641 auto image_pair = image_view_map->find(image_view);
642 if (image_pair == image_view_map->end()) {
643 std::stringstream error_str;
644 error_str << "Invalid VkImageView: " << image_view;
645 *error = error_str.str();
646 return false;
647 } else {
648 // Validate that imageLayout is compatible with aspect_mask and image format
649 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
650 VkImage image = image_pair->second.image;
651 VkFormat format = VK_FORMAT_MAX_ENUM;
652 auto img_pair = image_map->find(image);
653 if (img_pair != image_map->end()) {
654 format = img_pair->second.createInfo.format;
655 } else {
656 // Also need to check the swapchains.
657 auto swapchain_pair = image_to_swapchain_map->find(image);
658 if (swapchain_pair != image_to_swapchain_map->end()) {
659 VkSwapchainKHR swapchain = swapchain_pair->second;
660 auto swapchain_pair = swapchain_map->find(swapchain);
661 if (swapchain_pair != swapchain_map->end()) {
662 format = swapchain_pair->second->createInfo.imageFormat;
663 }
664 }
665 }
666 if (format == VK_FORMAT_MAX_ENUM) {
667 std::stringstream error_str;
668 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
669 *error = error_str.str();
670 return false;
671 } else {
672 bool ds = vk_format_is_depth_or_stencil(format);
673 switch (image_layout) {
674 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
675 // Only Color bit must be set
676 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
677 std::stringstream error_str;
678 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
679 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
680 *error = error_str.str();
681 return false;
682 }
683 // format must NOT be DS
684 if (ds) {
685 std::stringstream error_str;
686 error_str << "ImageView (" << image_view
687 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
688 << string_VkFormat(format) << " which is not a color format.";
689 *error = error_str.str();
690 return false;
691 }
692 break;
693 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
694 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
695 // Depth or stencil bit must be set, but both must NOT be set
696 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
697 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
698 // both must NOT be set
699 std::stringstream error_str;
700 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
701 *error = error_str.str();
702 return false;
703 }
704 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
705 // Neither were set
706 std::stringstream error_str;
707 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
708 << " but does not have STENCIL or DEPTH aspects set";
709 *error = error_str.str();
710 return false;
711 }
712 // format must be DS
713 if (!ds) {
714 std::stringstream error_str;
715 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
716 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
717 *error = error_str.str();
718 return false;
719 }
720 break;
721 default:
722 // anything to check for other layouts?
723 break;
724 }
725 }
726 }
727 return true;
728}
729bool cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
730 if (!immutable_) {
731 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
732 std::stringstream error_str;
733 error_str << "Attempted write update to sampler descriptor with invalid sampler: " << update->pImageInfo[index].sampler
734 << ".";
735 *error = error_str.str();
736 return false;
737 }
738 sampler_ = update->pImageInfo[index].sampler;
739 } else {
740 if (!ValidateSampler(sampler_, sampler_map_)) {
741 std::stringstream error_str;
742 error_str << "Attempted write update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
743 << ".";
744 *error = error_str.str();
745 return false;
746 }
747 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
748 }
749 updated = true;
750 return true;
751}
752
753bool cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
754 if (!immutable_) {
755 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
756 if (!ValidateSampler(update_sampler, sampler_map_)) {
757 std::stringstream error_str;
758 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
759 *error = error_str.str();
760 return false;
761 }
762 sampler_ = update_sampler;
763 } else {
764 if (!ValidateSampler(sampler_, sampler_map_)) {
765 std::stringstream error_str;
766 error_str << "Attempted copy update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
767 << ".";
768 *error = error_str.str();
769 return false;
770 }
771 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
772 }
773 updated = true;
774 return true;
775}
776cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
777 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
778 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
779 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
780 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
781 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
782 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
783 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
784 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
785 updated = false;
786 descriptor_class = ImageSampler;
787}
788
789cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
790 const VkSampler *immut, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
791 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
792 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
793 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
794 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
795 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
796 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
797 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
798 updated = false;
799 descriptor_class = ImageSampler;
800 if (immut) {
801 sampler_ = *immut;
802 immutable_ = true;
803 updated = true;
804 }
805}
806bool cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index,
807 std::string *error) {
808 // First check sampler
809 if (!immutable_) {
810 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
811 std::stringstream error_str;
812 error_str << "Attempted write update to combined image sampler descriptor with invalid sampler: "
813 << update->pImageInfo[index].sampler << ".";
814 *error = error_str.str();
815 return false;
816 }
817 sampler_ = update->pImageInfo[index].sampler;
818 } else {
819 if (!ValidateSampler(sampler_, sampler_map_)) {
820 std::stringstream error_str;
821 error_str << "Attempted write update to combined image sampler descriptor with immutable sampler which has invalid "
822 "immutable sampler: "
823 << sampler_ << ".";
824 *error = error_str.str();
825 return false;
826 }
827 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
828 }
829 // Now validate images
830 auto image_view = update->pImageInfo[index].imageView;
831 auto image_layout = update->pImageInfo[index].imageLayout;
832 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
833 error)) {
834 std::stringstream error_str;
835 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
836 *error = error_str.str();
837 return false;
838 }
839 updated = true;
840 image_view_ = image_view;
841 image_layout_ = image_layout;
842 return true;
843}
844
845bool cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
846 if (!immutable_) {
847 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
848 if (!ValidateSampler(update_sampler, sampler_map_)) {
849 std::stringstream error_str;
850 error_str << "Attempted copy update to combined image sampler descriptor with invalid sampler: " << update_sampler
851 << ".";
852 *error = error_str.str();
853 return false;
854 }
855 sampler_ = update_sampler;
856 } else {
857 if (!ValidateSampler(sampler_, sampler_map_)) {
858 std::stringstream error_str;
859 error_str << "Attempted copy update to combined image sampler descriptor with immutable sampler that has invalid "
860 "immutable sampler: "
861 << sampler_ << ".";
862 *error = error_str.str();
863 return false;
864 }
865 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
866 }
867 // Now validate images
868 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
869 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
870 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
871 error)) {
872 std::stringstream error_str;
873 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
874 *error = error_str.str();
875 return false;
876 }
877 updated = true;
878 image_view_ = image_view;
879 image_layout_ = image_layout;
880 return true;
881}
882
883cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type,
884 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
885 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
886 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
887 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
888 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED), image_view_map_(image_view_map),
889 image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
890 updated = false;
891 descriptor_class = Image;
892 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
893 storage_ = true;
894};
895
896bool cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
897 // First validate that the write update is valid
898 auto image_view = update->pImageInfo[index].imageView;
899 auto image_layout = update->pImageInfo[index].imageLayout;
900 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
901 error)) {
902 std::stringstream error_str;
903 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
904 *error = error_str.str();
905 return false;
906 }
907 // Update is clean so process it
908 updated = true;
909 image_view_ = image_view;
910 image_layout_ = image_layout;
911 return true;
912}
913
914bool cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
915 // First validate update
916 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
917 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
918 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
919 error)) {
920 std::stringstream error_str;
921 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
922 *error = error_str.str();
923 return false;
924 }
925 updated = true;
926 image_view_ = image_view;
927 image_layout_ = image_layout;
928 return true;
929}
930
931cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type,
932 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map)
933 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0), buffer_map_(buffer_map) {
934 updated = false;
935 descriptor_class = GeneralBuffer;
936 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
937 dynamic_ = true;
938 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
939 storage_ = true;
940 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
941 dynamic_ = true;
942 storage_ = true;
943 }
944}
945bool cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
946 // First validate bufferinfo
947 auto buffer = update->pBufferInfo[index].buffer;
948 if (!buffer_map_->count(buffer)) {
949 std::stringstream error_str;
950 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
951 *error = error_str.str();
952 return false;
953 }
954 updated = true;
955 buffer_ = buffer;
956 offset_ = update->pBufferInfo[index].offset;
957 range_ = update->pBufferInfo[index].range;
958 return true;
959}
960
961bool cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
962 // First validate bufferinfo
963 auto buffer = static_cast<const BufferDescriptor *>(src)->buffer_;
964 if (!buffer_map_->count(buffer)) {
965 std::stringstream error_str;
966 error_str << "Attempted copy update to buffer descriptor with invalid buffer: " << buffer;
967 *error = error_str.str();
968 return false;
969 }
970 updated = true;
971 buffer_ = buffer;
972 offset_ = static_cast<const BufferDescriptor *>(src)->offset_;
973 range_ = static_cast<const BufferDescriptor *>(src)->range_;
974 return true;
975}
976
977cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type,
978 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map)
979 : buffer_view_(VK_NULL_HANDLE), storage_(false), buffer_view_map_(buffer_view_map) {
980 updated = false;
981 descriptor_class = TexelBuffer;
982 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
983 storage_ = true;
984};
985bool cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
986 // First validate buffer view
987 auto buffer_view = update->pTexelBufferView[index];
988 if (!buffer_view_map_->count(buffer_view)) {
989 std::stringstream error_str;
990 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
991 *error = error_str.str();
992 return false;
993 }
994 updated = true;
995 buffer_view_ = buffer_view;
996 return true;
997}
998
999bool cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
1000 // First validate buffer view
1001 auto buffer_view = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1002 if (!buffer_view_map_->count(buffer_view)) {
1003 std::stringstream error_str;
1004 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1005 *error = error_str.str();
1006 return false;
1007 }
1008 updated = true;
1009 buffer_view_ = buffer_view;
1010 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001011}