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