blob: b12159e92e9a295eaae4011cdb4d1dbb37cc8205 [file] [log] [blame]
Jeremy Hayese4224ee2019-01-04 14:41:48 -07001/* Copyright (c) 2015-2019 The Khronos Group Inc.
2 * Copyright (c) 2015-2019 Valve Corporation
3 * Copyright (c) 2015-2019 LunarG, Inc.
4 * Copyright (C) 2015-2019 Google Inc.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -06005 *
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 Ehlis2d9deec2016-04-21 14:19:26 -060020 */
21#ifndef CORE_VALIDATION_DESCRIPTOR_SETS_H_
22#define CORE_VALIDATION_DESCRIPTOR_SETS_H_
23
John Zulaufd47d0612018-02-16 13:00:34 -070024#include "hash_vk_types.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060025#include "vk_layer_logging.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060026#include "vk_layer_utils.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060027#include "vk_safe_struct.h"
28#include "vulkan/vk_layer.h"
Mark Lobodzinski33826372017-04-13 11:10:11 -060029#include "vk_object_types.h"
Tobin Ehliscebc4c02016-08-22 10:10:43 -060030#include <map>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060031#include <memory>
John Zulaufb6d71202017-12-22 16:47:09 -070032#include <set>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060033#include <unordered_map>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060034#include <unordered_set>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060035#include <vector>
36
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070037class CoreChecks;
Tobin Ehlis58c884f2017-02-08 12:15:27 -070038
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060039// Descriptor Data structures
John Zulaufc483f442017-12-15 14:02:06 -070040namespace cvdescriptorset {
41
John Zulauf48a6a702017-12-22 17:14:54 -070042// Utility structs/classes/types
John Zulaufc483f442017-12-15 14:02:06 -070043// Index range for global indices below, end is exclusive, i.e. [start,end)
44struct IndexRange {
45 IndexRange(uint32_t start_in, uint32_t end_in) : start(start_in), end(end_in) {}
46 IndexRange() = default;
47 uint32_t start;
48 uint32_t end;
49};
John Zulauf48a6a702017-12-22 17:14:54 -070050typedef std::map<uint32_t, descriptor_req> BindingReqMap;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060051
52/*
John Zulauf34ebf272018-02-16 13:08:47 -070053 * DescriptorSetLayoutDef/DescriptorSetLayout classes
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060054 *
John Zulauf34ebf272018-02-16 13:08:47 -070055 * Overview - These two classes encapsulate the Vulkan VkDescriptorSetLayout data (layout).
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060056 * A layout consists of some number of bindings, each of which has a binding#, a
57 * type, descriptor count, stage flags, and pImmutableSamplers.
John Zulauf34ebf272018-02-16 13:08:47 -070058
59 * The DescriptorSetLayoutDef represents a canonicalization of the input data and contains
60 * neither per handle or per device state. It is possible for different handles on
61 * different devices to share a common def. This is used and useful for quick compatibiltiy
62 * validation. The DescriptorSetLayout refers to a DescriptorSetLayoutDef and contains
63 * all per handle state.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060064 *
65 * Index vs Binding - A layout is created with an array of VkDescriptorSetLayoutBinding
66 * where each array index will have a corresponding binding# that is defined in that struct.
Tobin Ehlis9637fb22016-12-12 15:59:34 -070067 * The binding#, then, is decoupled from VkDescriptorSetLayoutBinding index, which allows
68 * bindings to be defined out-of-order. This DescriptorSetLayout class, however, stores
69 * the bindings internally in-order. This is useful for operations which may "roll over"
70 * from a single binding to the next consecutive binding.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060071 *
Tobin Ehlis9637fb22016-12-12 15:59:34 -070072 * Note that although the bindings are stored in-order, there still may be "gaps" in the
73 * binding#. For example, if the binding creation order is 8, 7, 10, 3, 4, then the
74 * internal binding array will have five entries stored in binding order 3, 4, 7, 8, 10.
75 * To process all of the bindings in a layout you can iterate from 0 to GetBindingCount()
76 * and use the Get*FromIndex() functions for each index. To just process a single binding,
77 * use the Get*FromBinding() functions.
78 *
79 * Global Index - The binding vector index has as many indices as there are bindings.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060080 * This class also has the concept of a Global Index. For the global index functions,
81 * there are as many global indices as there are descriptors in the layout.
82 * For the global index, consider all of the bindings to be a flat array where
Tobin Ehlis9637fb22016-12-12 15:59:34 -070083 * descriptor 0 of of the lowest binding# is index 0 and each descriptor in the layout
84 * increments from there. So if the lowest binding# in this example had descriptorCount of
85 * 10, then the GlobalStartIndex of the 2nd lowest binding# will be 10 where 0-9 are the
86 * global indices for the lowest binding#.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060087 */
John Zulauf1f8174b2018-02-16 12:58:37 -070088class DescriptorSetLayoutDef {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070089 public:
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060090 // Constructors and destructor
John Zulauf1f8174b2018-02-16 12:58:37 -070091 DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info);
92 size_t hash() const;
93
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060094 uint32_t GetTotalDescriptorCount() const { return descriptor_count_; };
95 uint32_t GetDynamicDescriptorCount() const { return dynamic_descriptor_count_; };
Józef Kucia1bb00972017-09-10 11:24:08 +020096 VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return flags_; }
Tobin Ehlisf922ef82016-11-30 10:19:14 -070097 // For a given binding, return the number of descriptors in that binding and all successive bindings
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060098 uint32_t GetBindingCount() const { return binding_count_; };
John Zulaufb6d71202017-12-22 16:47:09 -070099 // Non-empty binding numbers in order
100 const std::set<uint32_t> &GetSortedBindingSet() const { return non_empty_bindings_; }
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600101 // Return true if given binding is present in this layout
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600102 bool HasBinding(const uint32_t binding) const { return binding_to_index_map_.count(binding) > 0; };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600103 // Return true if binding 1 beyond given exists and has same type, stageFlags & immutable sampler use
104 bool IsNextBindingConsistent(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700105 uint32_t GetIndexFromBinding(uint32_t binding) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600106 // Various Get functions that can either be passed a binding#, which will
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700107 // be automatically translated into the appropriate index, or the index# can be passed in directly
John Zulaufb6d71202017-12-22 16:47:09 -0700108 uint32_t GetMaxBinding() const { return bindings_[bindings_.size() - 1].binding; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600109 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700110 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(uint32_t binding) const {
111 return GetDescriptorSetLayoutBindingPtrFromIndex(GetIndexFromBinding(binding));
112 }
John Zulauf1f8174b2018-02-16 12:58:37 -0700113 const std::vector<safe_VkDescriptorSetLayoutBinding> &GetBindings() const { return bindings_; }
John Zulauf223b69d2018-11-09 16:00:59 -0700114 const std::vector<VkDescriptorBindingFlagsEXT> &GetBindingFlags() const { return binding_flags_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600115 uint32_t GetDescriptorCountFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700116 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
117 return GetDescriptorCountFromIndex(GetIndexFromBinding(binding));
118 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600119 VkDescriptorType GetTypeFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700120 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return GetTypeFromIndex(GetIndexFromBinding(binding)); }
121 VkShaderStageFlags GetStageFlagsFromIndex(const uint32_t) const;
122 VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t binding) const {
123 return GetStageFlagsFromIndex(GetIndexFromBinding(binding));
124 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500125 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromIndex(const uint32_t) const;
126 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromBinding(const uint32_t binding) const {
127 return GetDescriptorBindingFlagsFromIndex(GetIndexFromBinding(binding));
128 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600129 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t) const;
130 VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t) const;
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700131 // For a given binding and array index, return the corresponding index into the dynamic offset array
132 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
133 auto dyn_off = binding_to_dynamic_array_idx_map_.find(binding);
134 if (dyn_off == binding_to_dynamic_array_idx_map_.end()) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700135 assert(0); // Requesting dyn offset for invalid binding/array idx pair
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700136 return -1;
137 }
138 return dyn_off->second;
139 }
John Zulaufc483f442017-12-15 14:02:06 -0700140 // For a particular binding, get the global index range
141 // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists
142 const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t) const;
John Zulauf7705bfc2019-06-10 09:52:04 -0600143 const cvdescriptorset::IndexRange &GetGlobalIndexRangeFromIndex(uint32_t index) const;
John Zulaufc483f442017-12-15 14:02:06 -0700144
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -0700145 // Helper function to get the next valid binding for a descriptor
146 uint32_t GetNextValidBinding(const uint32_t) const;
Józef Kuciaf0c94d42017-10-25 22:15:22 +0200147 bool IsPushDescriptor() const { return GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR; };
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600148
John Zulauf48a6a702017-12-22 17:14:54 -0700149 struct BindingTypeStats {
150 uint32_t dynamic_buffer_count;
151 uint32_t non_dynamic_buffer_count;
152 uint32_t image_sampler_count;
153 };
154 const BindingTypeStats &GetBindingTypeStats() const { return binding_type_stats_; }
155
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700156 private:
John Zulauf223b69d2018-11-09 16:00:59 -0700157 // Only the first three data members are used for hash and equality checks, the other members are derived from them, and are
158 // used to speed up the various lookups/queries/validations
John Zulauf1f8174b2018-02-16 12:58:37 -0700159 VkDescriptorSetLayoutCreateFlags flags_;
160 std::vector<safe_VkDescriptorSetLayoutBinding> bindings_;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500161 std::vector<VkDescriptorBindingFlagsEXT> binding_flags_;
John Zulauf1f8174b2018-02-16 12:58:37 -0700162
163 // Convenience data structures for rapid lookup of various descriptor set layout properties
John Zulaufb6d71202017-12-22 16:47:09 -0700164 std::set<uint32_t> non_empty_bindings_; // Containing non-emtpy bindings in numerical order
165 std::unordered_map<uint32_t, uint32_t> binding_to_index_map_;
166 // The following map allows an non-iterative lookup of a binding from a global index...
John Zulauf7705bfc2019-06-10 09:52:04 -0600167 std::vector<IndexRange> global_index_range_; // range is exclusive of .end
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700168 // For a given binding map to associated index in the dynamic offset array
169 std::unordered_map<uint32_t, uint32_t> binding_to_dynamic_array_idx_map_;
John Zulauf1f8174b2018-02-16 12:58:37 -0700170
Jeff Bolzfdf96072018-04-10 14:32:18 -0500171 uint32_t binding_count_; // # of bindings in this layout
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700172 uint32_t descriptor_count_; // total # descriptors in this layout
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600173 uint32_t dynamic_descriptor_count_;
John Zulauf48a6a702017-12-22 17:14:54 -0700174 BindingTypeStats binding_type_stats_;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600175};
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600176
Jeremy Hayese4224ee2019-01-04 14:41:48 -0700177static inline bool operator==(const DescriptorSetLayoutDef &lhs, const DescriptorSetLayoutDef &rhs) {
John Zulauf223b69d2018-11-09 16:00:59 -0700178 bool result = (lhs.GetCreateFlags() == rhs.GetCreateFlags()) && (lhs.GetBindings() == rhs.GetBindings()) &&
179 (lhs.GetBindingFlags() == rhs.GetBindingFlags());
180 return result;
John Zulaufd47d0612018-02-16 13:00:34 -0700181}
182
John Zulauf34ebf272018-02-16 13:08:47 -0700183// Canonical dictionary of DSL definitions -- independent of device or handle
184using DescriptorSetLayoutDict = hash_util::Dictionary<DescriptorSetLayoutDef, hash_util::HasHashMember<DescriptorSetLayoutDef>>;
185using DescriptorSetLayoutId = DescriptorSetLayoutDict::Id;
John Zulauf1f8174b2018-02-16 12:58:37 -0700186
187class DescriptorSetLayout {
188 public:
189 // Constructors and destructor
190 DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info, const VkDescriptorSetLayout layout);
John Zulauf1f8174b2018-02-16 12:58:37 -0700191 bool HasBinding(const uint32_t binding) const { return layout_id_->HasBinding(binding); }
192 // Return true if this layout is compatible with passed in layout from a pipelineLayout,
193 // else return false and update error_msg with description of incompatibility
John Zulauf9ce3b252019-06-06 15:20:22 -0600194 // Return true if this layout is compatible with passed in layout
195 bool IsCompatible(DescriptorSetLayout const *rh_ds_layout) const;
John Zulauf1f8174b2018-02-16 12:58:37 -0700196 // Straightforward Get functions
197 VkDescriptorSetLayout GetDescriptorSetLayout() const { return layout_; };
198 bool IsDestroyed() const { return layout_destroyed_; }
199 void MarkDestroyed() { layout_destroyed_ = true; }
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600200 const DescriptorSetLayoutDef *GetLayoutDef() const { return layout_id_.get(); }
201 DescriptorSetLayoutId GetLayoutId() const { return layout_id_; }
John Zulauf1f8174b2018-02-16 12:58:37 -0700202 uint32_t GetTotalDescriptorCount() const { return layout_id_->GetTotalDescriptorCount(); };
203 uint32_t GetDynamicDescriptorCount() const { return layout_id_->GetDynamicDescriptorCount(); };
204 uint32_t GetBindingCount() const { return layout_id_->GetBindingCount(); };
205 VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return layout_id_->GetCreateFlags(); }
206 bool IsNextBindingConsistent(const uint32_t) const;
207 uint32_t GetIndexFromBinding(uint32_t binding) const { return layout_id_->GetIndexFromBinding(binding); }
208 // Various Get functions that can either be passed a binding#, which will
209 // be automatically translated into the appropriate index, or the index# can be passed in directly
210 uint32_t GetMaxBinding() const { return layout_id_->GetMaxBinding(); }
211 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
212 return layout_id_->GetDescriptorSetLayoutBindingPtrFromIndex(index);
213 }
214 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(uint32_t binding) const {
215 return layout_id_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
216 }
217 const std::vector<safe_VkDescriptorSetLayoutBinding> &GetBindings() const { return layout_id_->GetBindings(); }
Tony-LunarGa77cade2019-03-06 10:49:22 -0700218 const std::set<uint32_t> &GetSortedBindingSet() const { return layout_id_->GetSortedBindingSet(); }
John Zulauf1f8174b2018-02-16 12:58:37 -0700219 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return layout_id_->GetDescriptorCountFromIndex(index); }
220 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
221 return layout_id_->GetDescriptorCountFromBinding(binding);
222 }
223 VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return layout_id_->GetTypeFromIndex(index); }
224 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return layout_id_->GetTypeFromBinding(binding); }
225 VkShaderStageFlags GetStageFlagsFromIndex(const uint32_t index) const { return layout_id_->GetStageFlagsFromIndex(index); }
226 VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t binding) const {
227 return layout_id_->GetStageFlagsFromBinding(binding);
228 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500229 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromIndex(const uint32_t index) const {
230 return layout_id_->GetDescriptorBindingFlagsFromIndex(index);
231 }
232 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromBinding(const uint32_t binding) const {
233 return layout_id_->GetDescriptorBindingFlagsFromBinding(binding);
234 }
John Zulauf1f8174b2018-02-16 12:58:37 -0700235 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
236 return layout_id_->GetImmutableSamplerPtrFromBinding(binding);
237 }
238 VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
239 return layout_id_->GetImmutableSamplerPtrFromIndex(index);
240 }
241 // For a given binding and array index, return the corresponding index into the dynamic offset array
242 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
243 return layout_id_->GetDynamicOffsetIndexFromBinding(binding);
244 }
245 // For a particular binding, get the global index range
246 // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists
247 const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t binding) const {
248 return layout_id_->GetGlobalIndexRangeFromBinding(binding);
249 }
John Zulauf7705bfc2019-06-10 09:52:04 -0600250 const IndexRange &GetGlobalIndexRangeFromIndex(uint32_t index) const { return layout_id_->GetGlobalIndexRangeFromIndex(index); }
251
John Zulauf1f8174b2018-02-16 12:58:37 -0700252 // Helper function to get the next valid binding for a descriptor
253 uint32_t GetNextValidBinding(const uint32_t binding) const { return layout_id_->GetNextValidBinding(binding); }
John Zulauf1f8174b2018-02-16 12:58:37 -0700254 bool IsPushDescriptor() const { return layout_id_->IsPushDescriptor(); }
255
256 using BindingTypeStats = DescriptorSetLayoutDef::BindingTypeStats;
257 const BindingTypeStats &GetBindingTypeStats() const { return layout_id_->GetBindingTypeStats(); }
258
John Zulauf4a015c92019-06-04 09:50:05 -0600259 // Binding Iterator
260 class ConstBindingIterator {
261 public:
262 ConstBindingIterator() = delete;
263 ConstBindingIterator(const ConstBindingIterator &other) = default;
264 ConstBindingIterator &operator=(const ConstBindingIterator &rhs) = default;
265
266 ConstBindingIterator(const DescriptorSetLayout *layout) : layout_(layout), index_(0) { assert(layout); }
267 ConstBindingIterator(const DescriptorSetLayout *layout, uint32_t binding) : ConstBindingIterator(layout) {
268 index_ = layout->GetIndexFromBinding(binding);
269 }
270
271 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtr() const {
272 return layout_->GetDescriptorSetLayoutBindingPtrFromIndex(index_);
273 }
274 uint32_t GetDescriptorCount() const { return layout_->GetDescriptorCountFromIndex(index_); }
275 VkDescriptorType GetType() const { return layout_->GetTypeFromIndex(index_); }
276 VkShaderStageFlags GetStageFlags() const { return layout_->GetStageFlagsFromIndex(index_); }
277
278 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlags() const {
279 return layout_->GetDescriptorBindingFlagsFromIndex(index_);
280 }
281
282 VkSampler const *GetImmutableSamplerPtr() const { return layout_->GetImmutableSamplerPtrFromIndex(index_); }
John Zulauf7705bfc2019-06-10 09:52:04 -0600283 const IndexRange &GetGlobalIndexRange() const { return layout_->GetGlobalIndexRangeFromIndex(index_); }
John Zulauf4a015c92019-06-04 09:50:05 -0600284 bool AtEnd() const { return index_ == layout_->GetBindingCount(); }
285
286 bool operator==(const ConstBindingIterator &rhs) { return (index_ = rhs.index_) && (layout_ == rhs.layout_); }
287
288 ConstBindingIterator &operator++() {
289 if (!AtEnd()) {
290 index_++;
291 }
292 return *this;
293 }
294
295 bool IsConsistent(const ConstBindingIterator &other) const {
296 if (AtEnd() || other.AtEnd()) {
297 return false;
298 }
299 const auto *binding_ci = GetDescriptorSetLayoutBindingPtr();
300 const auto *other_binding_ci = other.GetDescriptorSetLayoutBindingPtr();
301 assert((binding_ci != nullptr) && (other_binding_ci != nullptr));
302
303 if ((binding_ci->descriptorType != other_binding_ci->descriptorType) ||
304 (binding_ci->stageFlags != other_binding_ci->stageFlags) ||
305 (!hash_util::similar_for_nullity(binding_ci->pImmutableSamplers, other_binding_ci->pImmutableSamplers)) ||
306 (GetDescriptorBindingFlags() != other.GetDescriptorBindingFlags())) {
307 return false;
308 }
309 return true;
310 }
311
312 const DescriptorSetLayout *Layout() const { return layout_; }
313 uint32_t Binding() const { return layout_->GetBindings()[index_].binding; }
314 ConstBindingIterator Next() {
315 ConstBindingIterator next(*this);
316 ++next;
317 return next;
318 }
319
320 private:
321 const DescriptorSetLayout *layout_;
322 uint32_t index_;
323 };
324 ConstBindingIterator end() const { return ConstBindingIterator(this, GetBindingCount()); }
325
John Zulauf1f8174b2018-02-16 12:58:37 -0700326 private:
327 VkDescriptorSetLayout layout_;
328 bool layout_destroyed_;
329 DescriptorSetLayoutId layout_id_;
330};
331
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600332/*
333 * Descriptor classes
334 * Descriptor is an abstract base class from which 5 separate descriptor types are derived.
335 * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per
336 * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*.
337 */
338
339// Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass"
Jeff Bolzfbe51582018-09-13 10:01:35 -0500340enum DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer, InlineUniform, AccelerationStructure };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600341
342class Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700343 public:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600344 virtual ~Descriptor(){};
Tobin Ehlis300888c2016-05-18 13:43:26 -0600345 virtual void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) = 0;
346 virtual void CopyUpdate(const Descriptor *) = 0;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600347 // Create binding between resources of this descriptor and given cb_node
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600348 virtual void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600349 virtual DescriptorClass GetClass() const { return descriptor_class; };
350 // Special fast-path check for SamplerDescriptors that are immutable
351 virtual bool IsImmutableSampler() const { return false; };
352 // Check for dynamic descriptor type
353 virtual bool IsDynamic() const { return false; };
354 // Check for storage descriptor type
355 virtual bool IsStorage() const { return false; };
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700356 bool updated; // Has descriptor been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600357 DescriptorClass descriptor_class;
358};
359// Shared helper functions - These are useful because the shared sampler image descriptor type
360// performs common functions with both sampler and image descriptors so they can share their common functions
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700361bool ValidateSampler(const VkSampler, CoreChecks *);
362bool ValidateImageUpdate(VkImageView, VkImageLayout, VkDescriptorType, CoreChecks *, const char *func_name, std::string *,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700363 std::string *);
John Zulauf9ce3b252019-06-06 15:20:22 -0600364// Return true if this layout is compatible with passed in layout from a pipelineLayout,
365// else return false and update error_msg with description of incompatibility
366bool VerifySetLayoutCompatibility(DescriptorSetLayout const *lh_ds_layout, DescriptorSetLayout const *rh_ds_layout,
367 std::string *error_msg);
John Zulaufd9435c32019-06-05 15:55:36 -0600368bool ValidateDescriptorSetLayoutCreateInfo(const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info,
369 const bool push_descriptor_ext, const uint32_t max_push_descriptors,
370 const bool descriptor_indexing_ext,
371 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
372 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
373 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600374
375class SamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700376 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600377 SamplerDescriptor(const VkSampler *);
378 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
379 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600380 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600381 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600382 VkSampler GetSampler() const { return sampler_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600383
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700384 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600385 VkSampler sampler_;
386 bool immutable_;
Tobin Ehlis546326f2016-04-26 11:06:05 -0600387};
388
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600389class ImageSamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700390 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600391 ImageSamplerDescriptor(const VkSampler *);
392 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
393 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600394 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlisc9625152016-05-24 16:47:36 -0600395 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600396 VkSampler GetSampler() const { return sampler_; }
397 VkImageView GetImageView() const { return image_view_; }
398 VkImageLayout GetImageLayout() const { return image_layout_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600399
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700400 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600401 VkSampler sampler_;
402 bool immutable_;
403 VkImageView image_view_;
404 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600405};
406
407class ImageDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700408 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600409 ImageDescriptor(const VkDescriptorType);
410 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
411 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600412 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Norbert Nopper419a1092016-05-15 19:19:41 +0200413 virtual bool IsStorage() const override { return storage_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600414 VkImageView GetImageView() const { return image_view_; }
415 VkImageLayout GetImageLayout() const { return image_layout_; }
416
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700417 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600418 bool storage_;
419 VkImageView image_view_;
420 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600421};
422
423class TexelDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700424 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600425 TexelDescriptor(const VkDescriptorType);
426 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
427 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600428 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlisf490f2e2016-05-17 06:43:48 -0600429 virtual bool IsStorage() const override { return storage_; }
430 VkBufferView GetBufferView() const { return buffer_view_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600431
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700432 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600433 VkBufferView buffer_view_;
434 bool storage_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600435};
436
437class BufferDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700438 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600439 BufferDescriptor(const VkDescriptorType);
440 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
441 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600442 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600443 virtual bool IsDynamic() const override { return dynamic_; }
444 virtual bool IsStorage() const override { return storage_; }
445 VkBuffer GetBuffer() const { return buffer_; }
446 VkDeviceSize GetOffset() const { return offset_; }
447 VkDeviceSize GetRange() const { return range_; }
448
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700449 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600450 bool storage_;
451 bool dynamic_;
452 VkBuffer buffer_;
453 VkDeviceSize offset_;
454 VkDeviceSize range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600455};
Jeff Bolze54ae892018-09-08 12:16:29 -0500456
457class InlineUniformDescriptor : public Descriptor {
458 public:
Dave Houlton142c4cb2018-10-17 15:04:41 -0600459 InlineUniformDescriptor(const VkDescriptorType) {
460 updated = false;
461 descriptor_class = InlineUniform;
462 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500463 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; }
Dave Houlton142c4cb2018-10-17 15:04:41 -0600464 void CopyUpdate(const Descriptor *) override { updated = true; }
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600465 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override {}
Jeff Bolze54ae892018-09-08 12:16:29 -0500466};
467
Jeff Bolzfbe51582018-09-13 10:01:35 -0500468class AccelerationStructureDescriptor : public Descriptor {
469 public:
Dave Houlton142c4cb2018-10-17 15:04:41 -0600470 AccelerationStructureDescriptor(const VkDescriptorType) {
471 updated = false;
472 descriptor_class = AccelerationStructure;
473 }
Jeff Bolzfbe51582018-09-13 10:01:35 -0500474 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; }
Dave Houlton142c4cb2018-10-17 15:04:41 -0600475 void CopyUpdate(const Descriptor *) override { updated = true; }
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600476 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override {}
Jeff Bolzfbe51582018-09-13 10:01:35 -0500477};
478
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600479// Structs to contain common elements that need to be shared between Validate* and Perform* calls below
480struct AllocateDescriptorSetsData {
Jeff Bolze54ae892018-09-08 12:16:29 -0500481 std::map<uint32_t, uint32_t> required_descriptors_by_type;
Tobin Ehlisa8e46e72017-06-21 10:16:10 -0600482 std::vector<std::shared_ptr<DescriptorSetLayout const>> layout_nodes;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600483 AllocateDescriptorSetsData(uint32_t);
484};
Tobin Ehlisee471462016-05-26 11:21:59 -0600485// Helper functions for descriptor set functions that cross multiple sets
486// "Validate" will make sure an update is ok without actually performing it
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700487bool ValidateUpdateDescriptorSets(const debug_report_data *, const CoreChecks *, uint32_t, const VkWriteDescriptorSet *, uint32_t,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700488 const VkCopyDescriptorSet *, const char *func_name);
Tobin Ehlisee471462016-05-26 11:21:59 -0600489// "Perform" does the update with the assumption that ValidateUpdateDescriptorSets() has passed for the given update
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700490void PerformUpdateDescriptorSets(CoreChecks *, uint32_t, const VkWriteDescriptorSet *, uint32_t, const VkCopyDescriptorSet *);
John Zulaufb845eb22018-10-12 11:41:06 -0600491
John Zulauf4a015c92019-06-04 09:50:05 -0600492// Core Validation specific validation checks using DescriptorSet and DescriptorSetLayoutAccessors
493// TODO: migrate out of descriptor_set.cpp/h
494// For a particular binding starting at offset and having update_count descriptors
495// updated, verify that for any binding boundaries crossed, the update is consistent
496bool VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator current_binding, uint32_t offset, uint32_t update_count,
497 const char *type, const VkDescriptorSet set, std::string *error_msg);
John Zulauf613fd982019-06-04 15:14:41 -0600498// Validate contents of a WriteUpdate
499bool ValidateWriteUpdate(const DescriptorSet *descriptor_set, const debug_report_data *report_data,
500 const VkWriteDescriptorSet *update, const char *func_name, std::string *error_code,
501 std::string *error_msg);
John Zulauf459939f2019-06-04 16:49:35 -0600502bool VerifyWriteUpdateContents(const DescriptorSet *dest_set, const VkWriteDescriptorSet *update, const uint32_t index,
503 const char *func_name, std::string *error_code, std::string *error_msg);
John Zulaufd9435c32019-06-05 15:55:36 -0600504// Validate contents of a CopyUpdate
505bool ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update, const DescriptorSet *dst_set,
506 const DescriptorSet *src_set, const char *func_name, std::string *error_code, std::string *error_msg);
507// Validate contents of a push descriptor update
508bool ValidatePushDescriptorsUpdate(const DescriptorSet *push_set, const debug_report_data *report_data, uint32_t write_count,
509 const VkWriteDescriptorSet *p_wds, const char *func_name);
510
511// Validate buffer descriptor update info
512bool ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type, std::string *error_code, std::string *error_msg);
513bool ValidateBufferUpdate(CoreChecks *device_data, VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
514 const char *func_name, std::string *error_code, std::string *error_msg);
515
516bool VerifyCopyUpdateContents(CoreChecks *device_data, const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
517 VkDescriptorType type, uint32_t index, const char *func_name, std::string *error_code,
518 std::string *error_msg);
John Zulauf4a015c92019-06-04 09:50:05 -0600519
John Zulaufb845eb22018-10-12 11:41:06 -0600520// Helper class to encapsulate the descriptor update template decoding logic
521struct DecodedTemplateUpdate {
522 std::vector<VkWriteDescriptorSet> desc_writes;
523 std::vector<VkWriteDescriptorSetInlineUniformBlockEXT> inline_infos;
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700524 DecodedTemplateUpdate(CoreChecks *device_data, VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
John Zulauf1d27e0a2018-11-05 10:12:48 -0700525 const void *pData, VkDescriptorSetLayout push_layout = VK_NULL_HANDLE);
John Zulaufb845eb22018-10-12 11:41:06 -0600526};
Tobin Ehlisee471462016-05-26 11:21:59 -0600527
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600528/*
529 * DescriptorSet class
530 *
531 * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set).
532 * A set has an underlying layout which defines the bindings in the set and the
533 * types and numbers of descriptors in each descriptor slot. Most of the layout
534 * interfaces are exposed through identically-named functions in the set class.
535 * Please refer to the DescriptorSetLayout comment above for a description of
536 * index, binding, and global index.
537 *
538 * At construction a vector of Descriptor* is created with types corresponding to the
539 * layout. The primary operation performed on the descriptors is to update them
540 * via write or copy updates, and validate that the update contents are correct.
541 * In order to validate update contents, the DescriptorSet stores a bunch of ptrs
542 * to data maps where various Vulkan objects can be looked up. The management of
543 * those maps is performed externally. The set class relies on their contents to
544 * be correct at the time of update.
545 */
Tobin Ehlis05be5df2016-05-05 08:25:02 -0600546class DescriptorSet : public BASE_NODE {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700547 public:
Tobin Ehlis452b4532017-06-21 09:56:13 -0600548 DescriptorSet(const VkDescriptorSet, const VkDescriptorPool, const std::shared_ptr<DescriptorSetLayout const> &,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700549 uint32_t variable_count, CoreChecks *);
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600550 ~DescriptorSet();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600551 // A number of common Get* functions that return data based on layout from which this set was created
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600552 uint32_t GetTotalDescriptorCount() const { return p_layout_->GetTotalDescriptorCount(); };
553 uint32_t GetDynamicDescriptorCount() const { return p_layout_->GetDynamicDescriptorCount(); };
554 uint32_t GetBindingCount() const { return p_layout_->GetBindingCount(); };
555 VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return p_layout_->GetTypeFromIndex(index); };
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600556 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return p_layout_->GetTypeFromBinding(binding); };
557 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return p_layout_->GetDescriptorCountFromIndex(index); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600558 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600559 return p_layout_->GetDescriptorCountFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600560 };
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700561 // Return index into dynamic offset array for given binding
562 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600563 return p_layout_->GetDynamicOffsetIndexFromBinding(binding);
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700564 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600565 // Return true if given binding is present in this set
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600566 bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600567 // For given bindings validate state at time of draw is correct, returning false on error and writing error details into string*
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600568 bool ValidateDrawState(const std::map<uint32_t, descriptor_req> &, const std::vector<uint32_t> &, CMD_BUFFER_STATE *,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600569 const char *caller, std::string *) const;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600570 // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number
571 // of objects inserted
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600572 uint32_t GetStorageUpdates(const std::map<uint32_t, descriptor_req> &, std::unordered_set<VkBuffer> *,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600573 std::unordered_set<VkImageView> *) const;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600574
John Zulauf4e7bcb52018-11-02 10:46:30 -0600575 std::string StringifySetAndLayout() const;
John Zulaufd9435c32019-06-05 15:55:36 -0600576
John Zulauf1d27e0a2018-11-05 10:12:48 -0700577 // Perform a push update whose contents were just validated using ValidatePushDescriptorsUpdate
578 void PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600579 // Perform a WriteUpdate whose contents were just validated using ValidateWriteUpdate
580 void PerformWriteUpdate(const VkWriteDescriptorSet *);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600581 // Perform a CopyUpdate whose contents were just validated using ValidateCopyUpdate
582 void PerformCopyUpdate(const VkCopyDescriptorSet *, const DescriptorSet *);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600583
John Zulauf1f8174b2018-02-16 12:58:37 -0700584 const std::shared_ptr<DescriptorSetLayout const> GetLayout() const { return p_layout_; };
John Zulaufd9435c32019-06-05 15:55:36 -0600585 VkDescriptorSetLayout GetDescriptorSetLayout() const { return p_layout_->GetDescriptorSetLayout(); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600586 VkDescriptorSet GetSet() const { return set_; };
587 // Return unordered_set of all command buffers that this set is bound to
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600588 std::unordered_set<CMD_BUFFER_STATE *> GetBoundCmdBuffers() const { return cb_bindings; }
John Zulauf6f3d2bd2018-10-29 17:08:42 -0600589 // Bind given cmd_buffer to this descriptor set and
590 // update CB image layout map with image/imagesampler descriptor image layouts
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600591 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *, const std::map<uint32_t, descriptor_req> &);
John Zulauf48a6a702017-12-22 17:14:54 -0700592
593 // Track work that has been bound or validated to avoid duplicate work, important when large descriptor arrays
594 // are present
595 typedef std::unordered_set<uint32_t> TrackedBindings;
596 static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req,
597 BindingReqMap *out_req, TrackedBindings *set);
598 static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req,
599 BindingReqMap *out_req, TrackedBindings *set, uint32_t limit);
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600600 void FilterAndTrackBindingReqs(CMD_BUFFER_STATE *, const BindingReqMap &in_req, BindingReqMap *out_req);
601 void FilterAndTrackBindingReqs(CMD_BUFFER_STATE *, PIPELINE_STATE *, const BindingReqMap &in_req, BindingReqMap *out_req);
602 void ClearCachedDynamicDescriptorValidation(CMD_BUFFER_STATE *cb_state) {
603 cached_validation_[cb_state].dynamic_buffers.clear();
604 }
605 void ClearCachedValidation(CMD_BUFFER_STATE *cb_state) { cached_validation_.erase(cb_state); }
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600606 // If given cmd_buffer is in the cb_bindings set, remove it
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600607 void RemoveBoundCommandBuffer(CMD_BUFFER_STATE *cb_node) {
John Zulauf48a6a702017-12-22 17:14:54 -0700608 cb_bindings.erase(cb_node);
609 ClearCachedValidation(cb_node);
610 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600611 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600612 return p_layout_->GetImmutableSamplerPtrFromBinding(index);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600613 };
614 // For a particular binding, get the global index
Tony-LunarGa77cade2019-03-06 10:49:22 -0700615 const IndexRange GetGlobalIndexRangeFromBinding(const uint32_t binding, bool actual_length = false) const {
616 if (actual_length && binding == p_layout_->GetMaxBinding() && IsVariableDescriptorCount(binding)) {
617 IndexRange range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
618 auto diff = GetDescriptorCountFromBinding(binding) - GetVariableDescriptorCount();
619 range.end -= diff;
620 return range;
621 }
John Zulaufc483f442017-12-15 14:02:06 -0700622 return p_layout_->GetGlobalIndexRangeFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600623 };
624 // Return true if any part of set has ever been updated
625 bool IsUpdated() const { return some_update_; };
Józef Kuciaf0c94d42017-10-25 22:15:22 +0200626 bool IsPushDescriptor() const { return p_layout_->IsPushDescriptor(); };
Jeff Bolzfdf96072018-04-10 14:32:18 -0500627 bool IsVariableDescriptorCount(uint32_t binding) const {
628 return !!(p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
629 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT);
630 }
Tony-LunarG81efe392019-03-07 15:43:27 -0700631 bool IsUpdateAfterBind(uint32_t binding) const {
632 return !!(p_layout_->GetDescriptorBindingFlagsFromBinding(binding) & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT);
633 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500634 uint32_t GetVariableDescriptorCount() const { return variable_count_; }
635 DESCRIPTOR_POOL_STATE *GetPoolState() const { return pool_state_; }
Tony-LunarGa77cade2019-03-06 10:49:22 -0700636 const Descriptor *GetDescriptorFromGlobalIndex(const uint32_t index) const { return descriptors_[index].get(); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600637
John Zulauf459939f2019-06-04 16:49:35 -0600638 // TODO: Clean up the const cleaness here. Getting non-const CoreChecks from a const DescriptorSet is probably not okay.
639 CoreChecks *GetDeviceData() const { return device_data_; }
640
John Zulauf613fd982019-06-04 15:14:41 -0600641 private:
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600642 // Private helper to set all bound cmd buffers to INVALID state
643 void InvalidateBoundCmdBuffers();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700644 bool some_update_; // has any part of the set ever been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600645 VkDescriptorSet set_;
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600646 DESCRIPTOR_POOL_STATE *pool_state_;
Tobin Ehlis452b4532017-06-21 09:56:13 -0600647 const std::shared_ptr<DescriptorSetLayout const> p_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600648 std::vector<std::unique_ptr<Descriptor>> descriptors_;
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700649 CoreChecks *device_data_;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500650 uint32_t variable_count_;
John Zulauf48a6a702017-12-22 17:14:54 -0700651
652 // Cached binding and validation support:
653 //
654 // For the lifespan of a given command buffer recording, do lazy evaluation, caching, and dirtying of
655 // expensive validation operation (typically per-draw)
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600656 typedef std::unordered_map<CMD_BUFFER_STATE *, TrackedBindings> TrackedBindingMap;
John Zulauf48a6a702017-12-22 17:14:54 -0700657 // Track the validation caching of bindings vs. the command buffer and draw state
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600658 typedef std::unordered_map<uint32_t, CMD_BUFFER_STATE::ImageLayoutUpdateCount> VersionedBindings;
John Zulauf48a6a702017-12-22 17:14:54 -0700659 struct CachedValidation {
660 TrackedBindings command_binding_and_usage; // Persistent for the life of the recording
661 TrackedBindings non_dynamic_buffers; // Persistent for the life of the recording
662 TrackedBindings dynamic_buffers; // Dirtied (flushed) each BindDescriptorSet
663 std::unordered_map<PIPELINE_STATE *, VersionedBindings> image_samplers; // Tested vs. changes to CB's ImageLayout
664 };
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600665 typedef std::unordered_map<CMD_BUFFER_STATE *, CachedValidation> CachedValidationMap;
John Zulauf48a6a702017-12-22 17:14:54 -0700666 // Image and ImageView bindings are validated per pipeline and not invalidate by repeated binding
667 CachedValidationMap cached_validation_;
668};
669// For the "bindless" style resource usage with many descriptors, need to optimize binding and validation
670class PrefilterBindRequestMap {
671 public:
672 static const uint32_t kManyDescriptors_ = 64; // TODO base this number on measured data
673 std::unique_ptr<BindingReqMap> filtered_map_;
674 const BindingReqMap &orig_map_;
675
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600676 PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, CMD_BUFFER_STATE *cb_state);
677 PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, CMD_BUFFER_STATE *cb_state, PIPELINE_STATE *);
John Zulauf48a6a702017-12-22 17:14:54 -0700678 const BindingReqMap &Map() const { return (filtered_map_) ? *filtered_map_ : orig_map_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600679};
Dave Houltona9df0ce2018-02-07 10:51:23 -0700680} // namespace cvdescriptorset
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700681#endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_