| Jeremy Hayes | e4224ee | 2019-01-04 14:41:48 -0700 | [diff] [blame^] | 1 | /* 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 Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Tobin Ehlis <tobine@google.com> |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame] | 19 | * John Zulauf <jzulauf@lunarg.com> |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 20 | */ |
| 21 | #ifndef CORE_VALIDATION_DESCRIPTOR_SETS_H_ |
| 22 | #define CORE_VALIDATION_DESCRIPTOR_SETS_H_ |
| 23 | |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 24 | #include "core_validation_error_enums.h" |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 25 | #include "core_validation_types.h" |
| John Zulauf | d47d061 | 2018-02-16 13:00:34 -0700 | [diff] [blame] | 26 | #include "hash_vk_types.h" |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 27 | #include "vk_layer_logging.h" |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 28 | #include "vk_layer_utils.h" |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 29 | #include "vk_safe_struct.h" |
| 30 | #include "vulkan/vk_layer.h" |
| Mark Lobodzinski | 3382637 | 2017-04-13 11:10:11 -0600 | [diff] [blame] | 31 | #include "vk_object_types.h" |
| Tobin Ehlis | cebc4c0 | 2016-08-22 10:10:43 -0600 | [diff] [blame] | 32 | #include <map> |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 33 | #include <memory> |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 34 | #include <set> |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 35 | #include <unordered_map> |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 36 | #include <unordered_set> |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
| Tobin Ehlis | 58c884f | 2017-02-08 12:15:27 -0700 | [diff] [blame] | 39 | using core_validation::layer_data; |
| 40 | |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 41 | // Descriptor Data structures |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame] | 42 | namespace cvdescriptorset { |
| 43 | |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 44 | // Utility structs/classes/types |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame] | 45 | // Index range for global indices below, end is exclusive, i.e. [start,end) |
| 46 | struct IndexRange { |
| 47 | IndexRange(uint32_t start_in, uint32_t end_in) : start(start_in), end(end_in) {} |
| 48 | IndexRange() = default; |
| 49 | uint32_t start; |
| 50 | uint32_t end; |
| 51 | }; |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 52 | typedef std::map<uint32_t, descriptor_req> BindingReqMap; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 53 | |
| 54 | /* |
| John Zulauf | 34ebf27 | 2018-02-16 13:08:47 -0700 | [diff] [blame] | 55 | * DescriptorSetLayoutDef/DescriptorSetLayout classes |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 56 | * |
| John Zulauf | 34ebf27 | 2018-02-16 13:08:47 -0700 | [diff] [blame] | 57 | * Overview - These two classes encapsulate the Vulkan VkDescriptorSetLayout data (layout). |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 58 | * A layout consists of some number of bindings, each of which has a binding#, a |
| 59 | * type, descriptor count, stage flags, and pImmutableSamplers. |
| John Zulauf | 34ebf27 | 2018-02-16 13:08:47 -0700 | [diff] [blame] | 60 | |
| 61 | * The DescriptorSetLayoutDef represents a canonicalization of the input data and contains |
| 62 | * neither per handle or per device state. It is possible for different handles on |
| 63 | * different devices to share a common def. This is used and useful for quick compatibiltiy |
| 64 | * validation. The DescriptorSetLayout refers to a DescriptorSetLayoutDef and contains |
| 65 | * all per handle state. |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 66 | * |
| 67 | * Index vs Binding - A layout is created with an array of VkDescriptorSetLayoutBinding |
| 68 | * where each array index will have a corresponding binding# that is defined in that struct. |
| Tobin Ehlis | 9637fb2 | 2016-12-12 15:59:34 -0700 | [diff] [blame] | 69 | * The binding#, then, is decoupled from VkDescriptorSetLayoutBinding index, which allows |
| 70 | * bindings to be defined out-of-order. This DescriptorSetLayout class, however, stores |
| 71 | * the bindings internally in-order. This is useful for operations which may "roll over" |
| 72 | * from a single binding to the next consecutive binding. |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 73 | * |
| Tobin Ehlis | 9637fb2 | 2016-12-12 15:59:34 -0700 | [diff] [blame] | 74 | * Note that although the bindings are stored in-order, there still may be "gaps" in the |
| 75 | * binding#. For example, if the binding creation order is 8, 7, 10, 3, 4, then the |
| 76 | * internal binding array will have five entries stored in binding order 3, 4, 7, 8, 10. |
| 77 | * To process all of the bindings in a layout you can iterate from 0 to GetBindingCount() |
| 78 | * and use the Get*FromIndex() functions for each index. To just process a single binding, |
| 79 | * use the Get*FromBinding() functions. |
| 80 | * |
| 81 | * Global Index - The binding vector index has as many indices as there are bindings. |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 82 | * This class also has the concept of a Global Index. For the global index functions, |
| 83 | * there are as many global indices as there are descriptors in the layout. |
| 84 | * For the global index, consider all of the bindings to be a flat array where |
| Tobin Ehlis | 9637fb2 | 2016-12-12 15:59:34 -0700 | [diff] [blame] | 85 | * descriptor 0 of of the lowest binding# is index 0 and each descriptor in the layout |
| 86 | * increments from there. So if the lowest binding# in this example had descriptorCount of |
| 87 | * 10, then the GlobalStartIndex of the 2nd lowest binding# will be 10 where 0-9 are the |
| 88 | * global indices for the lowest binding#. |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 89 | */ |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 90 | class DescriptorSetLayoutDef { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 91 | public: |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 92 | // Constructors and destructor |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 93 | DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info); |
| 94 | size_t hash() const; |
| 95 | |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 96 | uint32_t GetTotalDescriptorCount() const { return descriptor_count_; }; |
| 97 | uint32_t GetDynamicDescriptorCount() const { return dynamic_descriptor_count_; }; |
| Józef Kucia | 1bb0097 | 2017-09-10 11:24:08 +0200 | [diff] [blame] | 98 | VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return flags_; } |
| Tobin Ehlis | f922ef8 | 2016-11-30 10:19:14 -0700 | [diff] [blame] | 99 | // For a given binding, return the number of descriptors in that binding and all successive bindings |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 100 | uint32_t GetBindingCount() const { return binding_count_; }; |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 101 | // Non-empty binding numbers in order |
| 102 | const std::set<uint32_t> &GetSortedBindingSet() const { return non_empty_bindings_; } |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 103 | // Return true if given binding is present in this layout |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 104 | bool HasBinding(const uint32_t binding) const { return binding_to_index_map_.count(binding) > 0; }; |
| John Zulauf | df3c5c1 | 2018-03-06 16:44:43 -0700 | [diff] [blame] | 105 | // Return true if this DSL Def (referenced by the 1st layout) is compatible with another DSL Def (ref'd from the 2nd layout) |
| 106 | // else return false and update error_msg with description of incompatibility |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 107 | bool IsCompatible(VkDescriptorSetLayout, VkDescriptorSetLayout, DescriptorSetLayoutDef const *const, std::string *) const; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 108 | // Return true if binding 1 beyond given exists and has same type, stageFlags & immutable sampler use |
| 109 | bool IsNextBindingConsistent(const uint32_t) const; |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 110 | uint32_t GetIndexFromBinding(uint32_t binding) const; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 111 | // Various Get functions that can either be passed a binding#, which will |
| Tobin Ehlis | 9637fb2 | 2016-12-12 15:59:34 -0700 | [diff] [blame] | 112 | // be automatically translated into the appropriate index, or the index# can be passed in directly |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 113 | uint32_t GetMaxBinding() const { return bindings_[bindings_.size() - 1].binding; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 114 | VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t) const; |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 115 | VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(uint32_t binding) const { |
| 116 | return GetDescriptorSetLayoutBindingPtrFromIndex(GetIndexFromBinding(binding)); |
| 117 | } |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 118 | const std::vector<safe_VkDescriptorSetLayoutBinding> &GetBindings() const { return bindings_; } |
| John Zulauf | 223b69d | 2018-11-09 16:00:59 -0700 | [diff] [blame] | 119 | const std::vector<VkDescriptorBindingFlagsEXT> &GetBindingFlags() const { return binding_flags_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 120 | uint32_t GetDescriptorCountFromIndex(const uint32_t) const; |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 121 | uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const { |
| 122 | return GetDescriptorCountFromIndex(GetIndexFromBinding(binding)); |
| 123 | } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 124 | VkDescriptorType GetTypeFromIndex(const uint32_t) const; |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 125 | VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return GetTypeFromIndex(GetIndexFromBinding(binding)); } |
| 126 | VkShaderStageFlags GetStageFlagsFromIndex(const uint32_t) const; |
| 127 | VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t binding) const { |
| 128 | return GetStageFlagsFromIndex(GetIndexFromBinding(binding)); |
| 129 | } |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 130 | VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromIndex(const uint32_t) const; |
| 131 | VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromBinding(const uint32_t binding) const { |
| 132 | return GetDescriptorBindingFlagsFromIndex(GetIndexFromBinding(binding)); |
| 133 | } |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 134 | uint32_t GetIndexFromGlobalIndex(const uint32_t global_index) const; |
| 135 | VkDescriptorType GetTypeFromGlobalIndex(const uint32_t global_index) const { |
| 136 | return GetTypeFromIndex(GetIndexFromGlobalIndex(global_index)); |
| 137 | } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 138 | VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t) const; |
| 139 | VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t) const; |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 140 | // For a given binding and array index, return the corresponding index into the dynamic offset array |
| 141 | int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const { |
| 142 | auto dyn_off = binding_to_dynamic_array_idx_map_.find(binding); |
| 143 | if (dyn_off == binding_to_dynamic_array_idx_map_.end()) { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 144 | assert(0); // Requesting dyn offset for invalid binding/array idx pair |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 145 | return -1; |
| 146 | } |
| 147 | return dyn_off->second; |
| 148 | } |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame] | 149 | // For a particular binding, get the global index range |
| 150 | // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists |
| 151 | const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t) const; |
| 152 | |
| Mark Lobodzinski | 4aa479d | 2017-03-10 09:14:00 -0700 | [diff] [blame] | 153 | // Helper function to get the next valid binding for a descriptor |
| 154 | uint32_t GetNextValidBinding(const uint32_t) const; |
| Tobin Ehlis | 1f946f8 | 2016-05-05 12:03:44 -0600 | [diff] [blame] | 155 | // For a particular binding starting at offset and having update_count descriptors |
| 156 | // updated, verify that for any binding boundaries crossed, the update is consistent |
| 157 | bool VerifyUpdateConsistency(uint32_t, uint32_t, uint32_t, const char *, const VkDescriptorSet, std::string *) const; |
| Józef Kucia | f0c94d4 | 2017-10-25 22:15:22 +0200 | [diff] [blame] | 158 | bool IsPushDescriptor() const { return GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR; }; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 159 | |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 160 | struct BindingTypeStats { |
| 161 | uint32_t dynamic_buffer_count; |
| 162 | uint32_t non_dynamic_buffer_count; |
| 163 | uint32_t image_sampler_count; |
| 164 | }; |
| 165 | const BindingTypeStats &GetBindingTypeStats() const { return binding_type_stats_; } |
| 166 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 167 | private: |
| John Zulauf | 223b69d | 2018-11-09 16:00:59 -0700 | [diff] [blame] | 168 | // Only the first three data members are used for hash and equality checks, the other members are derived from them, and are |
| 169 | // used to speed up the various lookups/queries/validations |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 170 | VkDescriptorSetLayoutCreateFlags flags_; |
| 171 | std::vector<safe_VkDescriptorSetLayoutBinding> bindings_; |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 172 | std::vector<VkDescriptorBindingFlagsEXT> binding_flags_; |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 173 | |
| 174 | // Convenience data structures for rapid lookup of various descriptor set layout properties |
| John Zulauf | b6d7120 | 2017-12-22 16:47:09 -0700 | [diff] [blame] | 175 | std::set<uint32_t> non_empty_bindings_; // Containing non-emtpy bindings in numerical order |
| 176 | std::unordered_map<uint32_t, uint32_t> binding_to_index_map_; |
| 177 | // The following map allows an non-iterative lookup of a binding from a global index... |
| 178 | std::map<uint32_t, uint32_t> global_start_to_index_map_; // The index corresponding for a starting global (descriptor) index |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame] | 179 | std::unordered_map<uint32_t, IndexRange> binding_to_global_index_range_map_; // range is exclusive of .end |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 180 | // For a given binding map to associated index in the dynamic offset array |
| 181 | std::unordered_map<uint32_t, uint32_t> binding_to_dynamic_array_idx_map_; |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 182 | |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 183 | uint32_t binding_count_; // # of bindings in this layout |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 184 | uint32_t descriptor_count_; // total # descriptors in this layout |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 185 | uint32_t dynamic_descriptor_count_; |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 186 | BindingTypeStats binding_type_stats_; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 187 | }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 188 | |
| Jeremy Hayes | e4224ee | 2019-01-04 14:41:48 -0700 | [diff] [blame^] | 189 | static inline bool operator==(const DescriptorSetLayoutDef &lhs, const DescriptorSetLayoutDef &rhs) { |
| John Zulauf | 223b69d | 2018-11-09 16:00:59 -0700 | [diff] [blame] | 190 | bool result = (lhs.GetCreateFlags() == rhs.GetCreateFlags()) && (lhs.GetBindings() == rhs.GetBindings()) && |
| 191 | (lhs.GetBindingFlags() == rhs.GetBindingFlags()); |
| 192 | return result; |
| John Zulauf | d47d061 | 2018-02-16 13:00:34 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| John Zulauf | 34ebf27 | 2018-02-16 13:08:47 -0700 | [diff] [blame] | 195 | // Canonical dictionary of DSL definitions -- independent of device or handle |
| 196 | using DescriptorSetLayoutDict = hash_util::Dictionary<DescriptorSetLayoutDef, hash_util::HasHashMember<DescriptorSetLayoutDef>>; |
| 197 | using DescriptorSetLayoutId = DescriptorSetLayoutDict::Id; |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 198 | |
| 199 | class DescriptorSetLayout { |
| 200 | public: |
| 201 | // Constructors and destructor |
| 202 | DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info, const VkDescriptorSetLayout layout); |
| 203 | // Validate create info - should be called prior to creation |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 204 | static bool ValidateCreateInfo(const debug_report_data *, const VkDescriptorSetLayoutCreateInfo *, const bool, const uint32_t, |
| Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 205 | const bool, const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features, |
| 206 | const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features, |
| 207 | const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props); |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 208 | bool HasBinding(const uint32_t binding) const { return layout_id_->HasBinding(binding); } |
| 209 | // Return true if this layout is compatible with passed in layout from a pipelineLayout, |
| 210 | // else return false and update error_msg with description of incompatibility |
| 211 | bool IsCompatible(DescriptorSetLayout const *const, std::string *) const; |
| 212 | // Straightforward Get functions |
| 213 | VkDescriptorSetLayout GetDescriptorSetLayout() const { return layout_; }; |
| 214 | bool IsDestroyed() const { return layout_destroyed_; } |
| 215 | void MarkDestroyed() { layout_destroyed_ = true; } |
| Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 216 | const DescriptorSetLayoutDef *GetLayoutDef() const { return layout_id_.get(); } |
| 217 | DescriptorSetLayoutId GetLayoutId() const { return layout_id_; } |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 218 | uint32_t GetTotalDescriptorCount() const { return layout_id_->GetTotalDescriptorCount(); }; |
| 219 | uint32_t GetDynamicDescriptorCount() const { return layout_id_->GetDynamicDescriptorCount(); }; |
| 220 | uint32_t GetBindingCount() const { return layout_id_->GetBindingCount(); }; |
| 221 | VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return layout_id_->GetCreateFlags(); } |
| 222 | bool IsNextBindingConsistent(const uint32_t) const; |
| 223 | uint32_t GetIndexFromBinding(uint32_t binding) const { return layout_id_->GetIndexFromBinding(binding); } |
| 224 | // Various Get functions that can either be passed a binding#, which will |
| 225 | // be automatically translated into the appropriate index, or the index# can be passed in directly |
| 226 | uint32_t GetMaxBinding() const { return layout_id_->GetMaxBinding(); } |
| 227 | VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const { |
| 228 | return layout_id_->GetDescriptorSetLayoutBindingPtrFromIndex(index); |
| 229 | } |
| 230 | VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(uint32_t binding) const { |
| 231 | return layout_id_->GetDescriptorSetLayoutBindingPtrFromBinding(binding); |
| 232 | } |
| 233 | const std::vector<safe_VkDescriptorSetLayoutBinding> &GetBindings() const { return layout_id_->GetBindings(); } |
| 234 | uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return layout_id_->GetDescriptorCountFromIndex(index); } |
| 235 | uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const { |
| 236 | return layout_id_->GetDescriptorCountFromBinding(binding); |
| 237 | } |
| 238 | VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return layout_id_->GetTypeFromIndex(index); } |
| 239 | VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return layout_id_->GetTypeFromBinding(binding); } |
| 240 | VkShaderStageFlags GetStageFlagsFromIndex(const uint32_t index) const { return layout_id_->GetStageFlagsFromIndex(index); } |
| 241 | VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t binding) const { |
| 242 | return layout_id_->GetStageFlagsFromBinding(binding); |
| 243 | } |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 244 | VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromIndex(const uint32_t index) const { |
| 245 | return layout_id_->GetDescriptorBindingFlagsFromIndex(index); |
| 246 | } |
| 247 | VkDescriptorBindingFlagsEXT GetDescriptorBindingFlagsFromBinding(const uint32_t binding) const { |
| 248 | return layout_id_->GetDescriptorBindingFlagsFromBinding(binding); |
| 249 | } |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 250 | uint32_t GetIndexFromGlobalIndex(const uint32_t global_index) const { |
| 251 | return layout_id_->GetIndexFromGlobalIndex(global_index); |
| 252 | } |
| 253 | VkDescriptorType GetTypeFromGlobalIndex(const uint32_t global_index) const { |
| 254 | return GetTypeFromIndex(GetIndexFromGlobalIndex(global_index)); |
| 255 | } |
| 256 | VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t binding) const { |
| 257 | return layout_id_->GetImmutableSamplerPtrFromBinding(binding); |
| 258 | } |
| 259 | VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t index) const { |
| 260 | return layout_id_->GetImmutableSamplerPtrFromIndex(index); |
| 261 | } |
| 262 | // For a given binding and array index, return the corresponding index into the dynamic offset array |
| 263 | int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const { |
| 264 | return layout_id_->GetDynamicOffsetIndexFromBinding(binding); |
| 265 | } |
| 266 | // For a particular binding, get the global index range |
| 267 | // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists |
| 268 | const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t binding) const { |
| 269 | return layout_id_->GetGlobalIndexRangeFromBinding(binding); |
| 270 | } |
| 271 | // Helper function to get the next valid binding for a descriptor |
| 272 | uint32_t GetNextValidBinding(const uint32_t binding) const { return layout_id_->GetNextValidBinding(binding); } |
| 273 | // For a particular binding starting at offset and having update_count descriptors |
| 274 | // updated, verify that for any binding boundaries crossed, the update is consistent |
| 275 | bool VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count, const char *type, |
| 276 | const VkDescriptorSet set, std::string *error_msg) const { |
| 277 | return layout_id_->VerifyUpdateConsistency(current_binding, offset, update_count, type, set, error_msg); |
| 278 | } |
| 279 | bool IsPushDescriptor() const { return layout_id_->IsPushDescriptor(); } |
| 280 | |
| 281 | using BindingTypeStats = DescriptorSetLayoutDef::BindingTypeStats; |
| 282 | const BindingTypeStats &GetBindingTypeStats() const { return layout_id_->GetBindingTypeStats(); } |
| 283 | |
| 284 | private: |
| 285 | VkDescriptorSetLayout layout_; |
| 286 | bool layout_destroyed_; |
| 287 | DescriptorSetLayoutId layout_id_; |
| 288 | }; |
| 289 | |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 290 | /* |
| 291 | * Descriptor classes |
| 292 | * Descriptor is an abstract base class from which 5 separate descriptor types are derived. |
| 293 | * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per |
| 294 | * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*. |
| 295 | */ |
| 296 | |
| 297 | // Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass" |
| Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 298 | enum DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer, InlineUniform, AccelerationStructure }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 299 | |
| 300 | class Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 301 | public: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 302 | virtual ~Descriptor(){}; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 303 | virtual void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) = 0; |
| 304 | virtual void CopyUpdate(const Descriptor *) = 0; |
| Tobin Ehlis | 8020eea | 2016-08-17 11:10:41 -0600 | [diff] [blame] | 305 | // Create binding between resources of this descriptor and given cb_node |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 306 | virtual void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) = 0; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 307 | virtual DescriptorClass GetClass() const { return descriptor_class; }; |
| 308 | // Special fast-path check for SamplerDescriptors that are immutable |
| 309 | virtual bool IsImmutableSampler() const { return false; }; |
| 310 | // Check for dynamic descriptor type |
| 311 | virtual bool IsDynamic() const { return false; }; |
| 312 | // Check for storage descriptor type |
| 313 | virtual bool IsStorage() const { return false; }; |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 314 | bool updated; // Has descriptor been updated? |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 315 | DescriptorClass descriptor_class; |
| 316 | }; |
| 317 | // Shared helper functions - These are useful because the shared sampler image descriptor type |
| 318 | // performs common functions with both sampler and image descriptors so they can share their common functions |
| Tobin Ehlis | e2f8029 | 2016-06-02 10:08:53 -0600 | [diff] [blame] | 319 | bool ValidateSampler(const VkSampler, const core_validation::layer_data *); |
| John Zulauf | b45fdc3 | 2018-10-12 15:14:17 -0600 | [diff] [blame] | 320 | bool ValidateImageUpdate(VkImageView, VkImageLayout, VkDescriptorType, const core_validation::layer_data *, const char *func_name, |
| 321 | std::string *, std::string *); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 322 | |
| 323 | class SamplerDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 324 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 325 | SamplerDescriptor(const VkSampler *); |
| 326 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 327 | void CopyUpdate(const Descriptor *) override; |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 328 | void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 329 | virtual bool IsImmutableSampler() const override { return immutable_; }; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 330 | VkSampler GetSampler() const { return sampler_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 331 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 332 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 333 | // bool ValidateSampler(const VkSampler) const; |
| 334 | VkSampler sampler_; |
| 335 | bool immutable_; |
| Tobin Ehlis | 546326f | 2016-04-26 11:06:05 -0600 | [diff] [blame] | 336 | }; |
| 337 | |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 338 | class ImageSamplerDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 339 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 340 | ImageSamplerDescriptor(const VkSampler *); |
| 341 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 342 | void CopyUpdate(const Descriptor *) override; |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 343 | void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | c962515 | 2016-05-24 16:47:36 -0600 | [diff] [blame] | 344 | virtual bool IsImmutableSampler() const override { return immutable_; }; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 345 | VkSampler GetSampler() const { return sampler_; } |
| 346 | VkImageView GetImageView() const { return image_view_; } |
| 347 | VkImageLayout GetImageLayout() const { return image_layout_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 348 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 349 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 350 | VkSampler sampler_; |
| 351 | bool immutable_; |
| 352 | VkImageView image_view_; |
| 353 | VkImageLayout image_layout_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | class ImageDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 357 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 358 | ImageDescriptor(const VkDescriptorType); |
| 359 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 360 | void CopyUpdate(const Descriptor *) override; |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 361 | void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Norbert Nopper | 419a109 | 2016-05-15 19:19:41 +0200 | [diff] [blame] | 362 | virtual bool IsStorage() const override { return storage_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 363 | VkImageView GetImageView() const { return image_view_; } |
| 364 | VkImageLayout GetImageLayout() const { return image_layout_; } |
| 365 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 366 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 367 | bool storage_; |
| 368 | VkImageView image_view_; |
| 369 | VkImageLayout image_layout_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | class TexelDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 373 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 374 | TexelDescriptor(const VkDescriptorType); |
| 375 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 376 | void CopyUpdate(const Descriptor *) override; |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 377 | void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | f490f2e | 2016-05-17 06:43:48 -0600 | [diff] [blame] | 378 | virtual bool IsStorage() const override { return storage_; } |
| 379 | VkBufferView GetBufferView() const { return buffer_view_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 380 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 381 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 382 | VkBufferView buffer_view_; |
| 383 | bool storage_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | class BufferDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 387 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 388 | BufferDescriptor(const VkDescriptorType); |
| 389 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 390 | void CopyUpdate(const Descriptor *) override; |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 391 | void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 392 | virtual bool IsDynamic() const override { return dynamic_; } |
| 393 | virtual bool IsStorage() const override { return storage_; } |
| 394 | VkBuffer GetBuffer() const { return buffer_; } |
| 395 | VkDeviceSize GetOffset() const { return offset_; } |
| 396 | VkDeviceSize GetRange() const { return range_; } |
| 397 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 398 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 399 | bool storage_; |
| 400 | bool dynamic_; |
| 401 | VkBuffer buffer_; |
| 402 | VkDeviceSize offset_; |
| 403 | VkDeviceSize range_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 404 | }; |
| Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 405 | |
| 406 | class InlineUniformDescriptor : public Descriptor { |
| 407 | public: |
| Dave Houlton | 142c4cb | 2018-10-17 15:04:41 -0600 | [diff] [blame] | 408 | InlineUniformDescriptor(const VkDescriptorType) { |
| 409 | updated = false; |
| 410 | descriptor_class = InlineUniform; |
| 411 | } |
| Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 412 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; } |
| Dave Houlton | 142c4cb | 2018-10-17 15:04:41 -0600 | [diff] [blame] | 413 | void CopyUpdate(const Descriptor *) override { updated = true; } |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 414 | void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) override {} |
| Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 415 | }; |
| 416 | |
| Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 417 | class AccelerationStructureDescriptor : public Descriptor { |
| 418 | public: |
| Dave Houlton | 142c4cb | 2018-10-17 15:04:41 -0600 | [diff] [blame] | 419 | AccelerationStructureDescriptor(const VkDescriptorType) { |
| 420 | updated = false; |
| 421 | descriptor_class = AccelerationStructure; |
| 422 | } |
| Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 423 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override { updated = true; } |
| Dave Houlton | 142c4cb | 2018-10-17 15:04:41 -0600 | [diff] [blame] | 424 | void CopyUpdate(const Descriptor *) override { updated = true; } |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 425 | void UpdateDrawState(core_validation::layer_data *, GLOBAL_CB_NODE *) override {} |
| Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 426 | }; |
| 427 | |
| Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame] | 428 | // Structs to contain common elements that need to be shared between Validate* and Perform* calls below |
| 429 | struct AllocateDescriptorSetsData { |
| Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 430 | std::map<uint32_t, uint32_t> required_descriptors_by_type; |
| Tobin Ehlis | a8e46e7 | 2017-06-21 10:16:10 -0600 | [diff] [blame] | 431 | std::vector<std::shared_ptr<DescriptorSetLayout const>> layout_nodes; |
| Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame] | 432 | AllocateDescriptorSetsData(uint32_t); |
| 433 | }; |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 434 | // Helper functions for descriptor set functions that cross multiple sets |
| 435 | // "Validate" will make sure an update is ok without actually performing it |
| Tobin Ehlis | 6a72dc7 | 2016-06-01 16:41:17 -0600 | [diff] [blame] | 436 | bool ValidateUpdateDescriptorSets(const debug_report_data *, const core_validation::layer_data *, uint32_t, |
| John Zulauf | b45fdc3 | 2018-10-12 15:14:17 -0600 | [diff] [blame] | 437 | const VkWriteDescriptorSet *, uint32_t, const VkCopyDescriptorSet *, const char *func_name); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 438 | // "Perform" does the update with the assumption that ValidateUpdateDescriptorSets() has passed for the given update |
| Tobin Ehlis | 6a72dc7 | 2016-06-01 16:41:17 -0600 | [diff] [blame] | 439 | void PerformUpdateDescriptorSets(const core_validation::layer_data *, uint32_t, const VkWriteDescriptorSet *, uint32_t, |
| 440 | const VkCopyDescriptorSet *); |
| John Zulauf | b845eb2 | 2018-10-12 11:41:06 -0600 | [diff] [blame] | 441 | |
| 442 | // Helper class to encapsulate the descriptor update template decoding logic |
| 443 | struct DecodedTemplateUpdate { |
| 444 | std::vector<VkWriteDescriptorSet> desc_writes; |
| 445 | std::vector<VkWriteDescriptorSetInlineUniformBlockEXT> inline_infos; |
| 446 | DecodedTemplateUpdate(layer_data *device_data, VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state, |
| John Zulauf | 1d27e0a | 2018-11-05 10:12:48 -0700 | [diff] [blame] | 447 | const void *pData, VkDescriptorSetLayout push_layout = VK_NULL_HANDLE); |
| John Zulauf | b845eb2 | 2018-10-12 11:41:06 -0600 | [diff] [blame] | 448 | }; |
| John Zulauf | b45fdc3 | 2018-10-12 15:14:17 -0600 | [diff] [blame] | 449 | // Helper wrapping ValidateUpdateDescriptorSets, updating via templates |
| 450 | bool ValidateUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet, |
| 451 | const TEMPLATE_STATE *template_state, const void *pData); |
| 452 | // Helper wrapping PerformUpdateDescriptorSets, updating via templates |
| 453 | void PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *, VkDescriptorSet, const TEMPLATE_STATE *, const void *); |
| Tobin Ehlis | f320b19 | 2017-03-14 11:22:50 -0600 | [diff] [blame] | 454 | // Update the common AllocateDescriptorSetsData struct which can then be shared between Validate* and Perform* funcs below |
| 455 | void UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *, |
| 456 | AllocateDescriptorSetsData *); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 457 | // Validate that Allocation state is ok |
| Tobin Ehlis | f320b19 | 2017-03-14 11:22:50 -0600 | [diff] [blame] | 458 | bool ValidateAllocateDescriptorSets(const core_validation::layer_data *, const VkDescriptorSetAllocateInfo *, |
| 459 | const AllocateDescriptorSetsData *); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 460 | // Update state based on allocating new descriptorsets |
| Tobin Ehlis | 997b258 | 2016-06-02 08:43:37 -0600 | [diff] [blame] | 461 | void PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *, const VkDescriptorSet *, const AllocateDescriptorSetsData *, |
| Tobin Ehlis | bd711bd | 2016-10-12 14:27:30 -0600 | [diff] [blame] | 462 | std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *, |
| Tobin Ehlis | 997b258 | 2016-06-02 08:43:37 -0600 | [diff] [blame] | 463 | std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *, |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 464 | core_validation::layer_data *); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 465 | |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 466 | /* |
| 467 | * DescriptorSet class |
| 468 | * |
| 469 | * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set). |
| 470 | * A set has an underlying layout which defines the bindings in the set and the |
| 471 | * types and numbers of descriptors in each descriptor slot. Most of the layout |
| 472 | * interfaces are exposed through identically-named functions in the set class. |
| 473 | * Please refer to the DescriptorSetLayout comment above for a description of |
| 474 | * index, binding, and global index. |
| 475 | * |
| 476 | * At construction a vector of Descriptor* is created with types corresponding to the |
| 477 | * layout. The primary operation performed on the descriptors is to update them |
| 478 | * via write or copy updates, and validate that the update contents are correct. |
| 479 | * In order to validate update contents, the DescriptorSet stores a bunch of ptrs |
| 480 | * to data maps where various Vulkan objects can be looked up. The management of |
| 481 | * those maps is performed externally. The set class relies on their contents to |
| 482 | * be correct at the time of update. |
| 483 | */ |
| Tobin Ehlis | 05be5df | 2016-05-05 08:25:02 -0600 | [diff] [blame] | 484 | class DescriptorSet : public BASE_NODE { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 485 | public: |
| Tobin Ehlis | 452b453 | 2017-06-21 09:56:13 -0600 | [diff] [blame] | 486 | DescriptorSet(const VkDescriptorSet, const VkDescriptorPool, const std::shared_ptr<DescriptorSetLayout const> &, |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 487 | uint32_t variable_count, core_validation::layer_data *); |
| Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 488 | ~DescriptorSet(); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 489 | // A number of common Get* functions that return data based on layout from which this set was created |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 490 | uint32_t GetTotalDescriptorCount() const { return p_layout_->GetTotalDescriptorCount(); }; |
| 491 | uint32_t GetDynamicDescriptorCount() const { return p_layout_->GetDynamicDescriptorCount(); }; |
| 492 | uint32_t GetBindingCount() const { return p_layout_->GetBindingCount(); }; |
| 493 | VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return p_layout_->GetTypeFromIndex(index); }; |
| 494 | VkDescriptorType GetTypeFromGlobalIndex(const uint32_t index) const { return p_layout_->GetTypeFromGlobalIndex(index); }; |
| 495 | VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return p_layout_->GetTypeFromBinding(binding); }; |
| 496 | uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return p_layout_->GetDescriptorCountFromIndex(index); }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 497 | uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const { |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 498 | return p_layout_->GetDescriptorCountFromBinding(binding); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 499 | }; |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 500 | // Return index into dynamic offset array for given binding |
| 501 | int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const { |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 502 | return p_layout_->GetDynamicOffsetIndexFromBinding(binding); |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 503 | } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 504 | // Return true if given binding is present in this set |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 505 | bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 506 | // Is this set compatible with the given layout? |
| Tobin Ehlis | 6dc57dd | 2017-06-21 10:08:52 -0600 | [diff] [blame] | 507 | bool IsCompatible(DescriptorSetLayout const *const, std::string *) const; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 508 | // For given bindings validate state at time of draw is correct, returning false on error and writing error details into string* |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 509 | bool ValidateDrawState(const std::map<uint32_t, descriptor_req> &, const std::vector<uint32_t> &, GLOBAL_CB_NODE *, |
| Tobin Ehlis | c826645 | 2017-04-07 12:20:30 -0600 | [diff] [blame] | 510 | const char *caller, std::string *) const; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 511 | // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number |
| 512 | // of objects inserted |
| Tobin Ehlis | cebc4c0 | 2016-08-22 10:10:43 -0600 | [diff] [blame] | 513 | uint32_t GetStorageUpdates(const std::map<uint32_t, descriptor_req> &, std::unordered_set<VkBuffer> *, |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 514 | std::unordered_set<VkImageView> *) const; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 515 | |
| John Zulauf | 4e7bcb5 | 2018-11-02 10:46:30 -0600 | [diff] [blame] | 516 | std::string StringifySetAndLayout() const; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 517 | // Descriptor Update functions. These functions validate state and perform update separately |
| John Zulauf | 1d27e0a | 2018-11-05 10:12:48 -0700 | [diff] [blame] | 518 | // Validate contents of a push descriptor update |
| 519 | bool ValidatePushDescriptorsUpdate(const debug_report_data *report_data, uint32_t write_count, |
| 520 | const VkWriteDescriptorSet *p_wds, const char *func_name); |
| 521 | // Perform a push update whose contents were just validated using ValidatePushDescriptorsUpdate |
| 522 | void PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds); |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 523 | // Validate contents of a WriteUpdate |
| John Zulauf | b45fdc3 | 2018-10-12 15:14:17 -0600 | [diff] [blame] | 524 | bool ValidateWriteUpdate(const debug_report_data *, const VkWriteDescriptorSet *, const char *, std::string *, std::string *); |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 525 | // Perform a WriteUpdate whose contents were just validated using ValidateWriteUpdate |
| 526 | void PerformWriteUpdate(const VkWriteDescriptorSet *); |
| 527 | // Validate contents of a CopyUpdate |
| John Zulauf | b45fdc3 | 2018-10-12 15:14:17 -0600 | [diff] [blame] | 528 | bool ValidateCopyUpdate(const debug_report_data *, const VkCopyDescriptorSet *, const DescriptorSet *, const char *func_name, |
| 529 | std::string *, std::string *); |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 530 | // Perform a CopyUpdate whose contents were just validated using ValidateCopyUpdate |
| 531 | void PerformCopyUpdate(const VkCopyDescriptorSet *, const DescriptorSet *); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 532 | |
| John Zulauf | 1f8174b | 2018-02-16 12:58:37 -0700 | [diff] [blame] | 533 | const std::shared_ptr<DescriptorSetLayout const> GetLayout() const { return p_layout_; }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 534 | VkDescriptorSet GetSet() const { return set_; }; |
| 535 | // Return unordered_set of all command buffers that this set is bound to |
| Tobin Ehlis | 2556f5b | 2016-06-24 17:22:16 -0600 | [diff] [blame] | 536 | std::unordered_set<GLOBAL_CB_NODE *> GetBoundCmdBuffers() const { return cb_bindings; } |
| John Zulauf | 6f3d2bd | 2018-10-29 17:08:42 -0600 | [diff] [blame] | 537 | // Bind given cmd_buffer to this descriptor set and |
| 538 | // update CB image layout map with image/imagesampler descriptor image layouts |
| 539 | void UpdateDrawState(GLOBAL_CB_NODE *, const std::map<uint32_t, descriptor_req> &); |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 540 | |
| 541 | // Track work that has been bound or validated to avoid duplicate work, important when large descriptor arrays |
| 542 | // are present |
| 543 | typedef std::unordered_set<uint32_t> TrackedBindings; |
| 544 | static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req, |
| 545 | BindingReqMap *out_req, TrackedBindings *set); |
| 546 | static void FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair, const BindingReqMap &in_req, |
| 547 | BindingReqMap *out_req, TrackedBindings *set, uint32_t limit); |
| 548 | void FilterAndTrackBindingReqs(GLOBAL_CB_NODE *, const BindingReqMap &in_req, BindingReqMap *out_req); |
| 549 | void FilterAndTrackBindingReqs(GLOBAL_CB_NODE *, PIPELINE_STATE *, const BindingReqMap &in_req, BindingReqMap *out_req); |
| 550 | void ClearCachedDynamicDescriptorValidation(GLOBAL_CB_NODE *cb_state) { cached_validation_[cb_state].dynamic_buffers.clear(); } |
| 551 | void ClearCachedValidation(GLOBAL_CB_NODE *cb_state) { cached_validation_.erase(cb_state); } |
| Tobin Ehlis | 2556f5b | 2016-06-24 17:22:16 -0600 | [diff] [blame] | 552 | // If given cmd_buffer is in the cb_bindings set, remove it |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 553 | void RemoveBoundCommandBuffer(GLOBAL_CB_NODE *cb_node) { |
| 554 | cb_bindings.erase(cb_node); |
| 555 | ClearCachedValidation(cb_node); |
| 556 | } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 557 | VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const { |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 558 | return p_layout_->GetImmutableSamplerPtrFromBinding(index); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 559 | }; |
| 560 | // For a particular binding, get the global index |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame] | 561 | const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t binding) const { |
| 562 | return p_layout_->GetGlobalIndexRangeFromBinding(binding); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 563 | }; |
| 564 | // Return true if any part of set has ever been updated |
| 565 | bool IsUpdated() const { return some_update_; }; |
| Józef Kucia | f0c94d4 | 2017-10-25 22:15:22 +0200 | [diff] [blame] | 566 | bool IsPushDescriptor() const { return p_layout_->IsPushDescriptor(); }; |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 567 | bool IsVariableDescriptorCount(uint32_t binding) const { |
| 568 | return !!(p_layout_->GetDescriptorBindingFlagsFromBinding(binding) & |
| 569 | VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT); |
| 570 | } |
| 571 | uint32_t GetVariableDescriptorCount() const { return variable_count_; } |
| 572 | DESCRIPTOR_POOL_STATE *GetPoolState() const { return pool_state_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 573 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 574 | private: |
| John Zulauf | b45fdc3 | 2018-10-12 15:14:17 -0600 | [diff] [blame] | 575 | bool VerifyWriteUpdateContents(const VkWriteDescriptorSet *, const uint32_t, const char *, std::string *, std::string *) const; |
| 576 | bool VerifyCopyUpdateContents(const VkCopyDescriptorSet *, const DescriptorSet *, VkDescriptorType, uint32_t, const char *, |
| 577 | std::string *, std::string *) const; |
| Dave Houlton | 00c154e | 2018-05-24 13:20:50 -0600 | [diff] [blame] | 578 | bool ValidateBufferUsage(BUFFER_STATE const *, VkDescriptorType, std::string *, std::string *) const; |
| John Zulauf | b45fdc3 | 2018-10-12 15:14:17 -0600 | [diff] [blame] | 579 | bool ValidateBufferUpdate(VkDescriptorBufferInfo const *, VkDescriptorType, const char *, std::string *, std::string *) const; |
| Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 580 | // Private helper to set all bound cmd buffers to INVALID state |
| 581 | void InvalidateBoundCmdBuffers(); |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 582 | bool some_update_; // has any part of the set ever been updated? |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 583 | VkDescriptorSet set_; |
| Tobin Ehlis | 7ca20be | 2016-10-12 15:09:16 -0600 | [diff] [blame] | 584 | DESCRIPTOR_POOL_STATE *pool_state_; |
| Tobin Ehlis | 452b453 | 2017-06-21 09:56:13 -0600 | [diff] [blame] | 585 | const std::shared_ptr<DescriptorSetLayout const> p_layout_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 586 | std::vector<std::unique_ptr<Descriptor>> descriptors_; |
| Tobin Ehlis | 3d15c4a | 2016-06-02 13:04:47 -0600 | [diff] [blame] | 587 | // Ptr to device data used for various data look-ups |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 588 | core_validation::layer_data *const device_data_; |
| Tobin Ehlis | c3b6c4c | 2017-02-02 17:26:40 -0700 | [diff] [blame] | 589 | const VkPhysicalDeviceLimits limits_; |
| Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 590 | uint32_t variable_count_; |
| John Zulauf | 48a6a70 | 2017-12-22 17:14:54 -0700 | [diff] [blame] | 591 | |
| 592 | // Cached binding and validation support: |
| 593 | // |
| 594 | // For the lifespan of a given command buffer recording, do lazy evaluation, caching, and dirtying of |
| 595 | // expensive validation operation (typically per-draw) |
| 596 | typedef std::unordered_map<GLOBAL_CB_NODE *, TrackedBindings> TrackedBindingMap; |
| 597 | typedef std::unordered_map<PIPELINE_STATE *, TrackedBindingMap> ValidatedBindings; |
| 598 | // Track the validation caching of bindings vs. the command buffer and draw state |
| 599 | typedef std::unordered_map<uint32_t, GLOBAL_CB_NODE::ImageLayoutUpdateCount> VersionedBindings; |
| 600 | struct CachedValidation { |
| 601 | TrackedBindings command_binding_and_usage; // Persistent for the life of the recording |
| 602 | TrackedBindings non_dynamic_buffers; // Persistent for the life of the recording |
| 603 | TrackedBindings dynamic_buffers; // Dirtied (flushed) each BindDescriptorSet |
| 604 | std::unordered_map<PIPELINE_STATE *, VersionedBindings> image_samplers; // Tested vs. changes to CB's ImageLayout |
| 605 | }; |
| 606 | typedef std::unordered_map<GLOBAL_CB_NODE *, CachedValidation> CachedValidationMap; |
| 607 | // Image and ImageView bindings are validated per pipeline and not invalidate by repeated binding |
| 608 | CachedValidationMap cached_validation_; |
| 609 | }; |
| 610 | // For the "bindless" style resource usage with many descriptors, need to optimize binding and validation |
| 611 | class PrefilterBindRequestMap { |
| 612 | public: |
| 613 | static const uint32_t kManyDescriptors_ = 64; // TODO base this number on measured data |
| 614 | std::unique_ptr<BindingReqMap> filtered_map_; |
| 615 | const BindingReqMap &orig_map_; |
| 616 | |
| 617 | PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, GLOBAL_CB_NODE *cb_state); |
| 618 | PrefilterBindRequestMap(DescriptorSet &ds, const BindingReqMap &in_map, GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *); |
| 619 | const BindingReqMap &Map() const { return (filtered_map_) ? *filtered_map_ : orig_map_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 620 | }; |
| Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 621 | } // namespace cvdescriptorset |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 622 | #endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_ |