blob: 10e41e1d3feb799a09f5c47403310c4fe4c9924c [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(); }
John Zulauf382e1912019-06-10 15:27:44 -0600255 bool IsVariableDescriptorCountFromIndex(uint32_t index) const {
256 return !!(GetDescriptorBindingFlagsFromIndex(index) & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT);
257 }
258 bool IsVariableDescriptorCount(uint32_t binding) const {
259 return IsVariableDescriptorCountFromIndex(GetIndexFromBinding(binding));
260 }
John Zulauf1f8174b2018-02-16 12:58:37 -0700261
262 using BindingTypeStats = DescriptorSetLayoutDef::BindingTypeStats;
263 const BindingTypeStats &GetBindingTypeStats() const { return layout_id_->GetBindingTypeStats(); }
264
John Zulauf4a015c92019-06-04 09:50:05 -0600265 // Binding Iterator
266 class ConstBindingIterator {
267 public:
268 ConstBindingIterator() = delete;
269 ConstBindingIterator(const ConstBindingIterator &other) = default;
270 ConstBindingIterator &operator=(const ConstBindingIterator &rhs) = default;
271
272 ConstBindingIterator(const DescriptorSetLayout *layout) : layout_(layout), index_(0) { assert(layout); }
273 ConstBindingIterator(const DescriptorSetLayout *layout, uint32_t binding) : ConstBindingIterator(layout) {
274 index_ = layout->GetIndexFromBinding(binding);
275 }
276
277 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtr() const {
278 return layout_->GetDescriptorSetLayoutBindingPtrFromIndex(index_);
279 }
280 uint32_t GetDescriptorCount() const { return layout_->GetDescriptorCountFromIndex(index_); }
281 VkDescriptorType GetType() const { return layout_->GetTypeFromIndex(index_); }
282 VkShaderStageFlags GetStageFlags() const { return layout_->GetStageFlagsFromIndex(index_); }
283
284 VkDescriptorBindingFlagsEXT GetDescriptorBindingFlags() const {
285 return layout_->GetDescriptorBindingFlagsFromIndex(index_);
286 }
287
John Zulauf382e1912019-06-10 15:27:44 -0600288 bool IsVariableDescriptorCount() const { return layout_->IsVariableDescriptorCountFromIndex(index_); }
289
John Zulauf4a015c92019-06-04 09:50:05 -0600290 VkSampler const *GetImmutableSamplerPtr() const { return layout_->GetImmutableSamplerPtrFromIndex(index_); }
John Zulauf7705bfc2019-06-10 09:52:04 -0600291 const IndexRange &GetGlobalIndexRange() const { return layout_->GetGlobalIndexRangeFromIndex(index_); }
John Zulauf4a015c92019-06-04 09:50:05 -0600292 bool AtEnd() const { return index_ == layout_->GetBindingCount(); }
293
John Zulauf382e1912019-06-10 15:27:44 -0600294 // Return index into dynamic offset array for given binding
295 int32_t GetDynamicOffsetIndex() const {
296 return layout_->GetDynamicOffsetIndexFromBinding(Binding()); // There is only binding mapped access in layout_
297 }
298
John Zulauf4a015c92019-06-04 09:50:05 -0600299 bool operator==(const ConstBindingIterator &rhs) { return (index_ = rhs.index_) && (layout_ == rhs.layout_); }
300
301 ConstBindingIterator &operator++() {
302 if (!AtEnd()) {
303 index_++;
304 }
305 return *this;
306 }
307
308 bool IsConsistent(const ConstBindingIterator &other) const {
309 if (AtEnd() || other.AtEnd()) {
310 return false;
311 }
312 const auto *binding_ci = GetDescriptorSetLayoutBindingPtr();
313 const auto *other_binding_ci = other.GetDescriptorSetLayoutBindingPtr();
314 assert((binding_ci != nullptr) && (other_binding_ci != nullptr));
315
316 if ((binding_ci->descriptorType != other_binding_ci->descriptorType) ||
317 (binding_ci->stageFlags != other_binding_ci->stageFlags) ||
318 (!hash_util::similar_for_nullity(binding_ci->pImmutableSamplers, other_binding_ci->pImmutableSamplers)) ||
319 (GetDescriptorBindingFlags() != other.GetDescriptorBindingFlags())) {
320 return false;
321 }
322 return true;
323 }
324
325 const DescriptorSetLayout *Layout() const { return layout_; }
326 uint32_t Binding() const { return layout_->GetBindings()[index_].binding; }
327 ConstBindingIterator Next() {
328 ConstBindingIterator next(*this);
329 ++next;
330 return next;
331 }
332
333 private:
334 const DescriptorSetLayout *layout_;
335 uint32_t index_;
336 };
337 ConstBindingIterator end() const { return ConstBindingIterator(this, GetBindingCount()); }
338
John Zulauf1f8174b2018-02-16 12:58:37 -0700339 private:
340 VkDescriptorSetLayout layout_;
341 bool layout_destroyed_;
342 DescriptorSetLayoutId layout_id_;
343};
344
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600345/*
346 * Descriptor classes
347 * Descriptor is an abstract base class from which 5 separate descriptor types are derived.
348 * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per
349 * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*.
350 */
351
352// Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass"
Jeff Bolzfbe51582018-09-13 10:01:35 -0500353enum DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer, InlineUniform, AccelerationStructure };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600354
355class Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700356 public:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600357 virtual ~Descriptor(){};
Tobin Ehlis300888c2016-05-18 13:43:26 -0600358 virtual void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) = 0;
359 virtual void CopyUpdate(const Descriptor *) = 0;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600360 // Create binding between resources of this descriptor and given cb_node
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600361 virtual void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600362 virtual DescriptorClass GetClass() const { return descriptor_class; };
363 // Special fast-path check for SamplerDescriptors that are immutable
364 virtual bool IsImmutableSampler() const { return false; };
365 // Check for dynamic descriptor type
366 virtual bool IsDynamic() const { return false; };
367 // Check for storage descriptor type
368 virtual bool IsStorage() const { return false; };
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700369 bool updated; // Has descriptor been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600370 DescriptorClass descriptor_class;
371};
372// Shared helper functions - These are useful because the shared sampler image descriptor type
373// performs common functions with both sampler and image descriptors so they can share their common functions
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700374bool ValidateSampler(const VkSampler, CoreChecks *);
375bool ValidateImageUpdate(VkImageView, VkImageLayout, VkDescriptorType, CoreChecks *, const char *func_name, std::string *,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700376 std::string *);
John Zulauf9ce3b252019-06-06 15:20:22 -0600377// Return true if this layout is compatible with passed in layout from a pipelineLayout,
378// else return false and update error_msg with description of incompatibility
379bool VerifySetLayoutCompatibility(DescriptorSetLayout const *lh_ds_layout, DescriptorSetLayout const *rh_ds_layout,
380 std::string *error_msg);
John Zulaufd9435c32019-06-05 15:55:36 -0600381bool ValidateDescriptorSetLayoutCreateInfo(const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info,
382 const bool push_descriptor_ext, const uint32_t max_push_descriptors,
383 const bool descriptor_indexing_ext,
384 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
385 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
386 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600387
388class SamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700389 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600390 SamplerDescriptor(const VkSampler *);
391 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
392 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600393 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600394 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600395 VkSampler GetSampler() const { return sampler_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600396
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700397 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600398 VkSampler sampler_;
399 bool immutable_;
Tobin Ehlis546326f2016-04-26 11:06:05 -0600400};
401
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600402class ImageSamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700403 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600404 ImageSamplerDescriptor(const VkSampler *);
405 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
406 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600407 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlisc9625152016-05-24 16:47:36 -0600408 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600409 VkSampler GetSampler() const { return sampler_; }
410 VkImageView GetImageView() const { return image_view_; }
411 VkImageLayout GetImageLayout() const { return image_layout_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600412
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700413 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600414 VkSampler sampler_;
415 bool immutable_;
416 VkImageView image_view_;
417 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600418};
419
420class ImageDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700421 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600422 ImageDescriptor(const VkDescriptorType);
423 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
424 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600425 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Norbert Nopper419a1092016-05-15 19:19:41 +0200426 virtual bool IsStorage() const override { return storage_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600427 VkImageView GetImageView() const { return image_view_; }
428 VkImageLayout GetImageLayout() const { return image_layout_; }
429
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700430 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600431 bool storage_;
432 VkImageView image_view_;
433 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600434};
435
436class TexelDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700437 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600438 TexelDescriptor(const VkDescriptorType);
439 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
440 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600441 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlisf490f2e2016-05-17 06:43:48 -0600442 virtual bool IsStorage() const override { return storage_; }
443 VkBufferView GetBufferView() const { return buffer_view_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600444
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700445 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600446 VkBufferView buffer_view_;
447 bool storage_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600448};
449
450class BufferDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700451 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600452 BufferDescriptor(const VkDescriptorType);
453 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
454 void CopyUpdate(const Descriptor *) override;
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600455 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600456 virtual bool IsDynamic() const override { return dynamic_; }
457 virtual bool IsStorage() const override { return storage_; }
458 VkBuffer GetBuffer() const { return buffer_; }
459 VkDeviceSize GetOffset() const { return offset_; }
460 VkDeviceSize GetRange() const { return range_; }
461
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700462 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600463 bool storage_;
464 bool dynamic_;
465 VkBuffer buffer_;
466 VkDeviceSize offset_;
467 VkDeviceSize range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600468};
Jeff Bolze54ae892018-09-08 12:16:29 -0500469
470class InlineUniformDescriptor : public Descriptor {
471 public:
Dave Houlton142c4cb2018-10-17 15:04:41 -0600472 InlineUniformDescriptor(const VkDescriptorType) {
473 updated = false;
474 descriptor_class = InlineUniform;
475 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500476 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; }
Dave Houlton142c4cb2018-10-17 15:04:41 -0600477 void CopyUpdate(const Descriptor *) override { updated = true; }
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600478 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override {}
Jeff Bolze54ae892018-09-08 12:16:29 -0500479};
480
Jeff Bolzfbe51582018-09-13 10:01:35 -0500481class AccelerationStructureDescriptor : public Descriptor {
482 public:
Dave Houlton142c4cb2018-10-17 15:04:41 -0600483 AccelerationStructureDescriptor(const VkDescriptorType) {
484 updated = false;
485 descriptor_class = AccelerationStructure;
486 }
Jeff Bolzfbe51582018-09-13 10:01:35 -0500487 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; }
Dave Houlton142c4cb2018-10-17 15:04:41 -0600488 void CopyUpdate(const Descriptor *) override { updated = true; }
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600489 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *) override {}
Jeff Bolzfbe51582018-09-13 10:01:35 -0500490};
491
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600492// Structs to contain common elements that need to be shared between Validate* and Perform* calls below
493struct AllocateDescriptorSetsData {
Jeff Bolze54ae892018-09-08 12:16:29 -0500494 std::map<uint32_t, uint32_t> required_descriptors_by_type;
Tobin Ehlisa8e46e72017-06-21 10:16:10 -0600495 std::vector<std::shared_ptr<DescriptorSetLayout const>> layout_nodes;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600496 AllocateDescriptorSetsData(uint32_t);
497};
Tobin Ehlisee471462016-05-26 11:21:59 -0600498// Helper functions for descriptor set functions that cross multiple sets
499// "Validate" will make sure an update is ok without actually performing it
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700500bool ValidateUpdateDescriptorSets(const debug_report_data *, const CoreChecks *, uint32_t, const VkWriteDescriptorSet *, uint32_t,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700501 const VkCopyDescriptorSet *, const char *func_name);
Tobin Ehlisee471462016-05-26 11:21:59 -0600502// "Perform" does the update with the assumption that ValidateUpdateDescriptorSets() has passed for the given update
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700503void PerformUpdateDescriptorSets(CoreChecks *, uint32_t, const VkWriteDescriptorSet *, uint32_t, const VkCopyDescriptorSet *);
John Zulaufb845eb22018-10-12 11:41:06 -0600504
John Zulauf4a015c92019-06-04 09:50:05 -0600505// Core Validation specific validation checks using DescriptorSet and DescriptorSetLayoutAccessors
506// TODO: migrate out of descriptor_set.cpp/h
507// For a particular binding starting at offset and having update_count descriptors
508// updated, verify that for any binding boundaries crossed, the update is consistent
509bool VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator current_binding, uint32_t offset, uint32_t update_count,
510 const char *type, const VkDescriptorSet set, std::string *error_msg);
John Zulauf613fd982019-06-04 15:14:41 -0600511// Validate contents of a WriteUpdate
512bool ValidateWriteUpdate(const DescriptorSet *descriptor_set, const debug_report_data *report_data,
513 const VkWriteDescriptorSet *update, const char *func_name, std::string *error_code,
514 std::string *error_msg);
John Zulauf459939f2019-06-04 16:49:35 -0600515bool VerifyWriteUpdateContents(const DescriptorSet *dest_set, const VkWriteDescriptorSet *update, const uint32_t index,
516 const char *func_name, std::string *error_code, std::string *error_msg);
John Zulauf382e1912019-06-10 15:27:44 -0600517// For given bindings validate state at time of draw is correct, returning false on error and writing error details into string*
518bool ValidateDrawState(const DescriptorSet *descriptor_set, const std::map<uint32_t, descriptor_req> &bindings,
519 const std::vector<uint32_t> &dynamic_offsets, CMD_BUFFER_STATE *cb_node, const char *caller,
520 std::string *error);
John Zulaufd9435c32019-06-05 15:55:36 -0600521// Validate contents of a CopyUpdate
522bool ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update, const DescriptorSet *dst_set,
523 const DescriptorSet *src_set, const char *func_name, std::string *error_code, std::string *error_msg);
524// Validate contents of a push descriptor update
525bool ValidatePushDescriptorsUpdate(const DescriptorSet *push_set, const debug_report_data *report_data, uint32_t write_count,
526 const VkWriteDescriptorSet *p_wds, const char *func_name);
527
528// Validate buffer descriptor update info
529bool ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type, std::string *error_code, std::string *error_msg);
530bool ValidateBufferUpdate(CoreChecks *device_data, VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
531 const char *func_name, std::string *error_code, std::string *error_msg);
532
533bool VerifyCopyUpdateContents(CoreChecks *device_data, const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
534 VkDescriptorType type, uint32_t index, const char *func_name, std::string *error_code,
535 std::string *error_msg);
John Zulauf4a015c92019-06-04 09:50:05 -0600536
John Zulaufb845eb22018-10-12 11:41:06 -0600537// Helper class to encapsulate the descriptor update template decoding logic
538struct DecodedTemplateUpdate {
539 std::vector<VkWriteDescriptorSet> desc_writes;
540 std::vector<VkWriteDescriptorSetInlineUniformBlockEXT> inline_infos;
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700541 DecodedTemplateUpdate(CoreChecks *device_data, VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
John Zulauf1d27e0a2018-11-05 10:12:48 -0700542 const void *pData, VkDescriptorSetLayout push_layout = VK_NULL_HANDLE);
John Zulaufb845eb22018-10-12 11:41:06 -0600543};
Tobin Ehlisee471462016-05-26 11:21:59 -0600544
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600545/*
546 * DescriptorSet class
547 *
548 * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set).
549 * A set has an underlying layout which defines the bindings in the set and the
550 * types and numbers of descriptors in each descriptor slot. Most of the layout
551 * interfaces are exposed through identically-named functions in the set class.
552 * Please refer to the DescriptorSetLayout comment above for a description of
553 * index, binding, and global index.
554 *
555 * At construction a vector of Descriptor* is created with types corresponding to the
556 * layout. The primary operation performed on the descriptors is to update them
557 * via write or copy updates, and validate that the update contents are correct.
558 * In order to validate update contents, the DescriptorSet stores a bunch of ptrs
559 * to data maps where various Vulkan objects can be looked up. The management of
560 * those maps is performed externally. The set class relies on their contents to
561 * be correct at the time of update.
562 */
Tobin Ehlis05be5df2016-05-05 08:25:02 -0600563class DescriptorSet : public BASE_NODE {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700564 public:
Tobin Ehlis452b4532017-06-21 09:56:13 -0600565 DescriptorSet(const VkDescriptorSet, const VkDescriptorPool, const std::shared_ptr<DescriptorSetLayout const> &,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700566 uint32_t variable_count, CoreChecks *);
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600567 ~DescriptorSet();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600568 // 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 -0600569 uint32_t GetTotalDescriptorCount() const { return p_layout_->GetTotalDescriptorCount(); };
570 uint32_t GetDynamicDescriptorCount() const { return p_layout_->GetDynamicDescriptorCount(); };
571 uint32_t GetBindingCount() const { return p_layout_->GetBindingCount(); };
572 VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return p_layout_->GetTypeFromIndex(index); };
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600573 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return p_layout_->GetTypeFromBinding(binding); };
574 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return p_layout_->GetDescriptorCountFromIndex(index); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600575 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600576 return p_layout_->GetDescriptorCountFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600577 };
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700578 // Return index into dynamic offset array for given binding
579 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600580 return p_layout_->GetDynamicOffsetIndexFromBinding(binding);
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700581 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600582 // Return true if given binding is present in this set
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600583 bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600584 // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number
585 // of objects inserted
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600586 uint32_t GetStorageUpdates(const std::map<uint32_t, descriptor_req> &, std::unordered_set<VkBuffer> *,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600587 std::unordered_set<VkImageView> *) const;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600588
John Zulauf4e7bcb52018-11-02 10:46:30 -0600589 std::string StringifySetAndLayout() const;
John Zulaufd9435c32019-06-05 15:55:36 -0600590
John Zulauf1d27e0a2018-11-05 10:12:48 -0700591 // Perform a push update whose contents were just validated using ValidatePushDescriptorsUpdate
592 void PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600593 // Perform a WriteUpdate whose contents were just validated using ValidateWriteUpdate
594 void PerformWriteUpdate(const VkWriteDescriptorSet *);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600595 // Perform a CopyUpdate whose contents were just validated using ValidateCopyUpdate
596 void PerformCopyUpdate(const VkCopyDescriptorSet *, const DescriptorSet *);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600597
John Zulauf1f8174b2018-02-16 12:58:37 -0700598 const std::shared_ptr<DescriptorSetLayout const> GetLayout() const { return p_layout_; };
John Zulaufd9435c32019-06-05 15:55:36 -0600599 VkDescriptorSetLayout GetDescriptorSetLayout() const { return p_layout_->GetDescriptorSetLayout(); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600600 VkDescriptorSet GetSet() const { return set_; };
601 // Return unordered_set of all command buffers that this set is bound to
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600602 std::unordered_set<CMD_BUFFER_STATE *> GetBoundCmdBuffers() const { return cb_bindings; }
John Zulauf6f3d2bd2018-10-29 17:08:42 -0600603 // Bind given cmd_buffer to this descriptor set and
604 // update CB image layout map with image/imagesampler descriptor image layouts
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600605 void UpdateDrawState(CoreChecks *, CMD_BUFFER_STATE *, const std::map<uint32_t, descriptor_req> &);
John Zulauf48a6a702017-12-22 17:14:54 -0700606
607 // Track work that has been bound or validated to avoid duplicate work, important when large descriptor arrays
608 // are present
609 typedef std::unordered_set<uint32_t> TrackedBindings;
610 static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req,
611 BindingReqMap *out_req, TrackedBindings *set);
612 static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req,
613 BindingReqMap *out_req, TrackedBindings *set, uint32_t limit);
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600614 void FilterAndTrackBindingReqs(CMD_BUFFER_STATE *, const BindingReqMap &in_req, BindingReqMap *out_req);
615 void FilterAndTrackBindingReqs(CMD_BUFFER_STATE *, PIPELINE_STATE *, const BindingReqMap &in_req, BindingReqMap *out_req);
616 void ClearCachedDynamicDescriptorValidation(CMD_BUFFER_STATE *cb_state) {
617 cached_validation_[cb_state].dynamic_buffers.clear();
618 }
619 void ClearCachedValidation(CMD_BUFFER_STATE *cb_state) { cached_validation_.erase(cb_state); }
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600620 // If given cmd_buffer is in the cb_bindings set, remove it
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600621 void RemoveBoundCommandBuffer(CMD_BUFFER_STATE *cb_node) {
John Zulauf48a6a702017-12-22 17:14:54 -0700622 cb_bindings.erase(cb_node);
623 ClearCachedValidation(cb_node);
624 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600625 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600626 return p_layout_->GetImmutableSamplerPtrFromBinding(index);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600627 };
628 // For a particular binding, get the global index
Tony-LunarGa77cade2019-03-06 10:49:22 -0700629 const IndexRange GetGlobalIndexRangeFromBinding(const uint32_t binding, bool actual_length = false) const {
630 if (actual_length && binding == p_layout_->GetMaxBinding() && IsVariableDescriptorCount(binding)) {
631 IndexRange range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
632 auto diff = GetDescriptorCountFromBinding(binding) - GetVariableDescriptorCount();
633 range.end -= diff;
634 return range;
635 }
John Zulaufc483f442017-12-15 14:02:06 -0700636 return p_layout_->GetGlobalIndexRangeFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600637 };
638 // Return true if any part of set has ever been updated
639 bool IsUpdated() const { return some_update_; };
Józef Kuciaf0c94d42017-10-25 22:15:22 +0200640 bool IsPushDescriptor() const { return p_layout_->IsPushDescriptor(); };
John Zulauf382e1912019-06-10 15:27:44 -0600641 bool IsVariableDescriptorCount(uint32_t binding) const { return p_layout_->IsVariableDescriptorCount(binding); }
Tony-LunarG81efe392019-03-07 15:43:27 -0700642 bool IsUpdateAfterBind(uint32_t binding) const {
643 return !!(p_layout_->GetDescriptorBindingFlagsFromBinding(binding) & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT);
644 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500645 uint32_t GetVariableDescriptorCount() const { return variable_count_; }
646 DESCRIPTOR_POOL_STATE *GetPoolState() const { return pool_state_; }
Tony-LunarGa77cade2019-03-06 10:49:22 -0700647 const Descriptor *GetDescriptorFromGlobalIndex(const uint32_t index) const { return descriptors_[index].get(); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600648
John Zulauf459939f2019-06-04 16:49:35 -0600649 // TODO: Clean up the const cleaness here. Getting non-const CoreChecks from a const DescriptorSet is probably not okay.
650 CoreChecks *GetDeviceData() const { return device_data_; }
651
John Zulauf613fd982019-06-04 15:14:41 -0600652 private:
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600653 // Private helper to set all bound cmd buffers to INVALID state
654 void InvalidateBoundCmdBuffers();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700655 bool some_update_; // has any part of the set ever been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600656 VkDescriptorSet set_;
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600657 DESCRIPTOR_POOL_STATE *pool_state_;
Tobin Ehlis452b4532017-06-21 09:56:13 -0600658 const std::shared_ptr<DescriptorSetLayout const> p_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600659 std::vector<std::unique_ptr<Descriptor>> descriptors_;
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700660 CoreChecks *device_data_;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500661 uint32_t variable_count_;
John Zulauf48a6a702017-12-22 17:14:54 -0700662
663 // Cached binding and validation support:
664 //
665 // For the lifespan of a given command buffer recording, do lazy evaluation, caching, and dirtying of
666 // expensive validation operation (typically per-draw)
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600667 typedef std::unordered_map<CMD_BUFFER_STATE *, TrackedBindings> TrackedBindingMap;
John Zulauf48a6a702017-12-22 17:14:54 -0700668 // Track the validation caching of bindings vs. the command buffer and draw state
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600669 typedef std::unordered_map<uint32_t, CMD_BUFFER_STATE::ImageLayoutUpdateCount> VersionedBindings;
John Zulauf48a6a702017-12-22 17:14:54 -0700670 struct CachedValidation {
671 TrackedBindings command_binding_and_usage; // Persistent for the life of the recording
672 TrackedBindings non_dynamic_buffers; // Persistent for the life of the recording
673 TrackedBindings dynamic_buffers; // Dirtied (flushed) each BindDescriptorSet
674 std::unordered_map<PIPELINE_STATE *, VersionedBindings> image_samplers; // Tested vs. changes to CB's ImageLayout
675 };
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600676 typedef std::unordered_map<CMD_BUFFER_STATE *, CachedValidation> CachedValidationMap;
John Zulauf48a6a702017-12-22 17:14:54 -0700677 // Image and ImageView bindings are validated per pipeline and not invalidate by repeated binding
678 CachedValidationMap cached_validation_;
679};
680// For the "bindless" style resource usage with many descriptors, need to optimize binding and validation
681class PrefilterBindRequestMap {
682 public:
683 static const uint32_t kManyDescriptors_ = 64; // TODO base this number on measured data
684 std::unique_ptr<BindingReqMap> filtered_map_;
685 const BindingReqMap &orig_map_;
686
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600687 PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, CMD_BUFFER_STATE *cb_state);
688 PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, CMD_BUFFER_STATE *cb_state, PIPELINE_STATE *);
John Zulauf48a6a702017-12-22 17:14:54 -0700689 const BindingReqMap &Map() const { return (filtered_map_) ? *filtered_map_ : orig_map_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600690};
Dave Houltona9df0ce2018-02-07 10:51:23 -0700691} // namespace cvdescriptorset
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700692#endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_