blob: ede1b5a3a32554a4e8ecb196d0708cdc0333c1c2 [file] [log] [blame]
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Tobin Ehlis <tobine@google.com>
19 */
20
21#include "descriptor_sets.h"
22#include "vk_enum_string_helper.h"
23#include "vk_safe_struct.h"
24#include <sstream>
25
26// Construct DescriptorSetLayout instance from given create info
27cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(debug_report_data *report_data,
28 const VkDescriptorSetLayoutCreateInfo *p_create_info,
29 const VkDescriptorSetLayout layout)
30 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
31 uint32_t global_index = 0;
32 for (uint32_t i = 0; i < binding_count_; ++i) {
33 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
34 if (!binding_to_index_map_.emplace(p_create_info->pBindings[i].binding, i).second) {
35 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
36 reinterpret_cast<uint64_t &>(layout_), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
37 "duplicated binding number in "
38 "VkDescriptorSetLayoutBinding");
39 }
40 binding_to_global_start_index_map_[p_create_info->pBindings[i].binding] = global_index;
41 global_index += p_create_info->pBindings[i].descriptorCount ? p_create_info->pBindings[i].descriptorCount - 1 : 0;
42 binding_to_global_end_index_map_[p_create_info->pBindings[i].binding] = global_index;
43 global_index++;
Tobin Ehlis664e6012016-05-05 11:04:44 -060044 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060045 // In cases where we should ignore pImmutableSamplers make sure it's NULL
46 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
47 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
48 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060049 bindings_.back().pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050 }
51 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
52 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
53 dynamic_descriptor_count_++;
54 }
55 }
56}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060057// put all bindings into the given set
58void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
59 for (auto binding_index_pair : binding_to_index_map_)
60 binding_set->insert(binding_index_pair.first);
61}
Tobin Ehlis56a30942016-05-19 08:00:00 -060062
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060063VkDescriptorSetLayoutBinding const *
64cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060065 const auto &bi_itr = binding_to_index_map_.find(binding);
66 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060067 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -060068 }
69 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060070}
71VkDescriptorSetLayoutBinding const *
72cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
73 if (index >= bindings_.size())
74 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -060075 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060076}
77// Return descriptorCount for given binding, 0 if index is unavailable
78uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060079 const auto &bi_itr = binding_to_index_map_.find(binding);
80 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060081 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060082 }
83 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060084}
85// Return descriptorCount for given index, 0 if index is unavailable
86uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
87 if (index >= bindings_.size())
88 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -060089 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060090}
91// For the given binding, return descriptorType
92VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
93 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -060094 const auto &bi_itr = binding_to_index_map_.find(binding);
95 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060096 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060097 }
98 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060099}
100// For the given index, return descriptorType
101VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
102 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600103 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600104}
105// For the given global index, return descriptorType
106// Currently just counting up through bindings_, may improve this in future
107VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
108 uint32_t global_offset = 0;
109 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600110 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600111 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600112 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113 }
114 assert(0); // requested global index is out of bounds
115 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
116}
117// For the given binding, return stageFlags
118VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
119 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600120 const auto &bi_itr = binding_to_index_map_.find(binding);
121 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600122 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600123 }
124 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600125}
126// For the given binding, return start index
127uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
128 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600129 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
130 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
131 return btgsi_itr->second;
132 }
133 // In error case max uint32_t so index is out of bounds to break ASAP
134 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600135}
136// For the given binding, return end index
137uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
138 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600139 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
140 if (btgei_itr != binding_to_global_end_index_map_.end()) {
141 return btgei_itr->second;
142 }
143 // In error case max uint32_t so index is out of bounds to break ASAP
144 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600145}
146// For given binding, return ptr to ImmutableSampler array
147VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
148 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600149 const auto &bi_itr = binding_to_index_map_.find(binding);
150 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600151 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600152 }
153 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600154}
155// For given index, return ptr to ImmutableSampler array
156VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
157 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600158 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600159}
160// If our layout is compatible with rh_ds_layout, return true,
161// else return false and fill in error_msg will description of what causes incompatibility
162bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
163 // Trivial case
164 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
165 return true;
166 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
167 std::stringstream error_str;
168 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
169 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
170 *error_msg = error_str.str();
171 return false; // trivial fail case
172 }
173 // Descriptor counts match so need to go through bindings one-by-one
174 // and verify that type and stageFlags match
175 for (auto binding : bindings_) {
176 // TODO : Do we also need to check immutable samplers?
177 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600178 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600179 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600180 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
181 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600182 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600183 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600184 *error_msg = error_str.str();
185 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600186 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600187 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600188 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
189 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600190 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600191 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600192 *error_msg = error_str.str();
193 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600194 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600195 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600196 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
197 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600198 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600199 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600200 *error_msg = error_str.str();
201 return false;
202 }
203 }
204 return true;
205}
206
207bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
208 if (!binding_to_index_map_.count(binding + 1))
209 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600210 auto const &bi_itr = binding_to_index_map_.find(binding);
211 if (bi_itr != binding_to_index_map_.end()) {
212 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
213 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600214 auto type = bindings_[bi_itr->second].descriptorType;
215 auto stage_flags = bindings_[bi_itr->second].stageFlags;
216 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
217 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
218 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
219 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600220 return false;
221 }
222 return true;
223 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600224 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600225 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600226}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600227// Starting at offset descriptor of given binding, parse over update_count
228// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
229// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
230// If so, return true. If not, fill in error_msg and return false
231bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
232 const char *type, const VkDescriptorSet set,
233 std::string *error_msg) const {
234 // Verify consecutive bindings match (if needed)
235 auto orig_binding = current_binding;
236 // Track count of descriptors in the current_bindings that are remaining to be updated
237 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
238 // First, it's legal to offset beyond your own binding so handle that case
239 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
240 while (offset >= binding_remaining) {
241 // Advance to next binding, decrement offset by binding size
242 offset -= binding_remaining;
243 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
244 }
245 binding_remaining -= offset;
246 while (update_count > binding_remaining) { // While our updates overstep current binding
247 // Verify next consecutive binding matches type, stage flags & immutable sampler use
248 if (!IsNextBindingConsistent(current_binding++)) {
249 std::stringstream error_str;
250 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
251 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
252 "next binding is not consistent with current binding so this update is invalid.";
253 *error_msg = error_str.str();
254 return false;
255 }
256 // For sake of this check consider the bindings updated and grab count for next binding
257 update_count -= binding_remaining;
258 binding_remaining = GetDescriptorCountFromBinding(current_binding);
259 }
260 return true;
261}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600262
263cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout,
264 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map,
265 const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *memory_map,
266 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map,
267 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map,
268 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
269 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
270 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
271 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlis56a30942016-05-19 08:00:00 -0600272 : some_update_(false), set_(set), p_layout_(layout), buffer_map_(buffer_map), memory_map_(memory_map),
273 buffer_view_map_(buffer_view_map), sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
274 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600275 // Foreach binding, create default descriptors of given type
276 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
277 auto type = p_layout_->GetTypeFromIndex(i);
278 switch (type) {
279 case VK_DESCRIPTOR_TYPE_SAMPLER: {
280 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
281 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
282 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200283 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600284 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200285 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600286 }
287 break;
288 }
289 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
290 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
291 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
292 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200293 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600294 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200295 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600296 }
297 break;
298 }
299 // ImageDescriptors
300 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
301 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
302 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
303 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200304 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600305 break;
306 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
307 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
308 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200309 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600310 break;
311 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
312 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
313 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
314 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
315 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200316 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600317 break;
318 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600319 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600320 break;
321 }
322 }
323}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600324
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600325cvdescriptorset::DescriptorSet::~DescriptorSet() {
326 InvalidateBoundCmdBuffers();
327 // Remove link to any cmd buffers
328 for (auto cb : bound_cmd_buffers_) {
329 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
330 cb->lastBound[i].uniqueBoundSets.erase(this);
331 }
332 }
333}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600334// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
335bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
336 return layout->IsCompatible(p_layout_, error);
337}
338// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
339// This includes validating that all descriptors in the given bindings are updated,
340// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
341// Return true if state is acceptable, or false and write an error message into error string
342bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
343 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200344 auto dyn_offset_index = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600345 for (auto binding : bindings) {
346 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600347 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600348 // Nothing to do for strictly immutable sampler
349 } else {
350 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600351 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600352 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600353 std::stringstream error_str;
354 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
355 << " is being used in draw but has not been updated.";
356 *error = error_str.str();
357 return false;
358 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600359 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600360 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600361 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600362 auto buffer_node = buffer_map_->find(buffer);
363 if (buffer_node == buffer_map_->end()) {
364 std::stringstream error_str;
365 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
366 << " references invalid buffer " << buffer << ".";
367 *error = error_str.str();
368 return false;
369 } else {
370 auto mem_entry = memory_map_->find(buffer_node->second.mem);
371 if (mem_entry == memory_map_->end()) {
372 std::stringstream error_str;
373 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
374 << " uses buffer " << buffer << " that references invalid memory "
375 << buffer_node->second.mem << ".";
376 *error = error_str.str();
377 return false;
378 }
379 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600380 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600381 // Validate that dynamic offsets are within the buffer
382 auto buffer_size = buffer_node->second.createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600383 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
384 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600385 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
386 if (VK_WHOLE_SIZE == range) {
387 if ((dyn_offset + desc_offset) > buffer_size) {
388 std::stringstream error_str;
389 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
390 << " uses buffer " << buffer
391 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
392 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
393 << buffer_size << ".";
394 *error = error_str.str();
395 return false;
396 }
397 } else {
398 if ((dyn_offset + desc_offset + range) > buffer_size) {
399 std::stringstream error_str;
400 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
401 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
402 << " combined with offset " << desc_offset << " and range " << range
403 << " that oversteps the buffer size of " << buffer_size << ".";
404 *error = error_str.str();
405 return false;
406 }
407 }
408 }
409 }
410 }
411 }
412 }
413 }
414 return true;
415}
416// For given bindings, place any update buffers or images into the passed-in unordered_sets
417uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
418 std::unordered_set<VkBuffer> *buffer_set,
419 std::unordered_set<VkImageView> *image_set) const {
420 auto num_updates = 0;
421 for (auto binding : bindings) {
422 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600423 if (descriptors_[start_idx]->IsStorage()) {
424 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600425 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600426 if (descriptors_[start_idx + i]->updated) {
427 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600428 num_updates++;
429 }
430 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600431 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600432 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600433 if (descriptors_[start_idx + i]->updated) {
434 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600435 const auto &buff_pair = buffer_view_map_->find(bufferview);
436 if (buff_pair != buffer_view_map_->end()) {
437 buffer_set->insert(buff_pair->second.buffer);
438 num_updates++;
439 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600440 }
441 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600442 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600443 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600444 if (descriptors_[start_idx + i]->updated) {
445 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600446 num_updates++;
447 }
448 }
449 }
450 }
451 }
452 return num_updates;
453}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600454// Set is being deleted or updates so invalidate all bound cmd buffers
455void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
456 for (auto cb_node : bound_cmd_buffers_) {
457 cb_node->state = CB_INVALID;
458 }
459}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600460// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600461void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600462 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
463 // perform update
464 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
465 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600466 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600467 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600468 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600469
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600470 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600471}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600472// Validate Copy update
473bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
474 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600475 // Verify idle ds
476 if (in_use.load()) {
477 std::stringstream error_str;
478 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
479 << " that is in use by a command buffer.";
480 *error = error_str.str();
481 return false;
482 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600483 if (!p_layout_->HasBinding(update->dstBinding)) {
484 std::stringstream error_str;
485 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
486 *error = error_str.str();
487 return false;
488 }
489 if (!src_set->HasBinding(update->srcBinding)) {
490 std::stringstream error_str;
491 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
492 *error = error_str.str();
493 return false;
494 }
495 // src & dst set bindings are valid
496 // Check bounds of src & dst
497 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
498 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
499 // SRC update out of bounds
500 std::stringstream error_str;
501 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
502 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
503 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
504 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
505 *error = error_str.str();
506 return false;
507 }
508 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
509 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
510 // DST update out of bounds
511 std::stringstream error_str;
512 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
513 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
514 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
515 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
516 *error = error_str.str();
517 return false;
518 }
519 // Check that types match
520 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
521 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
522 if (src_type != dst_type) {
523 std::stringstream error_str;
524 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
525 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
526 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
527 *error = error_str.str();
528 return false;
529 }
530 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600531 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
532 "copy update from", src_set->GetSet(), error)) ||
533 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
534 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600535 return false;
536 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600537 // First make sure source descriptors are updated
538 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
539 if (!src_set->descriptors_[src_start_idx + i]) {
540 std::stringstream error_str;
541 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
542 << update->srcArrayElement + i << " has not been updated.";
543 *error = error_str.str();
544 return false;
545 }
546 }
547 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600548 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600549 return false;
550
551 // All checks passed so update is good
552 return true;
553}
554// Perform Copy update
555void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600556 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
557 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600558 // Update parameters all look good so perform update
559 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600560 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600561 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600562 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600563 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600564
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600565 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600566}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600567
Tobin Ehlis300888c2016-05-18 13:43:26 -0600568cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600569 updated = false;
570 descriptor_class = PlainSampler;
571};
572
Tobin Ehlis300888c2016-05-18 13:43:26 -0600573cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600574 updated = false;
575 descriptor_class = PlainSampler;
576 if (immut) {
577 sampler_ = *immut;
578 immutable_ = true;
579 updated = true;
580 }
581}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600582
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600583bool cvdescriptorset::ValidateSampler(const VkSampler sampler,
584 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600585 return (sampler_map->count(sampler) != 0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600586}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600587
Tobin Ehlis554bf382016-05-24 11:14:43 -0600588bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600589 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
590 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
591 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
592 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
593 std::string *error) {
594 auto image_pair = image_view_map->find(image_view);
595 if (image_pair == image_view_map->end()) {
596 std::stringstream error_str;
597 error_str << "Invalid VkImageView: " << image_view;
598 *error = error_str.str();
599 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600600 }
601 // Validate that imageLayout is compatible with aspect_mask and image format
602 // and validate that image usage bits are correct for given usage
603 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
604 VkImage image = image_pair->second.image;
605 VkFormat format = VK_FORMAT_MAX_ENUM;
606 VkImageUsageFlags usage = 0;
607 auto img_pair = image_map->find(image);
608 if (img_pair != image_map->end()) {
609 format = img_pair->second.createInfo.format;
610 usage = img_pair->second.createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600611 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600612 // Also need to check the swapchains.
613 auto swapchain_pair = image_to_swapchain_map->find(image);
614 if (swapchain_pair != image_to_swapchain_map->end()) {
615 VkSwapchainKHR swapchain = swapchain_pair->second;
616 auto swapchain_pair = swapchain_map->find(swapchain);
617 if (swapchain_pair != swapchain_map->end()) {
618 format = swapchain_pair->second->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600619 }
620 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600621 }
622 // First validate that format and layout are compatible
623 if (format == VK_FORMAT_MAX_ENUM) {
624 std::stringstream error_str;
625 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
626 *error = error_str.str();
627 return false;
628 }
629 bool ds = vk_format_is_depth_or_stencil(format);
630 switch (image_layout) {
631 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
632 // Only Color bit must be set
633 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600634 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600635 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
636 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600637 *error = error_str.str();
638 return false;
639 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600640 // format must NOT be DS
641 if (ds) {
642 std::stringstream error_str;
643 error_str << "ImageView (" << image_view
644 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
645 << string_VkFormat(format) << " which is not a color format.";
646 *error = error_str.str();
647 return false;
648 }
649 break;
650 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
651 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
652 // Depth or stencil bit must be set, but both must NOT be set
653 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
654 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
655 // both must NOT be set
656 std::stringstream error_str;
657 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
658 *error = error_str.str();
659 return false;
660 }
661 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
662 // Neither were set
663 std::stringstream error_str;
664 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
665 << " but does not have STENCIL or DEPTH aspects set";
666 *error = error_str.str();
667 return false;
668 }
669 // format must be DS
670 if (!ds) {
671 std::stringstream error_str;
672 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
673 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
674 *error = error_str.str();
675 return false;
676 }
677 break;
678 default:
679 // anything to check for other layouts?
680 break;
681 }
682 // Now validate that usage flags are correctly set for given type of update
683 std::string error_usage_bit;
684 switch (type) {
685 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
686 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
687 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
688 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
689 }
690 break;
691 }
692 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
693 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
694 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
695 }
696 break;
697 }
698 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
699 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
700 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
701 }
702 break;
703 }
704 default:
705 break;
706 }
707 if (!error_usage_bit.empty()) {
708 std::stringstream error_str;
709 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
710 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
711 << error_usage_bit << " set.";
712 *error = error_str.str();
713 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600714 }
715 return true;
716}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600717
Tobin Ehlis300888c2016-05-18 13:43:26 -0600718void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
719 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600720 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600721}
722
Tobin Ehlis300888c2016-05-18 13:43:26 -0600723void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600724 if (!immutable_) {
725 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600726 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600727 }
728 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600729}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600730
Tobin Ehlis300888c2016-05-18 13:43:26 -0600731cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
732 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600733 updated = false;
734 descriptor_class = ImageSampler;
735}
736
Tobin Ehlis300888c2016-05-18 13:43:26 -0600737cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
738 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600739 updated = false;
740 descriptor_class = ImageSampler;
741 if (immut) {
742 sampler_ = *immut;
743 immutable_ = true;
744 updated = true;
745 }
746}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600747
Tobin Ehlis300888c2016-05-18 13:43:26 -0600748void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600749 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600750 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600751 sampler_ = image_info.sampler;
752 image_view_ = image_info.imageView;
753 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600754}
755
Tobin Ehlis300888c2016-05-18 13:43:26 -0600756void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600757 if (!immutable_) {
758 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600759 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600760 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600761 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
762 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600763 updated = true;
764 image_view_ = image_view;
765 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600766}
767
Tobin Ehlis300888c2016-05-18 13:43:26 -0600768cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
769 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600770 updated = false;
771 descriptor_class = Image;
772 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
773 storage_ = true;
774};
775
Tobin Ehlis300888c2016-05-18 13:43:26 -0600776void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600777 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600778 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600779 image_view_ = image_info.imageView;
780 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600781}
782
Tobin Ehlis300888c2016-05-18 13:43:26 -0600783void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600784 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
785 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600786 updated = true;
787 image_view_ = image_view;
788 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600789}
790
Tobin Ehlis300888c2016-05-18 13:43:26 -0600791cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
792 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600793 updated = false;
794 descriptor_class = GeneralBuffer;
795 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
796 dynamic_ = true;
797 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
798 storage_ = true;
799 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
800 dynamic_ = true;
801 storage_ = true;
802 }
803}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600804void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600805 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600806 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600807 buffer_ = buffer_info.buffer;
808 offset_ = buffer_info.offset;
809 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600810}
811
Tobin Ehlis300888c2016-05-18 13:43:26 -0600812void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
813 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600814 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600815 buffer_ = buff_desc->buffer_;
816 offset_ = buff_desc->offset_;
817 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600818}
819
Tobin Ehlis300888c2016-05-18 13:43:26 -0600820cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600821 updated = false;
822 descriptor_class = TexelBuffer;
823 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
824 storage_ = true;
825};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600826
Tobin Ehlis300888c2016-05-18 13:43:26 -0600827void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600828 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600829 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600830}
831
Tobin Ehlis300888c2016-05-18 13:43:26 -0600832void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
833 updated = true;
834 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
835}
836// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
837// sets, and then calls their respective Validate[Write|Copy]Update functions.
838// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
839// be skipped, then true is returned.
840// If there is no issue with the update, then false is returned.
841bool cvdescriptorset::ValidateUpdateDescriptorSets(
842 const debug_report_data *report_data, const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map,
Tobin Ehlis56a30942016-05-19 08:00:00 -0600843 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600844 bool skip_call = false;
845 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600846 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600847 auto dest_set = p_wds[i].dstSet;
848 auto set_pair = set_map.find(dest_set);
849 if (set_pair == set_map.end()) {
850 skip_call |=
851 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis56a30942016-05-19 08:00:00 -0600852 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600853 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
854 reinterpret_cast<uint64_t &>(dest_set));
855 } else {
856 std::string error_str;
857 if (!set_pair->second->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
858 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
859 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
860 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
861 " with error: %s",
862 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
863 }
864 }
865 }
866 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600867 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600868 auto dst_set = p_cds[i].dstSet;
869 auto src_set = p_cds[i].srcSet;
870 auto src_pair = set_map.find(src_set);
871 auto dst_pair = set_map.find(dst_set);
872 if (src_pair == set_map.end()) {
873 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis56a30942016-05-19 08:00:00 -0600874 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600875 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
876 " that has not been allocated.",
877 reinterpret_cast<uint64_t &>(src_set));
878 } else if (dst_pair == set_map.end()) {
879 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis56a30942016-05-19 08:00:00 -0600880 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600881 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
882 " that has not been allocated.",
883 reinterpret_cast<uint64_t &>(dst_set));
884 } else {
885 std::string error_str;
886 if (!dst_pair->second->ValidateCopyUpdate(report_data, &p_cds[i], src_pair->second, &error_str)) {
887 skip_call |=
888 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
889 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
890 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
891 " to Descriptor Set 0x%" PRIx64 " with error: %s",
892 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
893 }
894 }
895 }
896 return skip_call;
897}
898// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
899// sets, and then calls their respective Perform[Write|Copy]Update functions.
900// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
901// with the same set of updates.
902// This is split from the validate code to allow validation prior to calling down the chain, and then update after
903// calling down the chain.
904void cvdescriptorset::PerformUpdateDescriptorSets(
Tobin Ehlis56a30942016-05-19 08:00:00 -0600905 const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map, uint32_t write_count,
906 const VkWriteDescriptorSet *p_wds, uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600907 // Write updates first
908 uint32_t i = 0;
909 for (i = 0; i < write_count; ++i) {
910 auto dest_set = p_wds[i].dstSet;
911 auto set_pair = set_map.find(dest_set);
912 if (set_pair != set_map.end()) {
913 set_pair->second->PerformWriteUpdate(&p_wds[i]);
914 }
915 }
916 // Now copy updates
917 for (i = 0; i < copy_count; ++i) {
918 auto dst_set = p_cds[i].dstSet;
919 auto src_set = p_cds[i].srcSet;
920 auto src_pair = set_map.find(src_set);
921 auto dst_pair = set_map.find(dst_set);
922 if (src_pair != set_map.end() && dst_pair != set_map.end()) {
923 dst_pair->second->PerformCopyUpdate(&p_cds[i], src_pair->second);
924 }
925 }
926}
927// Validate the state for a given write update but don't actually perform the update
928// If an error would occur for this update, return false and fill in details in error_msg string
929bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
930 std::string *error_msg) {
931 // Verify idle ds
932 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600933 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600934 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
935 << " that is in use by a command buffer.";
936 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600937 return false;
938 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600939 // Verify dst binding exists
940 if (!p_layout_->HasBinding(update->dstBinding)) {
941 std::stringstream error_str;
942 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
943 *error_msg = error_str.str();
944 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -0600945 }
946 // We know that binding is valid, verify update and do update on each descriptor
947 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
948 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
949 if (type != update->descriptorType) {
950 std::stringstream error_str;
951 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
952 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
953 *error_msg = error_str.str();
954 return false;
955 }
956 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
957 std::stringstream error_str;
958 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
959 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
960 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
961 << " combined with update array element offset of " << update->dstArrayElement
962 << " oversteps the size of this descriptor set.";
963 *error_msg = error_str.str();
964 return false;
965 }
966 // Verify consecutive bindings match (if needed)
967 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
968 set_, error_msg))
969 return false;
970 // Update is within bounds and consistent so last step is to validate update contents
971 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
972 std::stringstream error_str;
973 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
974 << " failed with error message: " << error_msg->c_str();
975 *error_msg = error_str.str();
976 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600977 }
978 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600979 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600980}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600981// For the given buffer, verify that its creation parameters are appropriate for the given type
982// If there's an error, update the error string with details and return false, else return true
983bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkBuffer buffer, VkDescriptorType type, std::string *error) const {
984 // First make sure that buffer is valid
985 auto buff_it = buffer_map_->find(buffer);
986 if (buff_it == buffer_map_->end()) {
987 std::stringstream error_str;
988 error_str << "Invalid VkBuffer: " << buffer;
989 *error = error_str.str();
990 return false;
991 }
992 // Verify that usage bits set correctly for given type
993 auto usage = buff_it->second.createInfo.usage;
994 std::string error_usage_bit;
995 switch (type) {
996 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
997 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
998 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
999 }
1000 break;
1001 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1002 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1003 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1004 }
1005 break;
1006 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1007 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1008 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1009 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1010 }
1011 break;
1012 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1013 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1014 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1015 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1016 }
1017 break;
1018 default:
1019 break;
1020 }
1021 if (!error_usage_bit.empty()) {
1022 std::stringstream error_str;
1023 error_str << "Buffer (" << buffer << ") with usage mask 0x" << usage << " being used for a descriptor update of type "
1024 << string_VkDescriptorType(type) << " does not have " << error_usage_bit << " set.";
1025 *error = error_str.str();
1026 return false;
1027 }
1028 return true;
1029}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001030// Verify that the contents of the update are ok, but don't perform actual update
1031bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1032 std::string *error) const {
1033 switch (update->descriptorType) {
1034 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1035 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1036 // Validate image
1037 auto image_view = update->pImageInfo[di].imageView;
1038 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis554bf382016-05-24 11:14:43 -06001039 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, image_view_map_, image_map_,
1040 image_to_swapchain_map_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001041 std::stringstream error_str;
1042 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1043 *error = error_str.str();
1044 return false;
1045 }
1046 }
1047 // Intentional fall-through to validate sampler
1048 }
1049 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1050 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1051 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
1052 if (!ValidateSampler(update->pImageInfo[di].sampler, sampler_map_)) {
1053 std::stringstream error_str;
1054 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1055 << update->pImageInfo[di].sampler << ".";
1056 *error = error_str.str();
1057 return false;
1058 }
1059 } else {
1060 // TODO : Warn here
1061 }
1062 }
1063 break;
1064 }
1065 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1066 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1067 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1068 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1069 auto image_view = update->pImageInfo[di].imageView;
1070 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis554bf382016-05-24 11:14:43 -06001071 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, image_view_map_, image_map_,
1072 image_to_swapchain_map_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001073 std::stringstream error_str;
1074 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1075 *error = error_str.str();
1076 return false;
1077 }
1078 }
1079 break;
1080 }
1081 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1082 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1083 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1084 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001085 auto buffer_view_it = buffer_view_map_->find(buffer_view);
1086 if (buffer_view_it == buffer_view_map_->end()) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001087 std::stringstream error_str;
1088 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1089 *error = error_str.str();
1090 return false;
1091 }
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001092 auto buffer = buffer_view_it->second.buffer;
1093 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
1094 std::stringstream error_str;
1095 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1096 *error = error_str.str();
1097 return false;
1098 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001099 }
1100 break;
1101 }
1102 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1103 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1104 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1105 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1106 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1107 auto buffer = update->pBufferInfo[di].buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001108 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001109 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001110 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001111 *error = error_str.str();
1112 return false;
1113 }
1114 }
1115 break;
1116 }
1117 default:
1118 assert(0); // We've already verified update type so should never get here
1119 break;
1120 }
1121 // All checks passed so update contents are good
1122 return true;
1123}
1124// Verify that the contents of the update are ok, but don't perform actual update
1125bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001126 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001127 switch (src_set->descriptors_[index]->descriptor_class) {
1128 case PlainSampler: {
1129 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1130 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1131 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
1132 if (!ValidateSampler(update_sampler, sampler_map_)) {
1133 std::stringstream error_str;
1134 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1135 *error = error_str.str();
1136 return false;
1137 }
1138 } else {
1139 // TODO : Warn here
1140 }
1141 }
1142 break;
1143 }
1144 case ImageSampler: {
1145 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1146 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1147 // First validate sampler
1148 if (!img_samp_desc->IsImmutableSampler()) {
1149 auto update_sampler = img_samp_desc->GetSampler();
1150 if (!ValidateSampler(update_sampler, sampler_map_)) {
1151 std::stringstream error_str;
1152 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1153 *error = error_str.str();
1154 return false;
1155 }
1156 } else {
1157 // TODO : Warn here
1158 }
1159 // Validate image
1160 auto image_view = img_samp_desc->GetImageView();
1161 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001162 if (!ValidateImageUpdate(image_view, image_layout, type, image_view_map_, image_map_, image_to_swapchain_map_,
1163 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001164 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001165 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001166 *error = error_str.str();
1167 return false;
1168 }
1169 }
1170 }
1171 case Image: {
1172 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1173 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1174 auto image_view = img_desc->GetImageView();
1175 auto image_layout = img_desc->GetImageLayout();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001176 if (!ValidateImageUpdate(image_view, image_layout, type, image_view_map_, image_map_, image_to_swapchain_map_,
1177 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001178 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001179 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001180 *error = error_str.str();
1181 return false;
1182 }
1183 }
1184 break;
1185 }
1186 case TexelBuffer: {
1187 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1188 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001189 auto bv_it = buffer_view_map_->find(buffer_view);
1190 if (bv_it == buffer_view_map_->end()) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001191 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001192 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1193 *error = error_str.str();
1194 return false;
1195 }
1196 auto buffer = bv_it->second.buffer;
1197 if (!ValidateBufferUpdate(buffer, type, error)) {
1198 std::stringstream error_str;
1199 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001200 *error = error_str.str();
1201 return false;
1202 }
1203 }
1204 break;
1205 }
1206 case GeneralBuffer: {
1207 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1208 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001209 if (!ValidateBufferUpdate(buffer, type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001210 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001211 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001212 *error = error_str.str();
1213 return false;
1214 }
1215 }
1216 break;
1217 }
1218 default:
1219 assert(0); // We've already verified update type so should never get here
1220 break;
1221 }
1222 // All checks passed so update contents are good
1223 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001224}
Tobin Ehlisee471462016-05-26 11:21:59 -06001225// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
1226bool cvdescriptorset::ValidateAllocateDescriptorSets(
1227 const debug_report_data *report_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1228 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &set_layout_map,
1229 const std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> &pool_map) {
1230 bool skip_call = false;
1231 uint32_t requiredDescriptorsByType[VK_DESCRIPTOR_TYPE_RANGE_SIZE]{};
1232
1233 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1234 auto layout_it = set_layout_map.find(p_alloc_info->pSetLayouts[i]);
1235 if (layout_it == set_layout_map.end()) {
1236 skip_call |=
1237 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1238 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1239 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1240 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
1241 }
1242 // Count total descriptors required per type
1243 for (uint32_t j = 0; j < layout_it->second->GetBindingCount(); ++j) {
1244 const auto &binding_layout = layout_it->second->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1245 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1246 requiredDescriptorsByType[typeIndex] += binding_layout->descriptorCount;
1247 }
1248 }
1249 auto pool_it = pool_map.find(p_alloc_info->descriptorPool);
1250 if (pool_it == pool_map.end()) {
1251 skip_call |=
1252 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1253 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool), __LINE__, DRAWSTATE_INVALID_POOL, "DS",
1254 "Unable to find pool node for pool 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1255 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool));
1256 } else { // Make sure pool has all the available descriptors before calling down chain
1257 // Track number of descriptorSets allowable in this pool
1258 if (pool_it->second->availableSets < p_alloc_info->descriptorSetCount) {
1259 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1260 reinterpret_cast<uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1261 "DS", "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1262 ". This pool only has %d descriptorSets remaining.",
1263 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_it->second->pool),
1264 pool_it->second->availableSets);
1265 }
1266 // Determine whether descriptor counts are satisfiable
1267 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1268 if (requiredDescriptorsByType[i] > pool_it->second->availableDescriptorTypeCount[i]) {
1269 skip_call |=
1270 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1271 reinterpret_cast<const uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1272 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1273 ". This pool only has %d descriptors of this type remaining.",
1274 requiredDescriptorsByType[i], string_VkDescriptorType(VkDescriptorType(i)),
1275 reinterpret_cast<uint64_t &>(pool_it->second->pool), pool_it->second->availableDescriptorTypeCount[i]);
1276 }
1277 }
1278 }
1279 return skip_call;
1280}
1281// Decrement allocated sets from the pool and insert new sets into set_map
1282void cvdescriptorset::PerformAllocateDescriptorSets(
1283 const VkDescriptorSetAllocateInfo *p_alloc_info, const VkDescriptorSet *descriptor_sets,
1284 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
1285 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1286 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &layout_map,
1287 const std::unordered_map<VkBuffer, BUFFER_NODE> &buffer_map,
1288 const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> &mem_obj_map,
1289 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> &buffer_view_map,
1290 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> &sampler_map,
1291 const std::unordered_map<VkImageView, VkImageViewCreateInfo> &image_view_map,
1292 const std::unordered_map<VkImage, IMAGE_NODE> &image_map,
1293 const std::unordered_map<VkImage, VkSwapchainKHR> &image_to_swapchain_map,
1294 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> &swapchain_map) {
1295 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
1296 /* Account for sets allocated from pool */
1297 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
1298 /* Create tracking object for each descriptor set; insert into
1299 * global map and the pool's set.
1300 */
1301 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1302 auto layout_state = layout_map.find(p_alloc_info->pSetLayouts[i])->second;
1303 // Account for individual descriptors allocated from pool
1304 for (uint32_t j = 0; j < layout_state->GetBindingCount(); ++j) {
1305 const auto &binding_layout = layout_state->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1306 uint32_t type_index = static_cast<uint32_t>(binding_layout->descriptorType);
1307 pool_state->availableDescriptorTypeCount[type_index] -= binding_layout->descriptorCount;
1308 }
1309 auto new_ds =
1310 new cvdescriptorset::DescriptorSet(descriptor_sets[i], layout_state, &buffer_map, &mem_obj_map, &buffer_view_map,
1311 &sampler_map, &image_view_map, &image_map, &image_to_swapchain_map, &swapchain_map);
1312
1313 pool_state->sets.insert(new_ds);
1314 new_ds->in_use.store(0);
1315 (*set_map)[descriptor_sets[i]] = new_ds;
1316 }
1317}