blob: ed9bd401457ae1fc098ffe4692873e92f989cbe5 [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;
John Zulaufc93c4252019-06-25 09:19:49 -060038class ValidationStateTracker;
Tobin Ehlis58c884f2017-02-08 12:15:27 -070039
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060040// Descriptor Data structures
John Zulaufc483f442017-12-15 14:02:06 -070041namespace cvdescriptorset {
42
John Zulauf48a6a702017-12-22 17:14:54 -070043// Utility structs/classes/types
John Zulaufc483f442017-12-15 14:02:06 -070044// Index range for global indices below, end is exclusive, i.e. [start,end)
45struct IndexRange {
46 IndexRange(uint32_t start_in, uint32_t end_in) : start(start_in), end(end_in) {}
47 IndexRange() = default;
48 uint32_t start;
49 uint32_t end;
50};
John Zulauf48a6a702017-12-22 17:14:54 -070051typedef std::map<uint32_t, descriptor_req> BindingReqMap;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060052
53/*
John Zulauf34ebf272018-02-16 13:08:47 -070054 * DescriptorSetLayoutDef/DescriptorSetLayout classes
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060055 *
John Zulauf34ebf272018-02-16 13:08:47 -070056 * Overview - These two classes encapsulate the Vulkan VkDescriptorSetLayout data (layout).
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060057 * A layout consists of some number of bindings, each of which has a binding#, a
58 * type, descriptor count, stage flags, and pImmutableSamplers.
John Zulauf34ebf272018-02-16 13:08:47 -070059
60 * The DescriptorSetLayoutDef represents a canonicalization of the input data and contains
61 * neither per handle or per device state. It is possible for different handles on
62 * different devices to share a common def. This is used and useful for quick compatibiltiy
63 * validation. The DescriptorSetLayout refers to a DescriptorSetLayoutDef and contains
64 * all per handle state.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060065 *
66 * Index vs Binding - A layout is created with an array of VkDescriptorSetLayoutBinding
67 * where each array index will have a corresponding binding# that is defined in that struct.
Tobin Ehlis9637fb22016-12-12 15:59:34 -070068 * The binding#, then, is decoupled from VkDescriptorSetLayoutBinding index, which allows
69 * bindings to be defined out-of-order. This DescriptorSetLayout class, however, stores
70 * the bindings internally in-order. This is useful for operations which may "roll over"
71 * from a single binding to the next consecutive binding.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060072 *
Tobin Ehlis9637fb22016-12-12 15:59:34 -070073 * Note that although the bindings are stored in-order, there still may be "gaps" in the
74 * binding#. For example, if the binding creation order is 8, 7, 10, 3, 4, then the
75 * internal binding array will have five entries stored in binding order 3, 4, 7, 8, 10.
76 * To process all of the bindings in a layout you can iterate from 0 to GetBindingCount()
77 * and use the Get*FromIndex() functions for each index. To just process a single binding,
78 * use the Get*FromBinding() functions.
79 *
80 * Global Index - The binding vector index has as many indices as there are bindings.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060081 * This class also has the concept of a Global Index. For the global index functions,
82 * there are as many global indices as there are descriptors in the layout.
83 * For the global index, consider all of the bindings to be a flat array where
Tobin Ehlis9637fb22016-12-12 15:59:34 -070084 * descriptor 0 of of the lowest binding# is index 0 and each descriptor in the layout
85 * increments from there. So if the lowest binding# in this example had descriptorCount of
86 * 10, then the GlobalStartIndex of the 2nd lowest binding# will be 10 where 0-9 are the
87 * global indices for the lowest binding#.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060088 */
John Zulauf1f8174b2018-02-16 12:58:37 -070089class DescriptorSetLayoutDef {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070090 public:
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060091 // Constructors and destructor
John Zulauf1f8174b2018-02-16 12:58:37 -070092 DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info);
93 size_t hash() const;
94
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060095 uint32_t GetTotalDescriptorCount() const { return descriptor_count_; };
96 uint32_t GetDynamicDescriptorCount() const { return dynamic_descriptor_count_; };
Józef Kucia1bb00972017-09-10 11:24:08 +020097 VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return flags_; }
Tobin Ehlisf922ef82016-11-30 10:19:14 -070098 // For a given binding, return the number of descriptors in that binding and all successive bindings
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060099 uint32_t GetBindingCount() const { return binding_count_; };
John Zulaufb6d71202017-12-22 16:47:09 -0700100 // Non-empty binding numbers in order
101 const std::set<uint32_t> &GetSortedBindingSet() const { return non_empty_bindings_; }
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600102 // Return true if given binding is present in this layout
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600103 bool HasBinding(const uint32_t binding) const { return binding_to_index_map_.count(binding) > 0; };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600104 // Return true if binding 1 beyond given exists and has same type, stageFlags & immutable sampler use
105 bool IsNextBindingConsistent(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700106 uint32_t GetIndexFromBinding(uint32_t binding) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600107 // Various Get functions that can either be passed a binding#, which will
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700108 // be automatically translated into the appropriate index, or the index# can be passed in directly
John Zulaufb6d71202017-12-22 16:47:09 -0700109 uint32_t GetMaxBinding() const { return bindings_[bindings_.size() - 1].binding; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600110 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700111 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(uint32_t binding) const {
112 return GetDescriptorSetLayoutBindingPtrFromIndex(GetIndexFromBinding(binding));
113 }
John Zulauf1f8174b2018-02-16 12:58:37 -0700114 const std::vector<safe_VkDescriptorSetLayoutBinding> &GetBindings() const { return bindings_; }
John Zulauf223b69d2018-11-09 16:00:59 -0700115 const std::vector<VkDescriptorBindingFlagsEXT> &GetBindingFlags() const { return binding_flags_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600116 uint32_t GetDescriptorCountFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700117 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
118 return GetDescriptorCountFromIndex(GetIndexFromBinding(binding));
119 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600120 VkDescriptorType GetTypeFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700121 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return GetTypeFromIndex(GetIndexFromBinding(binding)); }
122 VkShaderStageFlags GetStageFlagsFromIndex(const uint32_t) const;
123 VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t binding) const {
124 return GetStageFlagsFromIndex(GetIndexFromBinding(binding));
125 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500126 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromIndex(const uint32_t) const;
127 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromBinding(const uint32_t binding) const {
128 return GetDescriptorBindingFlagsFromIndex(GetIndexFromBinding(binding));
129 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600130 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t) const;
131 VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t) const;
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700132 // For a given binding and array index, return the corresponding index into the dynamic offset array
133 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
134 auto dyn_off = binding_to_dynamic_array_idx_map_.find(binding);
135 if (dyn_off == binding_to_dynamic_array_idx_map_.end()) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700136 assert(0); // Requesting dyn offset for invalid binding/array idx pair
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700137 return -1;
138 }
139 return dyn_off->second;
140 }
John Zulaufc483f442017-12-15 14:02:06 -0700141 // For a particular binding, get the global index range
142 // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists
143 const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t) const;
John Zulauf7705bfc2019-06-10 09:52:04 -0600144 const cvdescriptorset::IndexRange &GetGlobalIndexRangeFromIndex(uint32_t index) const;
John Zulaufc483f442017-12-15 14:02:06 -0700145
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -0700146 // Helper function to get the next valid binding for a descriptor
147 uint32_t GetNextValidBinding(const uint32_t) const;
Józef Kuciaf0c94d42017-10-25 22:15:22 +0200148 bool IsPushDescriptor() const { return GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR; };
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600149
John Zulauf48a6a702017-12-22 17:14:54 -0700150 struct BindingTypeStats {
151 uint32_t dynamic_buffer_count;
152 uint32_t non_dynamic_buffer_count;
153 uint32_t image_sampler_count;
154 };
155 const BindingTypeStats &GetBindingTypeStats() const { return binding_type_stats_; }
156
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700157 private:
John Zulauf223b69d2018-11-09 16:00:59 -0700158 // Only the first three data members are used for hash and equality checks, the other members are derived from them, and are
159 // used to speed up the various lookups/queries/validations
John Zulauf1f8174b2018-02-16 12:58:37 -0700160 VkDescriptorSetLayoutCreateFlags flags_;
161 std::vector<safe_VkDescriptorSetLayoutBinding> bindings_;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500162 std::vector<VkDescriptorBindingFlagsEXT> binding_flags_;
John Zulauf1f8174b2018-02-16 12:58:37 -0700163
164 // Convenience data structures for rapid lookup of various descriptor set layout properties
John Zulaufb6d71202017-12-22 16:47:09 -0700165 std::set<uint32_t> non_empty_bindings_; // Containing non-emtpy bindings in numerical order
166 std::unordered_map<uint32_t, uint32_t> binding_to_index_map_;
167 // The following map allows an non-iterative lookup of a binding from a global index...
John Zulauf7705bfc2019-06-10 09:52:04 -0600168 std::vector<IndexRange> global_index_range_; // range is exclusive of .end
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700169 // For a given binding map to associated index in the dynamic offset array
170 std::unordered_map<uint32_t, uint32_t> binding_to_dynamic_array_idx_map_;
John Zulauf1f8174b2018-02-16 12:58:37 -0700171
Jeff Bolzfdf96072018-04-10 14:32:18 -0500172 uint32_t binding_count_; // # of bindings in this layout
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700173 uint32_t descriptor_count_; // total # descriptors in this layout
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600174 uint32_t dynamic_descriptor_count_;
John Zulauf48a6a702017-12-22 17:14:54 -0700175 BindingTypeStats binding_type_stats_;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600176};
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600177
Jeremy Hayese4224ee2019-01-04 14:41:48 -0700178static inline bool operator==(const DescriptorSetLayoutDef &lhs, const DescriptorSetLayoutDef &rhs) {
John Zulauf223b69d2018-11-09 16:00:59 -0700179 bool result = (lhs.GetCreateFlags() == rhs.GetCreateFlags()) && (lhs.GetBindings() == rhs.GetBindings()) &&
180 (lhs.GetBindingFlags() == rhs.GetBindingFlags());
181 return result;
John Zulaufd47d0612018-02-16 13:00:34 -0700182}
183
John Zulauf34ebf272018-02-16 13:08:47 -0700184// Canonical dictionary of DSL definitions -- independent of device or handle
185using DescriptorSetLayoutDict = hash_util::Dictionary<DescriptorSetLayoutDef, hash_util::HasHashMember<DescriptorSetLayoutDef>>;
186using DescriptorSetLayoutId = DescriptorSetLayoutDict::Id;
John Zulauf1f8174b2018-02-16 12:58:37 -0700187
188class DescriptorSetLayout {
189 public:
190 // Constructors and destructor
191 DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info, const VkDescriptorSetLayout layout);
John Zulauf1f8174b2018-02-16 12:58:37 -0700192 bool HasBinding(const uint32_t binding) const { return layout_id_->HasBinding(binding); }
193 // Return true if this layout is compatible with passed in layout from a pipelineLayout,
194 // else return false and update error_msg with description of incompatibility
John Zulauf9ce3b252019-06-06 15:20:22 -0600195 // Return true if this layout is compatible with passed in layout
196 bool IsCompatible(DescriptorSetLayout const *rh_ds_layout) const;
John Zulauf1f8174b2018-02-16 12:58:37 -0700197 // Straightforward Get functions
198 VkDescriptorSetLayout GetDescriptorSetLayout() const { return layout_; };
199 bool IsDestroyed() const { return layout_destroyed_; }
200 void MarkDestroyed() { layout_destroyed_ = true; }
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600201 const DescriptorSetLayoutDef *GetLayoutDef() const { return layout_id_.get(); }
202 DescriptorSetLayoutId GetLayoutId() const { return layout_id_; }
John Zulauf1f8174b2018-02-16 12:58:37 -0700203 uint32_t GetTotalDescriptorCount() const { return layout_id_->GetTotalDescriptorCount(); };
204 uint32_t GetDynamicDescriptorCount() const { return layout_id_->GetDynamicDescriptorCount(); };
205 uint32_t GetBindingCount() const { return layout_id_->GetBindingCount(); };
206 VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return layout_id_->GetCreateFlags(); }
207 bool IsNextBindingConsistent(const uint32_t) const;
208 uint32_t GetIndexFromBinding(uint32_t binding) const { return layout_id_->GetIndexFromBinding(binding); }
209 // Various Get functions that can either be passed a binding#, which will
210 // be automatically translated into the appropriate index, or the index# can be passed in directly
211 uint32_t GetMaxBinding() const { return layout_id_->GetMaxBinding(); }
212 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
213 return layout_id_->GetDescriptorSetLayoutBindingPtrFromIndex(index);
214 }
215 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(uint32_t binding) const {
216 return layout_id_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
217 }
218 const std::vector<safe_VkDescriptorSetLayoutBinding> &GetBindings() const { return layout_id_->GetBindings(); }
Tony-LunarGa77cade2019-03-06 10:49:22 -0700219 const std::set<uint32_t> &GetSortedBindingSet() const { return layout_id_->GetSortedBindingSet(); }
John Zulauf1f8174b2018-02-16 12:58:37 -0700220 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return layout_id_->GetDescriptorCountFromIndex(index); }
221 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
222 return layout_id_->GetDescriptorCountFromBinding(binding);
223 }
224 VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return layout_id_->GetTypeFromIndex(index); }
225 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return layout_id_->GetTypeFromBinding(binding); }
226 VkShaderStageFlags GetStageFlagsFromIndex(const uint32_t index) const { return layout_id_->GetStageFlagsFromIndex(index); }
227 VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t binding) const {
228 return layout_id_->GetStageFlagsFromBinding(binding);
229 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500230 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromIndex(const uint32_t index) const {
231 return layout_id_->GetDescriptorBindingFlagsFromIndex(index);
232 }
233 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromBinding(const uint32_t binding) const {
234 return layout_id_->GetDescriptorBindingFlagsFromBinding(binding);
235 }
John Zulauf1f8174b2018-02-16 12:58:37 -0700236 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
237 return layout_id_->GetImmutableSamplerPtrFromBinding(binding);
238 }
239 VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
240 return layout_id_->GetImmutableSamplerPtrFromIndex(index);
241 }
242 // For a given binding and array index, return the corresponding index into the dynamic offset array
243 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
244 return layout_id_->GetDynamicOffsetIndexFromBinding(binding);
245 }
246 // For a particular binding, get the global index range
247 // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists
248 const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t binding) const {
249 return layout_id_->GetGlobalIndexRangeFromBinding(binding);
250 }
John Zulauf7705bfc2019-06-10 09:52:04 -0600251 const IndexRange &GetGlobalIndexRangeFromIndex(uint32_t index) const { return layout_id_->GetGlobalIndexRangeFromIndex(index); }
252
John Zulauf1f8174b2018-02-16 12:58:37 -0700253 // Helper function to get the next valid binding for a descriptor
254 uint32_t GetNextValidBinding(const uint32_t binding) const { return layout_id_->GetNextValidBinding(binding); }
John Zulauf1f8174b2018-02-16 12:58:37 -0700255 bool IsPushDescriptor() const { return layout_id_->IsPushDescriptor(); }
John Zulauf382e1912019-06-10 15:27:44 -0600256 bool IsVariableDescriptorCountFromIndex(uint32_t index) const {
257 return !!(GetDescriptorBindingFlagsFromIndex(index) & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT);
258 }
259 bool IsVariableDescriptorCount(uint32_t binding) const {
260 return IsVariableDescriptorCountFromIndex(GetIndexFromBinding(binding));
261 }
John Zulauf1f8174b2018-02-16 12:58:37 -0700262
263 using BindingTypeStats = DescriptorSetLayoutDef::BindingTypeStats;
264 const BindingTypeStats &GetBindingTypeStats() const { return layout_id_->GetBindingTypeStats(); }
265
John Zulauf4a015c92019-06-04 09:50:05 -0600266 // Binding Iterator
267 class ConstBindingIterator {
268 public:
269 ConstBindingIterator() = delete;
270 ConstBindingIterator(const ConstBindingIterator &other) = default;
271 ConstBindingIterator &operator=(const ConstBindingIterator &rhs) = default;
272
273 ConstBindingIterator(const DescriptorSetLayout *layout) : layout_(layout), index_(0) { assert(layout); }
274 ConstBindingIterator(const DescriptorSetLayout *layout, uint32_t binding) : ConstBindingIterator(layout) {
275 index_ = layout->GetIndexFromBinding(binding);
276 }
277
278 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtr() const {
279 return layout_->GetDescriptorSetLayoutBindingPtrFromIndex(index_);
280 }
281 uint32_t GetDescriptorCount() const { return layout_->GetDescriptorCountFromIndex(index_); }
282 VkDescriptorType GetType() const { return layout_->GetTypeFromIndex(index_); }
283 VkShaderStageFlags GetStageFlags() const { return layout_->GetStageFlagsFromIndex(index_); }
284
285 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlags() const {
286 return layout_->GetDescriptorBindingFlagsFromIndex(index_);
287 }
288
John Zulauf382e1912019-06-10 15:27:44 -0600289 bool IsVariableDescriptorCount() const { return layout_->IsVariableDescriptorCountFromIndex(index_); }
290
John Zulauf4a015c92019-06-04 09:50:05 -0600291 VkSampler const *GetImmutableSamplerPtr() const { return layout_->GetImmutableSamplerPtrFromIndex(index_); }
John Zulauf7705bfc2019-06-10 09:52:04 -0600292 const IndexRange &GetGlobalIndexRange() const { return layout_->GetGlobalIndexRangeFromIndex(index_); }
John Zulauf4a015c92019-06-04 09:50:05 -0600293 bool AtEnd() const { return index_ == layout_->GetBindingCount(); }
294
John Zulauf382e1912019-06-10 15:27:44 -0600295 // Return index into dynamic offset array for given binding
296 int32_t GetDynamicOffsetIndex() const {
297 return layout_->GetDynamicOffsetIndexFromBinding(Binding()); // There is only binding mapped access in layout_
298 }
299
John Zulauf4a015c92019-06-04 09:50:05 -0600300 bool operator==(const ConstBindingIterator &rhs) { return (index_ = rhs.index_) && (layout_ == rhs.layout_); }
301
302 ConstBindingIterator &operator++() {
303 if (!AtEnd()) {
304 index_++;
305 }
306 return *this;
307 }
308
309 bool IsConsistent(const ConstBindingIterator &other) const {
310 if (AtEnd() || other.AtEnd()) {
311 return false;
312 }
313 const auto *binding_ci = GetDescriptorSetLayoutBindingPtr();
314 const auto *other_binding_ci = other.GetDescriptorSetLayoutBindingPtr();
315 assert((binding_ci != nullptr) && (other_binding_ci != nullptr));
316
317 if ((binding_ci->descriptorType != other_binding_ci->descriptorType) ||
318 (binding_ci->stageFlags != other_binding_ci->stageFlags) ||
319 (!hash_util::similar_for_nullity(binding_ci->pImmutableSamplers, other_binding_ci->pImmutableSamplers)) ||
320 (GetDescriptorBindingFlags() != other.GetDescriptorBindingFlags())) {
321 return false;
322 }
323 return true;
324 }
325
326 const DescriptorSetLayout *Layout() const { return layout_; }
327 uint32_t Binding() const { return layout_->GetBindings()[index_].binding; }
328 ConstBindingIterator Next() {
329 ConstBindingIterator next(*this);
330 ++next;
331 return next;
332 }
333
334 private:
335 const DescriptorSetLayout *layout_;
336 uint32_t index_;
337 };
338 ConstBindingIterator end() const { return ConstBindingIterator(this, GetBindingCount()); }
339
John Zulauf1f8174b2018-02-16 12:58:37 -0700340 private:
341 VkDescriptorSetLayout layout_;
342 bool layout_destroyed_;
343 DescriptorSetLayoutId layout_id_;
344};
345
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600346/*
347 * Descriptor classes
348 * Descriptor is an abstract base class from which 5 separate descriptor types are derived.
349 * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per
350 * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*.
351 */
352
353// Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass"
Jeff Bolzfbe51582018-09-13 10:01:35 -0500354enum DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer, InlineUniform, AccelerationStructure };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600355
356class Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700357 public:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600358 virtual ~Descriptor(){};
Tobin Ehlis300888c2016-05-18 13:43:26 -0600359 virtual void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) = 0;
360 virtual void CopyUpdate(const Descriptor *) = 0;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600361 // Create binding between resources of this descriptor and given cb_node
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600362 virtual void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600363 virtual DescriptorClass GetClass() const { return descriptor_class; };
364 // Special fast-path check for SamplerDescriptors that are immutable
365 virtual bool IsImmutableSampler() const { return false; };
366 // Check for dynamic descriptor type
367 virtual bool IsDynamic() const { return false; };
368 // Check for storage descriptor type
369 virtual bool IsStorage() const { return false; };
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700370 bool updated; // Has descriptor been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600371 DescriptorClass descriptor_class;
372};
John Zulaufc93c4252019-06-25 09:19:49 -0600373
John Zulauf9ce3b252019-06-06 15:20:22 -0600374// Return true if this layout is compatible with passed in layout from a pipelineLayout,
375// else return false and update error_msg with description of incompatibility
376bool VerifySetLayoutCompatibility(DescriptorSetLayout const *lh_ds_layout, DescriptorSetLayout const *rh_ds_layout,
377 std::string *error_msg);
John Zulaufd9435c32019-06-05 15:55:36 -0600378bool ValidateDescriptorSetLayoutCreateInfo(const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info,
379 const bool push_descriptor_ext, const uint32_t max_push_descriptors,
380 const bool descriptor_indexing_ext,
381 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
382 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
383 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600384
385class SamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700386 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600387 SamplerDescriptor(const VkSampler *);
388 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
389 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600390 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600391 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600392 VkSampler GetSampler() const { return sampler_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600393
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700394 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600395 VkSampler sampler_;
396 bool immutable_;
Tobin Ehlis546326f2016-04-26 11:06:05 -0600397};
398
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600399class ImageSamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700400 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600401 ImageSamplerDescriptor(const VkSampler *);
402 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
403 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600404 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlisc9625152016-05-24 16:47:36 -0600405 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600406 VkSampler GetSampler() const { return sampler_; }
407 VkImageView GetImageView() const { return image_view_; }
408 VkImageLayout GetImageLayout() const { return image_layout_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600409
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700410 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600411 VkSampler sampler_;
412 bool immutable_;
413 VkImageView image_view_;
414 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600415};
416
417class ImageDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700418 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600419 ImageDescriptor(const VkDescriptorType);
420 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
421 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600422 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Norbert Nopper419a1092016-05-15 19:19:41 +0200423 virtual bool IsStorage() const override { return storage_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600424 VkImageView GetImageView() const { return image_view_; }
425 VkImageLayout GetImageLayout() const { return image_layout_; }
426
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700427 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600428 bool storage_;
429 VkImageView image_view_;
430 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600431};
432
433class TexelDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700434 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600435 TexelDescriptor(const VkDescriptorType);
436 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
437 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600438 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlisf490f2e2016-05-17 06:43:48 -0600439 virtual bool IsStorage() const override { return storage_; }
440 VkBufferView GetBufferView() const { return buffer_view_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600441
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700442 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600443 VkBufferView buffer_view_;
444 bool storage_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600445};
446
447class BufferDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700448 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600449 BufferDescriptor(const VkDescriptorType);
450 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
451 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600452 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600453 virtual bool IsDynamic() const override { return dynamic_; }
454 virtual bool IsStorage() const override { return storage_; }
455 VkBuffer GetBuffer() const { return buffer_; }
456 VkDeviceSize GetOffset() const { return offset_; }
457 VkDeviceSize GetRange() const { return range_; }
458
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700459 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600460 bool storage_;
461 bool dynamic_;
462 VkBuffer buffer_;
463 VkDeviceSize offset_;
464 VkDeviceSize range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600465};
Jeff Bolze54ae892018-09-08 12:16:29 -0500466
467class InlineUniformDescriptor : public Descriptor {
468 public:
Dave Houlton142c4cb2018-10-17 15:04:41 -0600469 InlineUniformDescriptor(const VkDescriptorType) {
470 updated = false;
471 descriptor_class = InlineUniform;
472 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500473 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; }
Dave Houlton142c4cb2018-10-17 15:04:41 -0600474 void CopyUpdate(const Descriptor *) override { updated = true; }
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600475 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override {}
Jeff Bolze54ae892018-09-08 12:16:29 -0500476};
477
Jeff Bolzfbe51582018-09-13 10:01:35 -0500478class AccelerationStructureDescriptor : public Descriptor {
479 public:
Dave Houlton142c4cb2018-10-17 15:04:41 -0600480 AccelerationStructureDescriptor(const VkDescriptorType) {
481 updated = false;
482 descriptor_class = AccelerationStructure;
483 }
Jeff Bolzfbe51582018-09-13 10:01:35 -0500484 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; }
Dave Houlton142c4cb2018-10-17 15:04:41 -0600485 void CopyUpdate(const Descriptor *) override { updated = true; }
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600486 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override {}
Jeff Bolzfbe51582018-09-13 10:01:35 -0500487};
488
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600489// Structs to contain common elements that need to be shared between Validate* and Perform* calls below
490struct AllocateDescriptorSetsData {
Jeff Bolze54ae892018-09-08 12:16:29 -0500491 std::map<uint32_t, uint32_t> required_descriptors_by_type;
Tobin Ehlisa8e46e72017-06-21 10:16:10 -0600492 std::vector<std::shared_ptr<DescriptorSetLayout const>> layout_nodes;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600493 AllocateDescriptorSetsData(uint32_t);
494};
Tobin Ehlisee471462016-05-26 11:21:59 -0600495// Helper functions for descriptor set functions that cross multiple sets
496// "Validate" will make sure an update is ok without actually performing it
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700497bool ValidateUpdateDescriptorSets(const debug_report_data *, const CoreChecks *, uint32_t, const VkWriteDescriptorSet *, uint32_t,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700498 const VkCopyDescriptorSet *, const char *func_name);
Tobin Ehlisee471462016-05-26 11:21:59 -0600499// "Perform" does the update with the assumption that ValidateUpdateDescriptorSets() has passed for the given update
John Zulaufe3b35f32019-06-25 14:21:21 -0600500void PerformUpdateDescriptorSets(ValidationStateTracker *, uint32_t, const VkWriteDescriptorSet *, uint32_t,
501 const VkCopyDescriptorSet *);
John Zulaufb845eb22018-10-12 11:41:06 -0600502
John Zulauf4a015c92019-06-04 09:50:05 -0600503// Core Validation specific validation checks using DescriptorSet and DescriptorSetLayoutAccessors
504// TODO: migrate out of descriptor_set.cpp/h
505// For a particular binding starting at offset and having update_count descriptors
506// updated, verify that for any binding boundaries crossed, the update is consistent
507bool VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator current_binding, uint32_t offset, uint32_t update_count,
508 const char *type, const VkDescriptorSet set, std::string *error_msg);
John Zulaufd9435c32019-06-05 15:55:36 -0600509
510// Validate buffer descriptor update info
511bool ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type, std::string *error_code, std::string *error_msg);
John Zulauf4a015c92019-06-04 09:50:05 -0600512
John Zulaufb845eb22018-10-12 11:41:06 -0600513// Helper class to encapsulate the descriptor update template decoding logic
514struct DecodedTemplateUpdate {
515 std::vector<VkWriteDescriptorSet> desc_writes;
516 std::vector<VkWriteDescriptorSetInlineUniformBlockEXT> inline_infos;
John Zulaufe3b35f32019-06-25 14:21:21 -0600517 DecodedTemplateUpdate(const ValidationStateTracker *device_data, VkDescriptorSet descriptorSet,
518 const TEMPLATE_STATE *template_state, const void *pData,
519 VkDescriptorSetLayout push_layout = VK_NULL_HANDLE);
John Zulaufb845eb22018-10-12 11:41:06 -0600520};
Tobin Ehlisee471462016-05-26 11:21:59 -0600521
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600522/*
523 * DescriptorSet class
524 *
525 * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set).
526 * A set has an underlying layout which defines the bindings in the set and the
527 * types and numbers of descriptors in each descriptor slot. Most of the layout
528 * interfaces are exposed through identically-named functions in the set class.
529 * Please refer to the DescriptorSetLayout comment above for a description of
530 * index, binding, and global index.
531 *
532 * At construction a vector of Descriptor* is created with types corresponding to the
533 * layout. The primary operation performed on the descriptors is to update them
534 * via write or copy updates, and validate that the update contents are correct.
535 * In order to validate update contents, the DescriptorSet stores a bunch of ptrs
536 * to data maps where various Vulkan objects can be looked up. The management of
537 * those maps is performed externally. The set class relies on their contents to
538 * be correct at the time of update.
539 */
Tobin Ehlis05be5df2016-05-05 08:25:02 -0600540class DescriptorSet : public BASE_NODE {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700541 public:
John Zulaufc93c4252019-06-25 09:19:49 -0600542 using StateTracker = ValidationStateTracker;
Tobin Ehlis452b4532017-06-21 09:56:13 -0600543 DescriptorSet(const VkDescriptorSet, const VkDescriptorPool, const std::shared_ptr<DescriptorSetLayout const> &,
John Zulaufc93c4252019-06-25 09:19:49 -0600544 uint32_t variable_count, StateTracker *);
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600545 ~DescriptorSet();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600546 // 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 -0600547 uint32_t GetTotalDescriptorCount() const { return p_layout_->GetTotalDescriptorCount(); };
548 uint32_t GetDynamicDescriptorCount() const { return p_layout_->GetDynamicDescriptorCount(); };
549 uint32_t GetBindingCount() const { return p_layout_->GetBindingCount(); };
550 VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return p_layout_->GetTypeFromIndex(index); };
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600551 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return p_layout_->GetTypeFromBinding(binding); };
552 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return p_layout_->GetDescriptorCountFromIndex(index); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600553 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600554 return p_layout_->GetDescriptorCountFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600555 };
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700556 // Return index into dynamic offset array for given binding
557 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600558 return p_layout_->GetDynamicOffsetIndexFromBinding(binding);
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700559 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600560 // Return true if given binding is present in this set
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600561 bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600562 // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number
563 // of objects inserted
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600564 uint32_t GetStorageUpdates(const std::map<uint32_t, descriptor_req> &, std::unordered_set<VkBuffer> *,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600565 std::unordered_set<VkImageView> *) const;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600566
John Zulauf4e7bcb52018-11-02 10:46:30 -0600567 std::string StringifySetAndLayout() const;
John Zulaufd9435c32019-06-05 15:55:36 -0600568
John Zulauf1d27e0a2018-11-05 10:12:48 -0700569 // Perform a push update whose contents were just validated using ValidatePushDescriptorsUpdate
570 void PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600571 // Perform a WriteUpdate whose contents were just validated using ValidateWriteUpdate
572 void PerformWriteUpdate(const VkWriteDescriptorSet *);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600573 // Perform a CopyUpdate whose contents were just validated using ValidateCopyUpdate
574 void PerformCopyUpdate(const VkCopyDescriptorSet *, const DescriptorSet *);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600575
John Zulauf1f8174b2018-02-16 12:58:37 -0700576 const std::shared_ptr<DescriptorSetLayout const> GetLayout() const { return p_layout_; };
John Zulaufd9435c32019-06-05 15:55:36 -0600577 VkDescriptorSetLayout GetDescriptorSetLayout() const { return p_layout_->GetDescriptorSetLayout(); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600578 VkDescriptorSet GetSet() const { return set_; };
579 // Return unordered_set of all command buffers that this set is bound to
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600580 std::unordered_set<CMD_BUFFER_STATE *> GetBoundCmdBuffers() const { return cb_bindings; }
John Zulauf6f3d2bd2018-10-29 17:08:42 -0600581 // Bind given cmd_buffer to this descriptor set and
582 // update CB image layout map with image/imagesampler descriptor image layouts
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600583 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *, const std::map<uint32_t, descriptor_req> &);
John Zulauf48a6a702017-12-22 17:14:54 -0700584
585 // Track work that has been bound or validated to avoid duplicate work, important when large descriptor arrays
586 // are present
587 typedef std::unordered_set<uint32_t> TrackedBindings;
588 static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req,
589 BindingReqMap *out_req, TrackedBindings *set);
590 static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req,
591 BindingReqMap *out_req, TrackedBindings *set, uint32_t limit);
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600592 void FilterAndTrackBindingReqs(CMD_BUFFER_STATE *, const BindingReqMap &in_req, BindingReqMap *out_req);
593 void FilterAndTrackBindingReqs(CMD_BUFFER_STATE *, PIPELINE_STATE *, const BindingReqMap &in_req, BindingReqMap *out_req);
594 void ClearCachedDynamicDescriptorValidation(CMD_BUFFER_STATE *cb_state) {
595 cached_validation_[cb_state].dynamic_buffers.clear();
596 }
597 void ClearCachedValidation(CMD_BUFFER_STATE *cb_state) { cached_validation_.erase(cb_state); }
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600598 // If given cmd_buffer is in the cb_bindings set, remove it
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600599 void RemoveBoundCommandBuffer(CMD_BUFFER_STATE *cb_node) {
John Zulauf48a6a702017-12-22 17:14:54 -0700600 cb_bindings.erase(cb_node);
601 ClearCachedValidation(cb_node);
602 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600603 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600604 return p_layout_->GetImmutableSamplerPtrFromBinding(index);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600605 };
606 // For a particular binding, get the global index
Tony-LunarGa77cade2019-03-06 10:49:22 -0700607 const IndexRange GetGlobalIndexRangeFromBinding(const uint32_t binding, bool actual_length = false) const {
608 if (actual_length && binding == p_layout_->GetMaxBinding() && IsVariableDescriptorCount(binding)) {
609 IndexRange range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
610 auto diff = GetDescriptorCountFromBinding(binding) - GetVariableDescriptorCount();
611 range.end -= diff;
612 return range;
613 }
John Zulaufc483f442017-12-15 14:02:06 -0700614 return p_layout_->GetGlobalIndexRangeFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600615 };
616 // Return true if any part of set has ever been updated
617 bool IsUpdated() const { return some_update_; };
Józef Kuciaf0c94d42017-10-25 22:15:22 +0200618 bool IsPushDescriptor() const { return p_layout_->IsPushDescriptor(); };
John Zulauf382e1912019-06-10 15:27:44 -0600619 bool IsVariableDescriptorCount(uint32_t binding) const { return p_layout_->IsVariableDescriptorCount(binding); }
Tony-LunarG81efe392019-03-07 15:43:27 -0700620 bool IsUpdateAfterBind(uint32_t binding) const {
621 return !!(p_layout_->GetDescriptorBindingFlagsFromBinding(binding) & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT);
622 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500623 uint32_t GetVariableDescriptorCount() const { return variable_count_; }
624 DESCRIPTOR_POOL_STATE *GetPoolState() const { return pool_state_; }
Tony-LunarGa77cade2019-03-06 10:49:22 -0700625 const Descriptor *GetDescriptorFromGlobalIndex(const uint32_t index) const { return descriptors_[index].get(); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600626
John Zulauf613fd982019-06-04 15:14:41 -0600627 private:
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600628 // Private helper to set all bound cmd buffers to INVALID state
629 void InvalidateBoundCmdBuffers();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700630 bool some_update_; // has any part of the set ever been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600631 VkDescriptorSet set_;
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600632 DESCRIPTOR_POOL_STATE *pool_state_;
Tobin Ehlis452b4532017-06-21 09:56:13 -0600633 const std::shared_ptr<DescriptorSetLayout const> p_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600634 std::vector<std::unique_ptr<Descriptor>> descriptors_;
John Zulaufc93c4252019-06-25 09:19:49 -0600635 StateTracker *state_data_;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500636 uint32_t variable_count_;
John Zulauf48a6a702017-12-22 17:14:54 -0700637
638 // Cached binding and validation support:
639 //
640 // For the lifespan of a given command buffer recording, do lazy evaluation, caching, and dirtying of
641 // expensive validation operation (typically per-draw)
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600642 typedef std::unordered_map<CMD_BUFFER_STATE *, TrackedBindings> TrackedBindingMap;
John Zulauf48a6a702017-12-22 17:14:54 -0700643 // Track the validation caching of bindings vs. the command buffer and draw state
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600644 typedef std::unordered_map<uint32_t, CMD_BUFFER_STATE::ImageLayoutUpdateCount> VersionedBindings;
John Zulauf48a6a702017-12-22 17:14:54 -0700645 struct CachedValidation {
646 TrackedBindings command_binding_and_usage; // Persistent for the life of the recording
647 TrackedBindings non_dynamic_buffers; // Persistent for the life of the recording
648 TrackedBindings dynamic_buffers; // Dirtied (flushed) each BindDescriptorSet
649 std::unordered_map<PIPELINE_STATE *, VersionedBindings> image_samplers; // Tested vs. changes to CB's ImageLayout
650 };
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600651 typedef std::unordered_map<CMD_BUFFER_STATE *, CachedValidation> CachedValidationMap;
John Zulauf48a6a702017-12-22 17:14:54 -0700652 // Image and ImageView bindings are validated per pipeline and not invalidate by repeated binding
653 CachedValidationMap cached_validation_;
654};
655// For the "bindless" style resource usage with many descriptors, need to optimize binding and validation
656class PrefilterBindRequestMap {
657 public:
658 static const uint32_t kManyDescriptors_ = 64; // TODO base this number on measured data
659 std::unique_ptr<BindingReqMap> filtered_map_;
660 const BindingReqMap &orig_map_;
661
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600662 PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, CMD_BUFFER_STATE *cb_state);
663 PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, CMD_BUFFER_STATE *cb_state, PIPELINE_STATE *);
John Zulauf48a6a702017-12-22 17:14:54 -0700664 const BindingReqMap &Map() const { return (filtered_map_) ? *filtered_map_ : orig_map_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600665};
Dave Houltona9df0ce2018-02-07 10:51:23 -0700666} // namespace cvdescriptorset
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700667#endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_