blob: d0a56b83cbd3a393448e57ade6495436e26cdb72 [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>
John Zulaufc483f442017-12-15 14:02:06 -070019 * John Zulauf <jzulauf@lunarg.com>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060020 */
21
Tobin Ehlisf922ef82016-11-30 10:19:14 -070022// Allow use of STL min and max functions in Windows
23#define NOMINMAX
24
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060025#include "descriptor_sets.h"
John Zulaufd47d0612018-02-16 13:00:34 -070026#include "hash_vk_types.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060027#include "vk_enum_string_helper.h"
28#include "vk_safe_struct.h"
Tobin Ehlisc8266452017-04-07 12:20:30 -060029#include "buffer_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060030#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070031#include <algorithm>
John Zulauf1f8174b2018-02-16 12:58:37 -070032#include <memory>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060033
John Zulauf508d13a2018-01-05 15:10:34 -070034struct BindingNumCmp {
Yuly Novikovd3f37732018-01-12 18:54:12 -050035 bool operator()(const VkDescriptorSetLayoutBinding *a, const VkDescriptorSetLayoutBinding *b) const {
John Zulauf508d13a2018-01-05 15:10:34 -070036 return a->binding < b->binding;
37 }
38};
39
John Zulaufd47d0612018-02-16 13:00:34 -070040using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
41using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
42
John Zulauf34ebf272018-02-16 13:08:47 -070043// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
44cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
John Zulaufd47d0612018-02-16 13:00:34 -070045
John Zulauf34ebf272018-02-16 13:08:47 -070046DescriptorSetLayoutId get_canonical_id(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
47 return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
John Zulaufd47d0612018-02-16 13:00:34 -070048}
John Zulauf34ebf272018-02-16 13:08:47 -070049
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070051// Proactively reserve and resize as possible, as the reallocation was visible in profiling
John Zulauf1f8174b2018-02-16 12:58:37 -070052cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
53 : flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
John Zulauf48a6a702017-12-22 17:14:54 -070054 binding_type_stats_ = {0, 0, 0};
John Zulauf508d13a2018-01-05 15:10:34 -070055 std::set<const VkDescriptorSetLayoutBinding *, BindingNumCmp> sorted_bindings;
56 const uint32_t input_bindings_count = p_create_info->bindingCount;
57 // Sort the input bindings in binding number order, eliminating duplicates
58 for (uint32_t i = 0; i < input_bindings_count; i++) {
59 sorted_bindings.insert(p_create_info->pBindings + i);
John Zulaufb6d71202017-12-22 16:47:09 -070060 }
61
62 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070063 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070064 uint32_t index = 0;
65 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
66 bindings_.reserve(binding_count_);
67 binding_to_index_map_.reserve(binding_count_);
68 for (auto input_binding : sorted_bindings) {
69 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
70 const auto binding_num = input_binding->binding;
71 binding_to_index_map_[binding_num] = index++;
72 bindings_.emplace_back(input_binding);
73 auto &binding_info = bindings_.back();
74
John Zulaufb6d71202017-12-22 16:47:09 -070075 descriptor_count_ += binding_info.descriptorCount;
76 if (binding_info.descriptorCount > 0) {
77 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -070078 }
John Zulaufb6d71202017-12-22 16:47:09 -070079
80 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
81 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
82 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
83 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -070084 binding_type_stats_.dynamic_buffer_count++;
85 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
86 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
87 binding_type_stats_.non_dynamic_buffer_count++;
88 } else {
89 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060090 }
91 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -070092 assert(bindings_.size() == binding_count_);
93 uint32_t global_index = 0;
John Zulaufb6d71202017-12-22 16:47:09 -070094 binding_to_global_index_range_map_.reserve(binding_count_);
95 // Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
Tobin Ehlis9637fb22016-12-12 15:59:34 -070096 for (uint32_t i = 0; i < binding_count_; ++i) {
97 auto binding_num = bindings_[i].binding;
John Zulaufc483f442017-12-15 14:02:06 -070098 auto final_index = global_index + bindings_[i].descriptorCount;
99 binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
John Zulaufb6d71202017-12-22 16:47:09 -0700100 if (final_index != global_index) {
101 global_start_to_index_map_[global_index] = i;
102 }
John Zulaufc483f442017-12-15 14:02:06 -0700103 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700104 }
John Zulaufb6d71202017-12-22 16:47:09 -0700105
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700106 // Now create dyn offset array mapping for any dynamic descriptors
107 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700108 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700109 for (const auto &bc_pair : binding_to_dyn_count) {
110 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
111 dyn_array_idx += bc_pair.second;
112 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600114
John Zulaufd47d0612018-02-16 13:00:34 -0700115size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
116 hash_util::HashCombiner hc;
117 hc << flags_;
118 hc.Combine(bindings_);
119 return hc.Value();
120}
121//
122
John Zulauf1f8174b2018-02-16 12:58:37 -0700123// Return valid index or "end" i.e. binding_count_;
124// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
125// Common code for all binding lookups.
126uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
127 const auto &bi_itr = binding_to_index_map_.find(binding);
128 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
129 return GetBindingCount();
130}
131VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
132 const uint32_t index) const {
133 if (index >= bindings_.size()) return nullptr;
134 return bindings_[index].ptr();
135}
136// Return descriptorCount for given index, 0 if index is unavailable
137uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
138 if (index >= bindings_.size()) return 0;
139 return bindings_[index].descriptorCount;
140}
141// For the given index, return descriptorType
142VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
143 assert(index < bindings_.size());
144 if (index < bindings_.size()) return bindings_[index].descriptorType;
145 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
146}
147// For the given index, return stageFlags
148VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
149 assert(index < bindings_.size());
150 if (index < bindings_.size()) return bindings_[index].stageFlags;
151 return VkShaderStageFlags(0);
152}
153
154// For the given global index, return index
155uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromGlobalIndex(const uint32_t global_index) const {
156 auto start_it = global_start_to_index_map_.upper_bound(global_index);
157 uint32_t index = binding_count_;
158 assert(start_it != global_start_to_index_map_.cbegin());
159 if (start_it != global_start_to_index_map_.cbegin()) {
160 --start_it;
161 index = start_it->second;
162#ifndef NDEBUG
163 const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
164 assert(range.start <= global_index && global_index < range.end);
165#endif
166 }
167 return index;
168}
169
170// For the given binding, return the global index range
171// As start and end are often needed in pairs, get both with a single hash lookup.
172const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
173 const uint32_t binding) const {
174 assert(binding_to_global_index_range_map_.count(binding));
175 // In error case max uint32_t so index is out of bounds to break ASAP
176 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
177 const auto &range_it = binding_to_global_index_range_map_.find(binding);
178 if (range_it != binding_to_global_index_range_map_.end()) {
179 return range_it->second;
180 }
181 return kInvalidRange;
182}
183
184// For given binding, return ptr to ImmutableSampler array
185VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
186 const auto &bi_itr = binding_to_index_map_.find(binding);
187 if (bi_itr != binding_to_index_map_.end()) {
188 return bindings_[bi_itr->second].pImmutableSamplers;
189 }
190 return nullptr;
191}
192// Move to next valid binding having a non-zero binding count
193uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
194 auto it = non_empty_bindings_.upper_bound(binding);
195 assert(it != non_empty_bindings_.cend());
196 if (it != non_empty_bindings_.cend()) return *it;
197 return GetMaxBinding() + 1;
198}
199// For given index, return ptr to ImmutableSampler array
200VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
201 if (index < bindings_.size()) {
202 return bindings_[index].pImmutableSamplers;
203 }
204 return nullptr;
205}
206// If our layout is compatible with rh_ds_layout, return true,
207// else return false and fill in error_msg will description of what causes incompatibility
208bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
209 std::string *error_msg) const {
210 // Trivial case
211 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
John Zulaufd47d0612018-02-16 13:00:34 -0700212 if (get_layout_def() == rh_ds_layout->get_layout_def()) return true;
213 bool detailed_compat_check =
214 get_layout_def()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->get_layout_def(), error_msg);
215 // The detailed check should never tell us mismatching DSL are compatible
216 assert(!detailed_compat_check);
217 return detailed_compat_check;
John Zulauf1f8174b2018-02-16 12:58:37 -0700218}
219
John Zulaufdf3c5c12018-03-06 16:44:43 -0700220// Do a detailed compatibility check of this def (referenced by ds_layout), vs. the rhs (layout and def)
221// Should only be called if trivial accept has failed, and in that context should return false.
John Zulauf1f8174b2018-02-16 12:58:37 -0700222bool cvdescriptorset::DescriptorSetLayoutDef::IsCompatible(VkDescriptorSetLayout ds_layout, VkDescriptorSetLayout rh_ds_layout,
223 DescriptorSetLayoutDef const *const rh_ds_layout_def,
224 std::string *error_msg) const {
225 if (descriptor_count_ != rh_ds_layout_def->descriptor_count_) {
226 std::stringstream error_str;
227 error_str << "DescriptorSetLayout " << ds_layout << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
228 << rh_ds_layout << ", which comes from pipelineLayout, has " << rh_ds_layout_def->descriptor_count_
229 << " descriptors.";
230 *error_msg = error_str.str();
231 return false; // trivial fail case
232 }
John Zulaufd47d0612018-02-16 13:00:34 -0700233
John Zulauf1f8174b2018-02-16 12:58:37 -0700234 // Descriptor counts match so need to go through bindings one-by-one
235 // and verify that type and stageFlags match
236 for (auto binding : bindings_) {
237 // TODO : Do we also need to check immutable samplers?
238 // VkDescriptorSetLayoutBinding *rh_binding;
239 if (binding.descriptorCount != rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding)) {
240 std::stringstream error_str;
241 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has a descriptorCount of "
242 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
243 << rh_ds_layout << ", which comes from pipelineLayout, has a descriptorCount of "
244 << rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding);
245 *error_msg = error_str.str();
246 return false;
247 } else if (binding.descriptorType != rh_ds_layout_def->GetTypeFromBinding(binding.binding)) {
248 std::stringstream error_str;
249 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " is type '"
250 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
251 << " for DescriptorSetLayout " << rh_ds_layout << ", which comes from pipelineLayout, is type '"
252 << string_VkDescriptorType(rh_ds_layout_def->GetTypeFromBinding(binding.binding)) << "'";
253 *error_msg = error_str.str();
254 return false;
255 } else if (binding.stageFlags != rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding)) {
256 std::stringstream error_str;
257 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has stageFlags "
258 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " << rh_ds_layout
259 << ", which comes from pipelineLayout, has stageFlags "
260 << rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding);
261 *error_msg = error_str.str();
262 return false;
263 }
264 }
265 return true;
266}
267
268bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
269 if (!binding_to_index_map_.count(binding + 1)) return false;
270 auto const &bi_itr = binding_to_index_map_.find(binding);
271 if (bi_itr != binding_to_index_map_.end()) {
272 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
273 if (next_bi_itr != binding_to_index_map_.end()) {
274 auto type = bindings_[bi_itr->second].descriptorType;
275 auto stage_flags = bindings_[bi_itr->second].stageFlags;
276 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
277 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
278 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
279 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
280 return false;
281 }
282 return true;
283 }
284 }
285 return false;
286}
287// Starting at offset descriptor of given binding, parse over update_count
288// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
289// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
290// If so, return true. If not, fill in error_msg and return false
291bool cvdescriptorset::DescriptorSetLayoutDef::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset,
292 uint32_t update_count, const char *type,
293 const VkDescriptorSet set, std::string *error_msg) const {
294 // Verify consecutive bindings match (if needed)
295 auto orig_binding = current_binding;
296 // Track count of descriptors in the current_bindings that are remaining to be updated
297 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
298 // First, it's legal to offset beyond your own binding so handle that case
299 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
300 while (offset >= binding_remaining) {
301 // Advance to next binding, decrement offset by binding size
302 offset -= binding_remaining;
303 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
304 }
305 binding_remaining -= offset;
306 while (update_count > binding_remaining) { // While our updates overstep current binding
307 // Verify next consecutive binding matches type, stage flags & immutable sampler use
308 if (!IsNextBindingConsistent(current_binding++)) {
309 std::stringstream error_str;
310 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
311 << update_count
312 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
313 "not consistent with current binding so this update is invalid.";
314 *error_msg = error_str.str();
315 return false;
316 }
317 // For sake of this check consider the bindings updated and grab count for next binding
318 update_count -= binding_remaining;
319 binding_remaining = GetDescriptorCountFromBinding(current_binding);
320 }
321 return true;
322}
323
324// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
325// handle invariant portion
326cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
327 const VkDescriptorSetLayout layout)
John Zulauf34ebf272018-02-16 13:08:47 -0700328 : layout_(layout), layout_destroyed_(false), layout_id_(get_canonical_id(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700329
Tobin Ehlis154c2692016-10-25 09:36:53 -0600330// Validate descriptor set layout create info
John Zulauf0fdeab32018-01-23 11:27:35 -0700331bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(const debug_report_data *report_data,
332 const VkDescriptorSetLayoutCreateInfo *create_info,
333 const bool push_descriptor_ext, const uint32_t max_push_descriptors) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600334 bool skip = false;
335 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700336 uint64_t total_descriptors = 0;
337
338 const bool push_descriptor_set = create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR;
339 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600340 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600341 DRAWSTATE_EXTENSION_NOT_ENABLED,
John Zulauf0fdeab32018-01-23 11:27:35 -0700342 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
343 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
344 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
345 }
346
347 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
348 return !push_descriptor_set ||
349 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC));
350 };
351
Tobin Ehlis154c2692016-10-25 09:36:53 -0600352 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700353 const auto &binding_info = create_info->pBindings[i];
354 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600355 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600356 VALIDATION_ERROR_0500022e, "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600357 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700358 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600359 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600360 VALIDATION_ERROR_05000230,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600361 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
362 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700363 }
364 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600365 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700366
367 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
368 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600369 skip |= log_msg(
Mark Lobodzinski88529492018-04-01 10:38:15 -0600370 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, VALIDATION_ERROR_05000232,
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600371 "for push descriptor, total descriptor count in layout (%" PRIu64
372 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
373 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700374 }
375
Tobin Ehlis154c2692016-10-25 09:36:53 -0600376 return skip;
377}
378
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600379cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
380 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
381
Tobin Ehlis93f22372016-10-12 14:34:12 -0600382cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
John Zulauf48a6a702017-12-22 17:14:54 -0700383 const std::shared_ptr<DescriptorSetLayout const> &layout, layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700384 : some_update_(false),
385 set_(set),
386 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600387 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700388 device_data_(dev_data),
Józef Kucia1bb00972017-09-10 11:24:08 +0200389 limits_(GetPhysDevProperties(dev_data)->properties.limits) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700390 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600391 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700392 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600393 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
394 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600395 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700396 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600397 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
398 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600399 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700400 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600401 some_update_ = true; // Immutable samplers are updated at creation
402 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700403 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700404 }
405 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600406 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700407 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600408 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
409 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600410 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700411 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600412 some_update_ = true; // Immutable samplers are updated at creation
413 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700414 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700415 }
416 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600417 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700418 // ImageDescriptors
419 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
420 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
421 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600422 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700423 descriptors_.emplace_back(new ImageDescriptor(type));
424 break;
425 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
426 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600427 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700428 descriptors_.emplace_back(new TexelDescriptor(type));
429 break;
430 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
431 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
432 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
433 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600434 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700435 descriptors_.emplace_back(new BufferDescriptor(type));
436 break;
437 default:
438 assert(0); // Bad descriptor type specified
439 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600440 }
441 }
442}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600443
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700444cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200445
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700446static std::string string_descriptor_req_view_type(descriptor_req req) {
447 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200448 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
449 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700450 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700451 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200452 }
453 }
454
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700455 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700456
457 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200458}
459
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600460// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600461bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
462 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600463}
Chris Forbes57989132016-07-26 17:06:10 +1200464
Tobin Ehlis3066db62016-08-22 08:12:23 -0600465// Validate that the state of this set is appropriate for the given bindings and dynamic_offsets at Draw time
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600466// This includes validating that all descriptors in the given bindings are updated,
467// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
468// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600469bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
John Zulauf48a6a702017-12-22 17:14:54 -0700470 const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600471 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200472 for (auto binding_pair : bindings) {
473 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600474 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600475 std::stringstream error_str;
476 error_str << "Attempting to validate DrawState for binding #" << binding
477 << " which is an invalid binding for this descriptor set.";
478 *error = error_str.str();
479 return false;
480 }
John Zulaufc483f442017-12-15 14:02:06 -0700481 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700482 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
John Zulaufc483f442017-12-15 14:02:06 -0700483 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700484 if (!descriptors_[i]->updated) {
485 std::stringstream error_str;
486 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
487 << " is being used in draw but has not been updated.";
488 *error = error_str.str();
489 return false;
490 } else {
491 auto descriptor_class = descriptors_[i]->GetClass();
492 if (descriptor_class == GeneralBuffer) {
493 // Verify that buffers are valid
494 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
495 auto buffer_node = GetBufferState(device_data_, buffer);
496 if (!buffer_node) {
497 std::stringstream error_str;
498 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
499 << " references invalid buffer " << buffer << ".";
500 *error = error_str.str();
501 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700502 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700503 for (auto mem_binding : buffer_node->GetBoundMemory()) {
504 if (!GetMemObjInfo(device_data_, mem_binding)) {
505 std::stringstream error_str;
506 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
507 << " uses buffer " << buffer << " that references invalid memory " << mem_binding << ".";
508 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600509 return false;
510 }
511 }
John Zulauf48a6a702017-12-22 17:14:54 -0700512 } else {
513 // Enqueue sparse resource validation, as these can only be validated at submit time
514 auto device_data_copy = device_data_; // Cannot capture members by value, so make capturable copy.
515 std::function<bool(void)> function = [device_data_copy, caller, buffer_node]() {
516 return core_validation::ValidateBufferMemoryIsValid(device_data_copy, buffer_node, caller);
517 };
518 cb_node->queue_submit_functions.push_back(function);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700519 }
520 if (descriptors_[i]->IsDynamic()) {
521 // Validate that dynamic offsets are within the buffer
522 auto buffer_size = buffer_node->createInfo.size;
523 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
524 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
525 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
526 if (VK_WHOLE_SIZE == range) {
527 if ((dyn_offset + desc_offset) > buffer_size) {
528 std::stringstream error_str;
529 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
530 << " uses buffer " << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset "
531 << dyn_offset << " combined with offset " << desc_offset
532 << " that oversteps the buffer size of " << buffer_size << ".";
533 *error = error_str.str();
534 return false;
535 }
536 } else {
537 if ((dyn_offset + desc_offset + range) > buffer_size) {
538 std::stringstream error_str;
539 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
540 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
541 << " combined with offset " << desc_offset << " and range " << range
542 << " that oversteps the buffer size of " << buffer_size << ".";
543 *error = error_str.str();
544 return false;
545 }
546 }
547 }
548 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
549 VkImageView image_view;
550 VkImageLayout image_layout;
551 if (descriptor_class == ImageSampler) {
552 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
553 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
554 } else {
555 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
556 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
557 }
558 auto reqs = binding_pair.second;
559
560 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600561 if (nullptr == image_view_state) {
562 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
563 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
564 std::stringstream error_str;
565 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
566 << " is using imageView " << image_view << " that has been destroyed.";
567 *error = error_str.str();
568 return false;
569 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700570 auto image_view_ci = image_view_state->create_info;
571
572 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
573 // bad view type
574 std::stringstream error_str;
575 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
576 << " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
577 << string_VkImageViewType(image_view_ci.viewType) << ".";
578 *error = error_str.str();
579 return false;
580 }
581
582 auto image_node = GetImageState(device_data_, image_view_ci.image);
583 assert(image_node);
584 // Verify Image Layout
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700585 // Copy first mip level into sub_layers and loop over each mip level to verify layout
586 VkImageSubresourceLayers sub_layers;
587 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
588 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
589 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
590 bool hit_error = false;
591 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
592 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
593 sub_layers.mipLevel = cur_level;
594 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
Chris Forbesc6bb6ce2017-07-21 14:03:06 -0700595 caller, VALIDATION_ERROR_046002b0, &hit_error);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700596 if (hit_error) {
597 *error =
Dave Houltona9df0ce2018-02-07 10:51:23 -0700598 "Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at time "
599 "descriptor is used. See previous error callback for specific details.";
Chris Forbes57989132016-07-26 17:06:10 +1200600 return false;
601 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700602 }
603 // Verify Sample counts
604 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
605 std::stringstream error_str;
606 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
607 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
608 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
609 *error = error_str.str();
610 return false;
611 }
612 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
613 std::stringstream error_str;
614 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
615 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
616 *error = error_str.str();
617 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200618 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600619 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600620 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
621 // Verify Sampler still valid
622 VkSampler sampler;
623 if (descriptor_class == ImageSampler) {
624 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
625 } else {
626 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
627 }
628 if (!ValidateSampler(sampler, device_data_)) {
629 std::stringstream error_str;
630 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
631 << " is using sampler " << sampler << " that has been destroyed.";
632 *error = error_str.str();
633 return false;
634 }
635 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600636 }
637 }
638 }
639 return true;
640}
Chris Forbes57989132016-07-26 17:06:10 +1200641
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600642// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600643uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600644 std::unordered_set<VkBuffer> *buffer_set,
645 std::unordered_set<VkImageView> *image_set) const {
646 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200647 for (auto binding_pair : bindings) {
648 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600649 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600650 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600651 continue;
652 }
John Zulaufc483f442017-12-15 14:02:06 -0700653 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600654 if (descriptors_[start_idx]->IsStorage()) {
655 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600656 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600657 if (descriptors_[start_idx + i]->updated) {
658 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600659 num_updates++;
660 }
661 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600662 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600663 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600664 if (descriptors_[start_idx + i]->updated) {
665 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700666 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600667 if (bv_state) {
668 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600669 num_updates++;
670 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600671 }
672 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600673 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600674 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600675 if (descriptors_[start_idx + i]->updated) {
676 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600677 num_updates++;
678 }
679 }
680 }
681 }
682 }
683 return num_updates;
684}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600685// Set is being deleted or updates so invalidate all bound cmd buffers
686void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Petr Krausbc7f5442017-05-14 23:43:38 +0200687 core_validation::invalidateCommandBuffers(device_data_, cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600688}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600689// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600690void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700691 // Perform update on a per-binding basis as consecutive updates roll over to next binding
692 auto descriptors_remaining = update->descriptorCount;
693 auto binding_being_updated = update->dstBinding;
694 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600695 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700696 while (descriptors_remaining) {
697 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700698 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700699 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600700 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
701 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700702 }
703 // Roll over to next binding in case of consecutive update
704 descriptors_remaining -= update_count;
705 offset = 0;
706 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600707 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700708 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600709
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600710 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600711}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600712// Validate Copy update
713bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600714 const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
715 std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700716 // Verify dst layout still valid
717 if (p_layout_->IsDestroyed()) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700718 *error_code = VALIDATION_ERROR_03207601;
719 string_sprintf(error_msg,
720 "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set dstSet 0x%" PRIxLEAST64
721 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
722 HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
723 return false;
724 }
725
726 // Verify src layout still valid
727 if (src_set->p_layout_->IsDestroyed()) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700728 *error_code = VALIDATION_ERROR_0322d201;
729 string_sprintf(
730 error_msg,
731 "Cannot call vkUpdateDescriptorSets() to perform copy update of dstSet 0x%" PRIxLEAST64
732 " from descriptor set srcSet 0x%" PRIxLEAST64 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
733 HandleToUint64(set_), HandleToUint64(src_set->set_), HandleToUint64(src_set->p_layout_->GetDescriptorSetLayout()));
734 return false;
735 }
736
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600737 // Verify idle ds
738 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700739 // TODO : Re-using Free Idle error code, need copy update idle error code
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600740 *error_code = VALIDATION_ERROR_2860026a;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600741 std::stringstream error_str;
742 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700743 << " that is in use by a command buffer";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600744 *error_msg = error_str.str();
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600745 return false;
746 }
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600747 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600748 *error_code = VALIDATION_ERROR_032002b6;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600749 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700750 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600751 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600752 return false;
753 }
754 if (!src_set->HasBinding(update->srcBinding)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600755 *error_code = VALIDATION_ERROR_032002b2;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600756 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700757 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600758 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600759 return false;
760 }
761 // src & dst set bindings are valid
762 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -0700763 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600764 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
765 // SRC update out of bounds
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600766 *error_code = VALIDATION_ERROR_032002b4;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600767 std::stringstream error_str;
768 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -0700769 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600770 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700771 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600772 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600773 return false;
774 }
John Zulaufc483f442017-12-15 14:02:06 -0700775 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600776 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600777 // DST update out of bounds
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600778 *error_code = VALIDATION_ERROR_032002b8;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600779 std::stringstream error_str;
780 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -0700781 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600782 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600783 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600784 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600785 return false;
786 }
787 // Check that types match
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600788 // TODO : Base default error case going from here is VALIDATION_ERROR_0002b8012ba which covers all consistency issues, need more
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600789 // fine-grained error codes
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600790 *error_code = VALIDATION_ERROR_032002ba;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600791 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600792 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600793 if (src_type != dst_type) {
794 std::stringstream error_str;
795 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
796 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700797 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600798 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600799 return false;
800 }
801 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600802 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600803 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600804 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600805 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600806 return false;
807 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600808 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700809 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600810
811 // All checks passed so update is good
812 return true;
813}
814// Perform Copy update
815void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -0700816 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
817 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600818 // Update parameters all look good so perform update
819 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +0200820 auto src = src_set->descriptors_[src_start_idx + di].get();
821 auto dst = descriptors_[dst_start_idx + di].get();
822 if (src->updated) {
823 dst->CopyUpdate(src);
824 some_update_ = true;
825 } else {
826 dst->updated = false;
827 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600828 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600829
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600830 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600831}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600832
Tobin Ehlisf9519102016-08-17 09:49:13 -0600833// Bind cb_node to this set and this set to cb_node.
834// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
835// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -0700836void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -0700837 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600838 // bind cb to this descriptor set
839 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600840 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +0200841 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600842 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +0200843 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600844 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
845 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -0700846 for (auto binding_req_pair : binding_req_map) {
847 auto binding = binding_req_pair.first;
John Zulaufc483f442017-12-15 14:02:06 -0700848 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
849 for (uint32_t i = range.start; i < range.end; ++i) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600850 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
851 }
852 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600853}
John Zulauf48a6a702017-12-22 17:14:54 -0700854void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
855 const BindingReqMap &in_req, BindingReqMap *out_req,
856 TrackedBindings *bindings) {
857 assert(out_req);
858 assert(bindings);
859 const auto binding = binding_req_pair.first;
860 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
861 // Saves one hash lookup vs. find ... compare w/ end ... insert.
862 const auto it_bool_pair = bindings->insert(binding);
863 if (it_bool_pair.second) {
864 out_req->emplace(binding_req_pair);
865 }
866}
867void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
868 const BindingReqMap &in_req, BindingReqMap *out_req,
869 TrackedBindings *bindings, uint32_t limit) {
870 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
871}
872
873void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
874 BindingReqMap *out_req) {
875 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
876 if (bound.size() == GetBindingCount()) {
877 return; // All bindings are bound, out req is empty
878 }
879 for (const auto &binding_req_pair : in_req) {
880 const auto binding = binding_req_pair.first;
881 // If a binding doesn't exist, or has already been bound, skip it
882 if (p_layout_->HasBinding(binding)) {
883 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
884 }
885 }
886}
887
888void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
889 const BindingReqMap &in_req, BindingReqMap *out_req) {
890 auto &validated = cached_validation_[cb_state];
891 auto &image_sample_val = validated.image_samplers[pipeline];
892 auto *const dynamic_buffers = &validated.dynamic_buffers;
893 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
894 const auto &stats = p_layout_->GetBindingTypeStats();
895 for (const auto &binding_req_pair : in_req) {
896 auto binding = binding_req_pair.first;
897 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
898 if (!layout_binding) {
899 continue;
900 }
901 // Caching criteria differs per type.
902 // If image_layout have changed , the image descriptors need to be validated against them.
903 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
904 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
905 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
906 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
907 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
908 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
909 } else {
910 // This is rather crude, as the changed layouts may not impact the bound descriptors,
911 // but the simple "versioning" is a simple "dirt" test.
912 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
913 if (version != cb_state->image_layout_change_count) {
914 version = cb_state->image_layout_change_count;
915 out_req->emplace(binding_req_pair);
916 }
917 }
918 }
919}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600920
Tobin Ehlis300888c2016-05-18 13:43:26 -0600921cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600922 updated = false;
923 descriptor_class = PlainSampler;
924 if (immut) {
925 sampler_ = *immut;
926 immutable_ = true;
927 updated = true;
928 }
929}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600930// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700931bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700932 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600933}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600934
Tobin Ehlis554bf382016-05-24 11:14:43 -0600935bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700936 const layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600937 std::string *error_msg) {
938 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600939 *error_code = VALIDATION_ERROR_15c0028c;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700940 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600941 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600942 std::stringstream error_str;
943 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600944 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600945 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600946 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600947 // Note that when an imageview is created, we validated that memory is bound so no need to re-check here
Tobin Ehlis1809f912016-05-25 09:24:36 -0600948 // Validate that imageLayout is compatible with aspect_mask and image format
949 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600950 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
951 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600952 VkFormat format = VK_FORMAT_MAX_ENUM;
953 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700954 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600955 if (image_node) {
956 format = image_node->createInfo.format;
957 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600958 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700959 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
960 // the error here occurs is if memory bound to a created imageView has been freed.
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600961 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_0ac007f8)) {
962 *error_code = VALIDATION_ERROR_0ac007f8;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600963 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600964 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600965 }
Chris Forbes67757ff2017-07-21 13:59:01 -0700966
967 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
968 // but not binding them to descriptor sets.
969 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
970 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
971 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
972 *error_code = VALIDATION_ERROR_046002ae;
973 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
974 return false;
975 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600976 }
977 // First validate that format and layout are compatible
978 if (format == VK_FORMAT_MAX_ENUM) {
979 std::stringstream error_str;
980 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600981 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600982 return false;
983 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600984 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
985 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -0600986 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -0600987 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700988 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
989 // Only Color bit must be set
990 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600991 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -0700992 error_str
993 << "ImageView (" << image_view
994 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600995 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600996 return false;
997 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700998 // format must NOT be DS
999 if (ds) {
1000 std::stringstream error_str;
1001 error_str << "ImageView (" << image_view
1002 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1003 << string_VkFormat(format) << " which is not a color format.";
1004 *error_msg = error_str.str();
1005 return false;
1006 }
1007 break;
1008 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1009 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1010 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001011 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1012 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1013 // both must NOT be set
1014 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001015 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001016 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001017 return false;
1018 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001019 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1020 // Neither were set
1021 std::stringstream error_str;
1022 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1023 << " but does not have STENCIL or DEPTH aspects set";
1024 *error_msg = error_str.str();
1025 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001026 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001027 // format must be DS
1028 if (!ds) {
1029 std::stringstream error_str;
1030 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1031 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1032 *error_msg = error_str.str();
1033 return false;
1034 }
1035 break;
1036 default:
1037 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1038 if (ds) {
1039 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1040 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1041 // both must NOT be set
1042 std::stringstream error_str;
1043 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1044 << " and is using depth/stencil image of format " << string_VkFormat(format)
1045 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1046 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1047 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1048 "reads respectively.";
1049 *error_msg = error_str.str();
1050 return false;
1051 }
1052 }
1053 }
1054 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001055 }
1056 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001057 // As we're switching per-type, if any type has specific layout requirements, check those here as well
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001058 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1059 // under vkCreateImage()
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001060 // TODO : Need to also validate case VALIDATION_ERROR_15c002a0 where STORAGE_IMAGE & INPUT_ATTACH types must have been created
1061 // with identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -06001062 std::string error_usage_bit;
1063 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001064 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1065 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1066 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1067 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1068 }
1069 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001070 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001071 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1072 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1073 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1074 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1075 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001076 // TODO : Need to create custom enum error codes for these cases
1077 if (image_node->shared_presentable) {
1078 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001079 error_str << "ImageView (" << image_view
1080 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1081 "layout "
1082 << string_VkImageLayout(image_layout)
1083 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1084 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1085 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001086 *error_msg = error_str.str();
1087 return false;
1088 }
1089 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001090 error_str << "ImageView (" << image_view
1091 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1092 << string_VkImageLayout(image_layout)
1093 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1094 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001095 *error_msg = error_str.str();
1096 return false;
1097 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001098 }
1099 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001100 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001101 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1102 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1103 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1104 }
1105 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001106 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001107 default:
1108 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001109 }
1110 if (!error_usage_bit.empty()) {
1111 std::stringstream error_str;
1112 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1113 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1114 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001115 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001116 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001117 }
1118 return true;
1119}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001120
Tobin Ehlis300888c2016-05-18 13:43:26 -06001121void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
1122 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001123 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001124}
1125
Tobin Ehlis300888c2016-05-18 13:43:26 -06001126void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001127 if (!immutable_) {
1128 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001129 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001130 }
1131 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001132}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001133
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001134void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001135 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001136 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001137 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001138 }
1139}
1140
Tobin Ehlis300888c2016-05-18 13:43:26 -06001141cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001142 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001143 updated = false;
1144 descriptor_class = ImageSampler;
1145 if (immut) {
1146 sampler_ = *immut;
1147 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001148 }
1149}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001150
Tobin Ehlis300888c2016-05-18 13:43:26 -06001151void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001152 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001153 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001154 sampler_ = image_info.sampler;
1155 image_view_ = image_info.imageView;
1156 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001157}
1158
Tobin Ehlis300888c2016-05-18 13:43:26 -06001159void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001160 if (!immutable_) {
1161 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001162 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001163 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001164 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1165 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001166 updated = true;
1167 image_view_ = image_view;
1168 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001169}
1170
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001171void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001172 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001173 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001174 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001175 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001176 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001177 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001178 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001179 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001180 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001181 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001182}
1183
Tobin Ehlis300888c2016-05-18 13:43:26 -06001184cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1185 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001186 updated = false;
1187 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001188 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001189}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001190
Tobin Ehlis300888c2016-05-18 13:43:26 -06001191void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001192 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001193 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001194 image_view_ = image_info.imageView;
1195 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001196}
1197
Tobin Ehlis300888c2016-05-18 13:43:26 -06001198void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001199 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1200 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001201 updated = true;
1202 image_view_ = image_view;
1203 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001204}
1205
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001206void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001207 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001208 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001209 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001210 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001211 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001212}
1213
Tobin Ehlis300888c2016-05-18 13:43:26 -06001214cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1215 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001216 updated = false;
1217 descriptor_class = GeneralBuffer;
1218 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1219 dynamic_ = true;
1220 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1221 storage_ = true;
1222 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1223 dynamic_ = true;
1224 storage_ = true;
1225 }
1226}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001227void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001228 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001229 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001230 buffer_ = buffer_info.buffer;
1231 offset_ = buffer_info.offset;
1232 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001233}
1234
Tobin Ehlis300888c2016-05-18 13:43:26 -06001235void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1236 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001237 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001238 buffer_ = buff_desc->buffer_;
1239 offset_ = buff_desc->offset_;
1240 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001241}
1242
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001243void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001244 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001245 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001246}
1247
Tobin Ehlis300888c2016-05-18 13:43:26 -06001248cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001249 updated = false;
1250 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001251 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001252}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001253
Tobin Ehlis300888c2016-05-18 13:43:26 -06001254void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001255 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001256 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001257}
1258
Tobin Ehlis300888c2016-05-18 13:43:26 -06001259void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1260 updated = true;
1261 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1262}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001263
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001264void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001265 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001266 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001267 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001268 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001269}
1270
Tobin Ehlis300888c2016-05-18 13:43:26 -06001271// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1272// sets, and then calls their respective Validate[Write|Copy]Update functions.
1273// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1274// be skipped, then true is returned.
1275// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001276bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1277 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001278 const VkCopyDescriptorSet *p_cds) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001279 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001280 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001281 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001282 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001283 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001284 if (!set_node) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001285 skip |=
Tobin Ehlis300888c2016-05-18 13:43:26 -06001286 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -06001287 HandleToUint64(dest_set), DRAWSTATE_INVALID_DESCRIPTOR_SET,
Tobin Ehlis300888c2016-05-18 13:43:26 -06001288 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
Petr Krausbc7f5442017-05-14 23:43:38 +02001289 HandleToUint64(dest_set));
Tobin Ehlis300888c2016-05-18 13:43:26 -06001290 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001291 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001292 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001293 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001294 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -06001295 HandleToUint64(dest_set), error_code,
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01001296 "vkUpdateDescriptorSets() failed write update validation for Descriptor Set 0x%" PRIx64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001297 " with error: %s.",
1298 HandleToUint64(dest_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001299 }
1300 }
1301 }
1302 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001303 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001304 auto dst_set = p_cds[i].dstSet;
1305 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001306 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1307 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001308 // Object_tracker verifies that src & dest descriptor set are valid
1309 assert(src_node);
1310 assert(dst_node);
1311 UNIQUE_VALIDATION_ERROR_CODE error_code;
1312 std::string error_str;
1313 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001314 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -06001315 HandleToUint64(dst_set), error_code,
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01001316 "vkUpdateDescriptorSets() failed copy update from Descriptor Set 0x%" PRIx64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001317 " to Descriptor Set 0x%" PRIx64 " with error: %s.",
1318 HandleToUint64(src_set), HandleToUint64(dst_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001319 }
1320 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001321 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001322}
1323// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1324// sets, and then calls their respective Perform[Write|Copy]Update functions.
1325// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1326// with the same set of updates.
1327// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1328// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001329void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001330 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1331 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001332 // Write updates first
1333 uint32_t i = 0;
1334 for (i = 0; i < write_count; ++i) {
1335 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001336 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001337 if (set_node) {
1338 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001339 }
1340 }
1341 // Now copy updates
1342 for (i = 0; i < copy_count; ++i) {
1343 auto dst_set = p_cds[i].dstSet;
1344 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001345 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1346 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001347 if (src_node && dst_node) {
1348 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001349 }
1350 }
1351}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001352// This helper function carries out the state updates for descriptor updates peformed via update templates. It basically collects
1353// data and leverages the PerformUpdateDescriptor helper functions to do this.
1354void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1355 std::unique_ptr<TEMPLATE_STATE> const &template_state,
1356 const void *pData) {
1357 auto const &create_info = template_state->create_info;
1358
1359 // Create a vector of write structs
1360 std::vector<VkWriteDescriptorSet> desc_writes;
1361 auto layout_obj = GetDescriptorSetLayout(device_data, create_info.descriptorSetLayout);
1362
1363 // Create a WriteDescriptorSet struct for each template update entry
1364 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1365 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1366 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1367 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1368
John Zulaufb6d71202017-12-22 16:47:09 -07001369 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001370 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1371 desc_writes.emplace_back();
1372 auto &write_entry = desc_writes.back();
1373
1374 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1375 char *update_entry = (char *)(pData) + offset;
1376
1377 if (dst_array_element >= binding_count) {
1378 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001379 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001380 }
1381
1382 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1383 write_entry.pNext = NULL;
1384 write_entry.dstSet = descriptorSet;
1385 write_entry.dstBinding = binding_being_updated;
1386 write_entry.dstArrayElement = dst_array_element;
1387 write_entry.descriptorCount = 1;
1388 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1389
1390 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1391 case VK_DESCRIPTOR_TYPE_SAMPLER:
1392 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1393 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1394 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1395 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1396 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1397 break;
1398
1399 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1400 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1401 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1402 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1403 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1404 break;
1405
1406 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1407 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1408 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1409 break;
1410 default:
1411 assert(0);
1412 break;
1413 }
1414 dst_array_element++;
1415 }
1416 }
1417 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(desc_writes.size()), desc_writes.data(), 0, NULL);
1418}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001419// Validate the state for a given write update but don't actually perform the update
1420// If an error would occur for this update, return false and fill in details in error_msg string
1421bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001422 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001423 // Verify dst layout still valid
1424 if (p_layout_->IsDestroyed()) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001425 *error_code = VALIDATION_ERROR_15c00280;
1426 string_sprintf(error_msg,
1427 "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set 0x%" PRIxLEAST64
1428 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
1429 HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
1430 return false;
1431 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001432 // Verify idle ds
1433 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001434 // TODO : Re-using Free Idle error code, need write update idle error code
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001435 *error_code = VALIDATION_ERROR_2860026a;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001436 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001437 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001438 << " that is in use by a command buffer";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001439 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001440 return false;
1441 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001442 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001443 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001444 *error_code = VALIDATION_ERROR_15c00276;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001445 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001446 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001447 *error_msg = error_str.str();
1448 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001449 } else {
1450 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001451 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001452 *error_code = VALIDATION_ERROR_15c00278;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001453 std::stringstream error_str;
1454 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1455 *error_msg = error_str.str();
1456 return false;
1457 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001458 }
1459 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001460 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001461 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001462 if (type != update->descriptorType) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001463 *error_code = VALIDATION_ERROR_15c0027e;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001464 std::stringstream error_str;
1465 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1466 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1467 *error_msg = error_str.str();
1468 return false;
1469 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001470 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001471 *error_code = VALIDATION_ERROR_15c00282;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001472 std::stringstream error_str;
1473 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001474 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001475 << " descriptors in that binding and all successive bindings of the set, but update of "
1476 << update->descriptorCount << " descriptors combined with update array element offset of "
1477 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001478 *error_msg = error_str.str();
1479 return false;
1480 }
1481 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001482 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001483 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001484 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001485 *error_code = VALIDATION_ERROR_15c00282;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001486 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001487 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001488 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001489 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001490 std::stringstream error_str;
1491 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1492 << " failed with error message: " << error_msg->c_str();
1493 *error_msg = error_str.str();
1494 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001495 }
1496 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001497 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001498}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001499// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001500// If there's an error, update the error_msg string with details and return false, else return true
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001501bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001502 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001503 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001504 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001505 std::string error_usage_bit;
1506 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001507 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1508 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001509 *error_code = VALIDATION_ERROR_15c0029c;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001510 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1511 }
1512 break;
1513 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1514 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001515 *error_code = VALIDATION_ERROR_15c0029e;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001516 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1517 }
1518 break;
1519 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1520 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1521 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001522 *error_code = VALIDATION_ERROR_15c00292;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001523 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1524 }
1525 break;
1526 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1527 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1528 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001529 *error_code = VALIDATION_ERROR_15c00296;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001530 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1531 }
1532 break;
1533 default:
1534 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001535 }
1536 if (!error_usage_bit.empty()) {
1537 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001538 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1539 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1540 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001541 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001542 return false;
1543 }
1544 return true;
1545}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001546// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1547// 1. buffer is valid
1548// 2. buffer was created with correct usage flags
1549// 3. offset is less than buffer size
1550// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001551// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001552// If there's an error, update the error_msg string with details and return false, else return true
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001553bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001554 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001555 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001556 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001557 // Any invalid buffer should already be caught by object_tracker
1558 assert(buffer_node);
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001559 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_15c00294)) {
1560 *error_code = VALIDATION_ERROR_15c00294;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001561 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001562 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001563 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001564 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001565 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1566 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001567 return false;
1568 }
1569 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001570 if (buffer_info->offset >= buffer_node->createInfo.size) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001571 *error_code = VALIDATION_ERROR_044002a8;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001572 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001573 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
1574 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001575 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001576 return false;
1577 }
1578 if (buffer_info->range != VK_WHOLE_SIZE) {
1579 // Range must be VK_WHOLE_SIZE or > 0
1580 if (!buffer_info->range) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001581 *error_code = VALIDATION_ERROR_044002aa;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001582 std::stringstream error_str;
1583 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001584 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001585 return false;
1586 }
1587 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1588 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001589 *error_code = VALIDATION_ERROR_044002ac;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001590 std::stringstream error_str;
1591 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1592 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001593 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001594 return false;
1595 }
1596 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001597 // Check buffer update sizes against device limits
1598 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1599 auto max_ub_range = limits_.maxUniformBufferRange;
1600 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1601 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001602 *error_code = VALIDATION_ERROR_15c00298;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001603 std::stringstream error_str;
1604 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1605 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
1606 *error_msg = error_str.str();
1607 return false;
1608 }
1609 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1610 auto max_sb_range = limits_.maxStorageBufferRange;
1611 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1612 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001613 *error_code = VALIDATION_ERROR_15c0029a;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001614 std::stringstream error_str;
1615 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1616 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
1617 *error_msg = error_str.str();
1618 return false;
1619 }
1620 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001621 return true;
1622}
1623
Tobin Ehlis300888c2016-05-18 13:43:26 -06001624// Verify that the contents of the update are ok, but don't perform actual update
1625bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001626 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1627 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001628 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001629 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1630 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1631 // Validate image
1632 auto image_view = update->pImageInfo[di].imageView;
1633 auto image_layout = update->pImageInfo[di].imageLayout;
1634 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001635 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001636 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
1637 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001638 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001639 return false;
1640 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001641 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001642 // Intentional fall-through to validate sampler
Tobin Ehlis300888c2016-05-18 13:43:26 -06001643 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001644 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1645 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1646 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
1647 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001648 *error_code = VALIDATION_ERROR_15c0028a;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001649 std::stringstream error_str;
1650 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1651 << update->pImageInfo[di].sampler << ".";
1652 *error_msg = error_str.str();
1653 return false;
1654 }
1655 } else {
1656 // TODO : Warn here
1657 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001658 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001659 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001660 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001661 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1662 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1663 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1664 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1665 auto image_view = update->pImageInfo[di].imageView;
1666 auto image_layout = update->pImageInfo[di].imageLayout;
1667 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
1668 std::stringstream error_str;
1669 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1670 *error_msg = error_str.str();
1671 return false;
1672 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001673 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001674 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001675 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001676 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1677 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1678 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1679 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001680 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001681 if (!bv_state) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001682 *error_code = VALIDATION_ERROR_15c00286;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001683 std::stringstream error_str;
1684 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1685 *error_msg = error_str.str();
1686 return false;
1687 }
1688 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06001689 auto buffer_state = GetBufferState(device_data_, buffer);
1690 // Verify that buffer underlying the view hasn't been destroyed prematurely
1691 if (!buffer_state) {
1692 *error_code = VALIDATION_ERROR_15c00286;
1693 std::stringstream error_str;
1694 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
1695 << ") has been destroyed: " << error_msg->c_str();
1696 *error_msg = error_str.str();
1697 return false;
1698 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001699 std::stringstream error_str;
1700 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1701 *error_msg = error_str.str();
1702 return false;
1703 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001704 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001705 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001706 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001707 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1708 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1709 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1710 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1711 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1712 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
1713 std::stringstream error_str;
1714 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1715 *error_msg = error_str.str();
1716 return false;
1717 }
1718 }
1719 break;
1720 }
1721 default:
1722 assert(0); // We've already verified update type so should never get here
1723 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001724 }
1725 // All checks passed so update contents are good
1726 return true;
1727}
1728// Verify that the contents of the update are ok, but don't perform actual update
1729bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001730 VkDescriptorType type, uint32_t index,
1731 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1732 std::string *error_msg) const {
1733 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1734 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001735 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001736 case PlainSampler: {
1737 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001738 const auto src_desc = src_set->descriptors_[index + di].get();
1739 if (!src_desc->updated) continue;
1740 if (!src_desc->IsImmutableSampler()) {
1741 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001742 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001743 *error_code = VALIDATION_ERROR_15c0028a;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001744 std::stringstream error_str;
1745 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1746 *error_msg = error_str.str();
1747 return false;
1748 }
1749 } else {
1750 // TODO : Warn here
1751 }
1752 }
1753 break;
1754 }
1755 case ImageSampler: {
1756 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001757 const auto src_desc = src_set->descriptors_[index + di].get();
1758 if (!src_desc->updated) continue;
1759 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001760 // First validate sampler
1761 if (!img_samp_desc->IsImmutableSampler()) {
1762 auto update_sampler = img_samp_desc->GetSampler();
1763 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001764 *error_code = VALIDATION_ERROR_15c0028a;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001765 std::stringstream error_str;
1766 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1767 *error_msg = error_str.str();
1768 return false;
1769 }
1770 } else {
1771 // TODO : Warn here
1772 }
1773 // Validate image
1774 auto image_view = img_samp_desc->GetImageView();
1775 auto image_layout = img_samp_desc->GetImageLayout();
1776 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001777 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001778 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001779 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001780 return false;
1781 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001782 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001783 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001784 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001785 case Image: {
1786 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001787 const auto src_desc = src_set->descriptors_[index + di].get();
1788 if (!src_desc->updated) continue;
1789 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001790 auto image_view = img_desc->GetImageView();
1791 auto image_layout = img_desc->GetImageLayout();
1792 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001793 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001794 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001795 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001796 return false;
1797 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001798 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001799 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001800 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001801 case TexelBuffer: {
1802 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001803 const auto src_desc = src_set->descriptors_[index + di].get();
1804 if (!src_desc->updated) continue;
1805 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001806 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001807 if (!bv_state) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001808 *error_code = VALIDATION_ERROR_15c00286;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001809 std::stringstream error_str;
1810 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1811 *error_msg = error_str.str();
1812 return false;
1813 }
1814 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001815 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001816 std::stringstream error_str;
1817 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1818 *error_msg = error_str.str();
1819 return false;
1820 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001821 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001822 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001823 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001824 case GeneralBuffer: {
1825 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001826 const auto src_desc = src_set->descriptors_[index + di].get();
1827 if (!src_desc->updated) continue;
1828 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001829 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001830 std::stringstream error_str;
1831 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1832 *error_msg = error_str.str();
1833 return false;
1834 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001835 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001836 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001837 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001838 default:
1839 assert(0); // We've already verified update type so should never get here
1840 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001841 }
1842 // All checks passed so update contents are good
1843 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001844}
Tobin Ehlisf320b192017-03-14 11:22:50 -06001845// Update the common AllocateDescriptorSetsData
1846void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1847 AllocateDescriptorSetsData *ds_data) {
1848 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1849 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1850 if (layout) {
1851 ds_data->layout_nodes[i] = layout;
1852 // Count total descriptors required per type
1853 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1854 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1855 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1856 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1857 }
1858 }
1859 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
1860 }
Petr Kraus13c98a62017-12-09 00:22:39 +01001861}
Tobin Ehlisee471462016-05-26 11:21:59 -06001862// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06001863bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
1864 const VkDescriptorSetAllocateInfo *p_alloc_info,
1865 const AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001866 bool skip = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06001867 auto report_data = core_validation::GetReportData(dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001868
1869 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001870 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07001871 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
1872 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR) {
1873 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -06001874 HandleToUint64(p_alloc_info->pSetLayouts[i]), VALIDATION_ERROR_04c00268,
John Zulauf5562d062018-01-24 11:54:05 -07001875 "Layout 0x%" PRIxLEAST64 " specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001876 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
John Zulauf5562d062018-01-24 11:54:05 -07001877 HandleToUint64(p_alloc_info->pSetLayouts[i]), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001878 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07001879 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001880 }
1881 }
Mark Lobodzinski28426ae2017-06-01 07:56:38 -06001882 if (!GetDeviceExtensions(dev_data)->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001883 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
1884 // Track number of descriptorSets allowable in this pool
1885 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001886 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -06001887 HandleToUint64(pool_state->pool), VALIDATION_ERROR_04c00264,
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001888 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001889 ". This pool only has %d descriptorSets remaining.",
1890 p_alloc_info->descriptorSetCount, HandleToUint64(pool_state->pool), pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001891 }
1892 // Determine whether descriptor counts are satisfiable
1893 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1894 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001895 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -06001896 HandleToUint64(pool_state->pool), VALIDATION_ERROR_04c00266,
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001897 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001898 ". This pool only has %d descriptors of this type remaining.",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001899 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001900 HandleToUint64(pool_state->pool), pool_state->availableDescriptorTypeCount[i]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001901 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001902 }
1903 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001904
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001905 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06001906}
1907// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001908void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1909 const VkDescriptorSet *descriptor_sets,
1910 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001911 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001912 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
John Zulauf48a6a702017-12-22 17:14:54 -07001913 layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001914 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06001915 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06001916 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001917 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1918 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1919 }
Mark Lobodzinskic9430182017-06-13 13:00:05 -06001920 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06001921 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001922 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1923 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001924
1925 pool_state->sets.insert(new_ds);
1926 new_ds->in_use.store(0);
1927 (*set_map)[descriptor_sets[i]] = new_ds;
1928 }
1929}
John Zulauf48a6a702017-12-22 17:14:54 -07001930
1931cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
1932 GLOBAL_CB_NODE *cb_state)
1933 : filtered_map_(), orig_map_(in_map) {
1934 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
1935 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
1936 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
1937 }
1938}
1939cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
1940 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
1941 : filtered_map_(), orig_map_(in_map) {
1942 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
1943 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
1944 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
1945 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01001946}