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