blob: bc55e6db7b10cf34b34fca84c2fabf71464e1c4b [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}
296// For given global_index return bool of whether or not the underlying descriptor has been updated
297bool cvdescriptorset::DescriptorSet::IsUpdated(const uint32_t global_index) const {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600298 if (global_index >= descriptors_.size())
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600299 return false;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600300 return descriptors_[global_index]->updated;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600301}
302// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
303bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
304 return layout->IsCompatible(p_layout_, error);
305}
306// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
307// This includes validating that all descriptors in the given bindings are updated,
308// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
309// Return true if state is acceptable, or false and write an error message into error string
310bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
311 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
312 for (auto binding : bindings) {
313 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600314 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600315 // Nothing to do for strictly immutable sampler
316 } else {
317 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
318 auto dyn_offset_index = 0;
319 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600320 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600321 std::stringstream error_str;
322 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
323 << " is being used in draw but has not been updated.";
324 *error = error_str.str();
325 return false;
326 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600327 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600328 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600329 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600330 auto buffer_node = buffer_map_->find(buffer);
331 if (buffer_node == buffer_map_->end()) {
332 std::stringstream error_str;
333 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
334 << " references invalid buffer " << buffer << ".";
335 *error = error_str.str();
336 return false;
337 } else {
338 auto mem_entry = memory_map_->find(buffer_node->second.mem);
339 if (mem_entry == memory_map_->end()) {
340 std::stringstream error_str;
341 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
342 << " uses buffer " << buffer << " that references invalid memory "
343 << buffer_node->second.mem << ".";
344 *error = error_str.str();
345 return false;
346 }
347 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600348 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600349 // Validate that dynamic offsets are within the buffer
350 auto buffer_size = buffer_node->second.createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600351 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
352 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600353 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
354 if (VK_WHOLE_SIZE == range) {
355 if ((dyn_offset + desc_offset) > buffer_size) {
356 std::stringstream error_str;
357 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
358 << " uses buffer " << buffer
359 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
360 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
361 << buffer_size << ".";
362 *error = error_str.str();
363 return false;
364 }
365 } else {
366 if ((dyn_offset + desc_offset + range) > buffer_size) {
367 std::stringstream error_str;
368 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
369 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
370 << " combined with offset " << desc_offset << " and range " << range
371 << " that oversteps the buffer size of " << buffer_size << ".";
372 *error = error_str.str();
373 return false;
374 }
375 }
376 }
377 }
378 }
379 }
380 }
381 }
382 return true;
383}
384// For given bindings, place any update buffers or images into the passed-in unordered_sets
385uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
386 std::unordered_set<VkBuffer> *buffer_set,
387 std::unordered_set<VkImageView> *image_set) const {
388 auto num_updates = 0;
389 for (auto binding : bindings) {
390 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600391 if (descriptors_[start_idx]->IsStorage()) {
392 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600393 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600394 if (descriptors_[start_idx + i]->updated) {
395 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600396 num_updates++;
397 }
398 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600399 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600400 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600401 if (descriptors_[start_idx + i]->updated) {
402 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600403 const auto &buff_pair = buffer_view_map_->find(bufferview);
404 if (buff_pair != buffer_view_map_->end()) {
405 buffer_set->insert(buff_pair->second.buffer);
406 num_updates++;
407 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600408 }
409 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600410 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600411 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600412 if (descriptors_[start_idx + i]->updated) {
413 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600414 num_updates++;
415 }
416 }
417 }
418 }
419 }
420 return num_updates;
421}
422// This is a special case for compute shaders that should eventually be removed once we have proper valid binding info for compute
423// case
424uint32_t cvdescriptorset::DescriptorSet::GetAllStorageUpdates(std::unordered_set<VkBuffer> *buffer_set,
425 std::unordered_set<VkImageView> *image_set) const {
426 std::unordered_set<uint32_t> binding_set;
427 p_layout_->FillBindingSet(&binding_set);
428 return GetStorageUpdates(binding_set, buffer_set, image_set);
429}
430// Starting at given current_binding with binding_remaining descriptors, parse over update_count
431// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
432// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
433// If so, return true. If not, fill in error_msg and return false
434bool cvdescriptorset::DescriptorSet::VerifyUpdateConsistency(uint32_t current_binding, uint32_t binding_remaining,
435 uint32_t update_count, const char *type,
436 std::string *error_msg) const {
437 // Verify consecutive bindings match (if needed)
438 auto orig_binding = current_binding;
439 while (update_count > binding_remaining) { // While our updates overstep current binding
440 // Verify next consecutive binding matches type, stage flags & immutable sampler use
441 if (!p_layout_->IsNextBindingConsistent(current_binding++)) {
442 std::stringstream error_str;
443 error_str << "Attempting " << type << " descriptor set " << set_ << " binding #" << orig_binding << " with #"
444 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and then "
445 "next binding is not consistent with current binding so this update is invalid.";
446 *error_msg = error_str.str();
447 return false;
448 }
449 // For sake of this check consider the bindings updated and advance to next binding
450 update_count -= binding_remaining;
451 binding_remaining = p_layout_->GetDescriptorCountFromBinding(current_binding);
452 }
453 return true;
454}
455// Perform write update in given update struct
456// If an error occurs, return false and fill in details in error_msg string
457bool cvdescriptorset::DescriptorSet::WriteUpdate(debug_report_data *report_data, const VkWriteDescriptorSet *update,
458 std::string *error_msg) {
459 auto num_updates = 0;
460 // Verify dst binding exists
461 if (!p_layout_->HasBinding(update->dstBinding)) {
462 std::stringstream error_str;
463 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
464 *error_msg = error_str.str();
465 return false;
466 } else {
467 // We know that binding is valid, verify update and do update on each descriptor
468 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
469 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
470 if (type != update->descriptorType) {
471 std::stringstream error_str;
472 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
473 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
474 *error_msg = error_str.str();
475 return false;
476 }
477 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
478 std::stringstream error_str;
479 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
480 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
481 << " descriptors starting at binding offset of "
482 << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
483 << " combined with update array element offset of " << update->dstArrayElement
484 << " oversteps the size of this descriptor set.";
485 *error_msg = error_str.str();
486 return false;
487 }
488 // Verify consecutive bindings match (if needed)
489 auto binding_remaining = p_layout_->GetDescriptorCountFromBinding(update->dstBinding) - update->dstArrayElement;
490 if (!VerifyUpdateConsistency(update->dstBinding, binding_remaining, update->descriptorCount, "write update to", error_msg))
491 return false;
492 // Update is within bounds and consistent so perform update
493 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600494 // TODO : Can we break this into a set-level "Validate" followed by Descriptor updating itself
495 // if the validate passes? That saves us all the map ptrs in each descriptor instance
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600496 if (!descriptors_[start_idx + di]->WriteUpdate(update, di, error_msg)) {
497 std::stringstream error_str;
498 error_str << "Write update to descriptor at global index " << start_idx + di << " within set " << set_
499 << " binding #" << update->dstBinding << " failed with error message: " << error_msg->c_str();
500 *error_msg = error_str.str();
501 return false;
502 }
503 ++num_updates;
504 }
505 }
506 if (num_updates != 0) {
507 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600508 }
509 return true;
510}
511// Copy update
512bool cvdescriptorset::DescriptorSet::CopyUpdate(debug_report_data *report_data, const VkCopyDescriptorSet *update,
513 const DescriptorSet *src_set, std::string *error) {
514 auto num_updates = 0;
515 if (!p_layout_->HasBinding(update->dstBinding)) {
516 std::stringstream error_str;
517 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
518 *error = error_str.str();
519 return false;
520 }
521 if (!src_set->HasBinding(update->srcBinding)) {
522 std::stringstream error_str;
523 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
524 *error = error_str.str();
525 return false;
526 }
527 // src & dst set bindings are valid
528 // Check bounds of src & dst
529 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
530 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
531 // SRC update out of bounds
532 std::stringstream error_str;
533 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
534 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
535 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
536 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
537 *error = error_str.str();
538 return false;
539 }
540 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
541 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
542 // DST update out of bounds
543 std::stringstream error_str;
544 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
545 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
546 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
547 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
548 *error = error_str.str();
549 return false;
550 }
551 // Check that types match
552 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
553 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
554 if (src_type != dst_type) {
555 std::stringstream error_str;
556 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
557 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
558 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
559 *error = error_str.str();
560 return false;
561 }
562 // Verify consistency of src & dst bindings if update crosses binding boundaries
563 auto src_binding_remaining = src_set->GetDescriptorCountFromBinding(update->srcBinding) - update->srcArrayElement;
564 auto dst_binding_remaining = p_layout_->GetDescriptorCountFromBinding(update->dstBinding) - update->dstArrayElement;
565 if ((!VerifyUpdateConsistency(update->srcBinding, src_binding_remaining, update->descriptorCount, "copy update from", error)) ||
566 (!VerifyUpdateConsistency(update->dstBinding, dst_binding_remaining, update->descriptorCount, "copy update to", error))) {
567 return false;
568 }
569 // Update parameters all look good so perform update
570 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600571 if (!descriptors_[dst_start_idx]->CopyUpdate(src_set->descriptors_[src_start_idx].get(), error))
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600572 return false;
573 ++num_updates;
574 }
575 if (num_updates != 0) {
576 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600577 }
578 return true;
579}
580cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
581 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
582 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
583 updated = false;
584 descriptor_class = PlainSampler;
585};
586
587cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
588 const VkSampler *immut, const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
589 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
590 updated = false;
591 descriptor_class = PlainSampler;
592 if (immut) {
593 sampler_ = *immut;
594 immutable_ = true;
595 updated = true;
596 }
597}
598bool cvdescriptorset::ValidateSampler(const VkSampler sampler,
599 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600600 return (sampler_map->count(sampler) != 0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600601}
602bool cvdescriptorset::ValidateImageUpdate(const VkImageView image_view, const VkImageLayout image_layout,
603 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
604 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
605 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
606 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
607 std::string *error) {
608 auto image_pair = image_view_map->find(image_view);
609 if (image_pair == image_view_map->end()) {
610 std::stringstream error_str;
611 error_str << "Invalid VkImageView: " << image_view;
612 *error = error_str.str();
613 return false;
614 } else {
615 // Validate that imageLayout is compatible with aspect_mask and image format
616 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
617 VkImage image = image_pair->second.image;
618 VkFormat format = VK_FORMAT_MAX_ENUM;
619 auto img_pair = image_map->find(image);
620 if (img_pair != image_map->end()) {
621 format = img_pair->second.createInfo.format;
622 } else {
623 // Also need to check the swapchains.
624 auto swapchain_pair = image_to_swapchain_map->find(image);
625 if (swapchain_pair != image_to_swapchain_map->end()) {
626 VkSwapchainKHR swapchain = swapchain_pair->second;
627 auto swapchain_pair = swapchain_map->find(swapchain);
628 if (swapchain_pair != swapchain_map->end()) {
629 format = swapchain_pair->second->createInfo.imageFormat;
630 }
631 }
632 }
633 if (format == VK_FORMAT_MAX_ENUM) {
634 std::stringstream error_str;
635 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
636 *error = error_str.str();
637 return false;
638 } else {
639 bool ds = vk_format_is_depth_or_stencil(format);
640 switch (image_layout) {
641 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
642 // Only Color bit must be set
643 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
644 std::stringstream error_str;
645 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
646 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
647 *error = error_str.str();
648 return false;
649 }
650 // format must NOT be DS
651 if (ds) {
652 std::stringstream error_str;
653 error_str << "ImageView (" << image_view
654 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
655 << string_VkFormat(format) << " which is not a color format.";
656 *error = error_str.str();
657 return false;
658 }
659 break;
660 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
661 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
662 // Depth or stencil bit must be set, but both must NOT be set
663 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
664 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
665 // both must NOT be set
666 std::stringstream error_str;
667 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
668 *error = error_str.str();
669 return false;
670 }
671 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
672 // Neither were set
673 std::stringstream error_str;
674 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
675 << " but does not have STENCIL or DEPTH aspects set";
676 *error = error_str.str();
677 return false;
678 }
679 // format must be DS
680 if (!ds) {
681 std::stringstream error_str;
682 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
683 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
684 *error = error_str.str();
685 return false;
686 }
687 break;
688 default:
689 // anything to check for other layouts?
690 break;
691 }
692 }
693 }
694 return true;
695}
696bool cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
697 if (!immutable_) {
698 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
699 std::stringstream error_str;
700 error_str << "Attempted write update to sampler descriptor with invalid sampler: " << update->pImageInfo[index].sampler
701 << ".";
702 *error = error_str.str();
703 return false;
704 }
705 sampler_ = update->pImageInfo[index].sampler;
706 } else {
707 if (!ValidateSampler(sampler_, sampler_map_)) {
708 std::stringstream error_str;
709 error_str << "Attempted write update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
710 << ".";
711 *error = error_str.str();
712 return false;
713 }
714 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
715 }
716 updated = true;
717 return true;
718}
719
720bool cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
721 if (!immutable_) {
722 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
723 if (!ValidateSampler(update_sampler, sampler_map_)) {
724 std::stringstream error_str;
725 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
726 *error = error_str.str();
727 return false;
728 }
729 sampler_ = update_sampler;
730 } else {
731 if (!ValidateSampler(sampler_, sampler_map_)) {
732 std::stringstream error_str;
733 error_str << "Attempted copy update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
734 << ".";
735 *error = error_str.str();
736 return false;
737 }
738 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
739 }
740 updated = true;
741 return true;
742}
743cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
744 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
745 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
746 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
747 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
748 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
749 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
750 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
751 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
752 updated = false;
753 descriptor_class = ImageSampler;
754}
755
756cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
757 const VkSampler *immut, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
758 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
759 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
760 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
761 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
762 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
763 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
764 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
765 updated = false;
766 descriptor_class = ImageSampler;
767 if (immut) {
768 sampler_ = *immut;
769 immutable_ = true;
770 updated = true;
771 }
772}
773bool cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index,
774 std::string *error) {
775 // First check sampler
776 if (!immutable_) {
777 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
778 std::stringstream error_str;
779 error_str << "Attempted write update to combined image sampler descriptor with invalid sampler: "
780 << update->pImageInfo[index].sampler << ".";
781 *error = error_str.str();
782 return false;
783 }
784 sampler_ = update->pImageInfo[index].sampler;
785 } else {
786 if (!ValidateSampler(sampler_, sampler_map_)) {
787 std::stringstream error_str;
788 error_str << "Attempted write update to combined image sampler descriptor with immutable sampler which has invalid "
789 "immutable sampler: "
790 << sampler_ << ".";
791 *error = error_str.str();
792 return false;
793 }
794 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
795 }
796 // Now validate images
797 auto image_view = update->pImageInfo[index].imageView;
798 auto image_layout = update->pImageInfo[index].imageLayout;
799 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
800 error)) {
801 std::stringstream error_str;
802 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
803 *error = error_str.str();
804 return false;
805 }
806 updated = true;
807 image_view_ = image_view;
808 image_layout_ = image_layout;
809 return true;
810}
811
812bool cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
813 if (!immutable_) {
814 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
815 if (!ValidateSampler(update_sampler, sampler_map_)) {
816 std::stringstream error_str;
817 error_str << "Attempted copy update to combined image sampler descriptor with invalid sampler: " << update_sampler
818 << ".";
819 *error = error_str.str();
820 return false;
821 }
822 sampler_ = update_sampler;
823 } else {
824 if (!ValidateSampler(sampler_, sampler_map_)) {
825 std::stringstream error_str;
826 error_str << "Attempted copy update to combined image sampler descriptor with immutable sampler that has invalid "
827 "immutable sampler: "
828 << sampler_ << ".";
829 *error = error_str.str();
830 return false;
831 }
832 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
833 }
834 // Now validate images
835 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
836 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
837 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
838 error)) {
839 std::stringstream error_str;
840 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
841 *error = error_str.str();
842 return false;
843 }
844 updated = true;
845 image_view_ = image_view;
846 image_layout_ = image_layout;
847 return true;
848}
849
850cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type,
851 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
852 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
853 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
854 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
855 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED), image_view_map_(image_view_map),
856 image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
857 updated = false;
858 descriptor_class = Image;
859 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
860 storage_ = true;
861};
862
863bool cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
864 // First validate that the write update is valid
865 auto image_view = update->pImageInfo[index].imageView;
866 auto image_layout = update->pImageInfo[index].imageLayout;
867 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
868 error)) {
869 std::stringstream error_str;
870 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
871 *error = error_str.str();
872 return false;
873 }
874 // Update is clean so process it
875 updated = true;
876 image_view_ = image_view;
877 image_layout_ = image_layout;
878 return true;
879}
880
881bool cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
882 // First validate update
883 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
884 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
885 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
886 error)) {
887 std::stringstream error_str;
888 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
889 *error = error_str.str();
890 return false;
891 }
892 updated = true;
893 image_view_ = image_view;
894 image_layout_ = image_layout;
895 return true;
896}
897
898cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type,
899 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map)
900 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0), buffer_map_(buffer_map) {
901 updated = false;
902 descriptor_class = GeneralBuffer;
903 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
904 dynamic_ = true;
905 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
906 storage_ = true;
907 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
908 dynamic_ = true;
909 storage_ = true;
910 }
911}
912bool cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
913 // First validate bufferinfo
914 auto buffer = update->pBufferInfo[index].buffer;
915 if (!buffer_map_->count(buffer)) {
916 std::stringstream error_str;
917 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
918 *error = error_str.str();
919 return false;
920 }
921 updated = true;
922 buffer_ = buffer;
923 offset_ = update->pBufferInfo[index].offset;
924 range_ = update->pBufferInfo[index].range;
925 return true;
926}
927
928bool cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
929 // First validate bufferinfo
930 auto buffer = static_cast<const BufferDescriptor *>(src)->buffer_;
931 if (!buffer_map_->count(buffer)) {
932 std::stringstream error_str;
933 error_str << "Attempted copy update to buffer descriptor with invalid buffer: " << buffer;
934 *error = error_str.str();
935 return false;
936 }
937 updated = true;
938 buffer_ = buffer;
939 offset_ = static_cast<const BufferDescriptor *>(src)->offset_;
940 range_ = static_cast<const BufferDescriptor *>(src)->range_;
941 return true;
942}
943
944cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type,
945 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map)
946 : buffer_view_(VK_NULL_HANDLE), storage_(false), buffer_view_map_(buffer_view_map) {
947 updated = false;
948 descriptor_class = TexelBuffer;
949 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
950 storage_ = true;
951};
952bool cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
953 // First validate buffer view
954 auto buffer_view = update->pTexelBufferView[index];
955 if (!buffer_view_map_->count(buffer_view)) {
956 std::stringstream error_str;
957 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
958 *error = error_str.str();
959 return false;
960 }
961 updated = true;
962 buffer_view_ = buffer_view;
963 return true;
964}
965
966bool cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
967 // First validate buffer view
968 auto buffer_view = static_cast<const TexelDescriptor *>(src)->buffer_view_;
969 if (!buffer_view_map_->count(buffer_view)) {
970 std::stringstream error_str;
971 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
972 *error = error_str.str();
973 return false;
974 }
975 updated = true;
976 buffer_view_ = buffer_view;
977 return true;
978}