| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2016 Valve Corporation |
| 3 | * Copyright (c) 2015-2016 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2016 Google Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Tobin Ehlis <tobine@google.com> |
| John 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 | bf98b69 | 2016-10-06 12:58:06 -0600 | [diff] [blame] | 25 | #include "vk_validation_error_messages.h" |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 26 | #include "core_validation_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> |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 34 | #include <unordered_map> |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 35 | #include <unordered_set> |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
| Tobin Ehlis | 58c884f | 2017-02-08 12:15:27 -0700 | [diff] [blame] | 38 | using core_validation::layer_data; |
| 39 | |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 40 | // Descriptor Data structures |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame^] | 41 | namespace cvdescriptorset { |
| 42 | |
| 43 | // Index range for global indices below, end is exclusive, i.e. [start,end) |
| 44 | struct 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 | }; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * DescriptorSetLayout class |
| 53 | * |
| 54 | * Overview - This class encapsulates the Vulkan VkDescriptorSetLayout data (layout). |
| 55 | * A layout consists of some number of bindings, each of which has a binding#, a |
| 56 | * type, descriptor count, stage flags, and pImmutableSamplers. |
| 57 | * |
| 58 | * Index vs Binding - A layout is created with an array of VkDescriptorSetLayoutBinding |
| 59 | * 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] | 60 | * The binding#, then, is decoupled from VkDescriptorSetLayoutBinding index, which allows |
| 61 | * bindings to be defined out-of-order. This DescriptorSetLayout class, however, stores |
| 62 | * the bindings internally in-order. This is useful for operations which may "roll over" |
| 63 | * from a single binding to the next consecutive binding. |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 64 | * |
| Tobin Ehlis | 9637fb2 | 2016-12-12 15:59:34 -0700 | [diff] [blame] | 65 | * Note that although the bindings are stored in-order, there still may be "gaps" in the |
| 66 | * binding#. For example, if the binding creation order is 8, 7, 10, 3, 4, then the |
| 67 | * internal binding array will have five entries stored in binding order 3, 4, 7, 8, 10. |
| 68 | * To process all of the bindings in a layout you can iterate from 0 to GetBindingCount() |
| 69 | * and use the Get*FromIndex() functions for each index. To just process a single binding, |
| 70 | * use the Get*FromBinding() functions. |
| 71 | * |
| 72 | * 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] | 73 | * This class also has the concept of a Global Index. For the global index functions, |
| 74 | * there are as many global indices as there are descriptors in the layout. |
| 75 | * 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] | 76 | * descriptor 0 of of the lowest binding# is index 0 and each descriptor in the layout |
| 77 | * increments from there. So if the lowest binding# in this example had descriptorCount of |
| 78 | * 10, then the GlobalStartIndex of the 2nd lowest binding# will be 10 where 0-9 are the |
| 79 | * global indices for the lowest binding#. |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 80 | */ |
| 81 | class DescriptorSetLayout { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 82 | public: |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 83 | // Constructors and destructor |
| Tobin Ehlis | 154c269 | 2016-10-25 09:36:53 -0600 | [diff] [blame] | 84 | DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info, const VkDescriptorSetLayout layout); |
| 85 | // Validate create info - should be called prior to creation |
| 86 | static bool ValidateCreateInfo(debug_report_data *, const VkDescriptorSetLayoutCreateInfo *); |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 87 | // Straightforward Get functions |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 88 | VkDescriptorSetLayout GetDescriptorSetLayout() const { return layout_; }; |
| 89 | uint32_t GetTotalDescriptorCount() const { return descriptor_count_; }; |
| 90 | uint32_t GetDynamicDescriptorCount() const { return dynamic_descriptor_count_; }; |
| Józef Kucia | 1bb0097 | 2017-09-10 11:24:08 +0200 | [diff] [blame] | 91 | VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return flags_; } |
| Tobin Ehlis | f922ef8 | 2016-11-30 10:19:14 -0700 | [diff] [blame] | 92 | // 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] | 93 | uint32_t GetBindingCount() const { return binding_count_; }; |
| 94 | // Fill passed-in set with bindings |
| 95 | void FillBindingSet(std::unordered_set<uint32_t> *) const; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 96 | // Return true if given binding is present in this layout |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 97 | bool HasBinding(const uint32_t binding) const { return binding_to_index_map_.count(binding) > 0; }; |
| Tobin Ehlis | b1861d9 | 2017-07-06 16:50:10 -0600 | [diff] [blame] | 98 | // Return true if this layout is compatible with passed in layout from a pipelineLayout, |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 99 | // else return false and update error_msg with description of incompatibility |
| Tobin Ehlis | 6dc57dd | 2017-06-21 10:08:52 -0600 | [diff] [blame] | 100 | bool IsCompatible(DescriptorSetLayout const *const, std::string *) const; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 101 | // Return true if binding 1 beyond given exists and has same type, stageFlags & immutable sampler use |
| 102 | bool IsNextBindingConsistent(const uint32_t) const; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 103 | // Various Get functions that can either be passed a binding#, which will |
| Tobin Ehlis | 9637fb2 | 2016-12-12 15:59:34 -0700 | [diff] [blame] | 104 | // be automatically translated into the appropriate index, or the index# can be passed in directly |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 105 | VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t) const; |
| 106 | VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t) const; |
| 107 | uint32_t GetDescriptorCountFromBinding(const uint32_t) const; |
| 108 | uint32_t GetDescriptorCountFromIndex(const uint32_t) const; |
| 109 | VkDescriptorType GetTypeFromBinding(const uint32_t) const; |
| 110 | VkDescriptorType GetTypeFromIndex(const uint32_t) const; |
| 111 | VkDescriptorType GetTypeFromGlobalIndex(const uint32_t) const; |
| 112 | VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t) const; |
| 113 | VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t) const; |
| 114 | VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t) const; |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 115 | // For a given binding and array index, return the corresponding index into the dynamic offset array |
| 116 | int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const { |
| 117 | auto dyn_off = binding_to_dynamic_array_idx_map_.find(binding); |
| 118 | if (dyn_off == binding_to_dynamic_array_idx_map_.end()) { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 119 | assert(0); // Requesting dyn offset for invalid binding/array idx pair |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 120 | return -1; |
| 121 | } |
| 122 | return dyn_off->second; |
| 123 | } |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame^] | 124 | // For a particular binding, get the global index range |
| 125 | // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists |
| 126 | const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t) const; |
| 127 | |
| Mark Lobodzinski | 4aa479d | 2017-03-10 09:14:00 -0700 | [diff] [blame] | 128 | // Helper function to get the next valid binding for a descriptor |
| 129 | uint32_t GetNextValidBinding(const uint32_t) const; |
| Tobin Ehlis | 1f946f8 | 2016-05-05 12:03:44 -0600 | [diff] [blame] | 130 | // For a particular binding starting at offset and having update_count descriptors |
| 131 | // updated, verify that for any binding boundaries crossed, the update is consistent |
| 132 | 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] | 133 | 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] | 134 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 135 | private: |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 136 | VkDescriptorSetLayout layout_; |
| Tobin Ehlis | f922ef8 | 2016-11-30 10:19:14 -0700 | [diff] [blame] | 137 | std::map<uint32_t, uint32_t> binding_to_index_map_; |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame^] | 138 | 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] | 139 | // For a given binding map to associated index in the dynamic offset array |
| 140 | std::unordered_map<uint32_t, uint32_t> binding_to_dynamic_array_idx_map_; |
| Józef Kucia | 1bb0097 | 2017-09-10 11:24:08 +0200 | [diff] [blame] | 141 | VkDescriptorSetLayoutCreateFlags flags_; |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 142 | uint32_t binding_count_; // # of bindings in this layout |
| Tobin Ehlis | 664e601 | 2016-05-05 11:04:44 -0600 | [diff] [blame] | 143 | std::vector<safe_VkDescriptorSetLayoutBinding> bindings_; |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 144 | uint32_t descriptor_count_; // total # descriptors in this layout |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 145 | uint32_t dynamic_descriptor_count_; |
| 146 | }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Descriptor classes |
| 150 | * Descriptor is an abstract base class from which 5 separate descriptor types are derived. |
| 151 | * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per |
| 152 | * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*. |
| 153 | */ |
| 154 | |
| 155 | // Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass" |
| Mark Lobodzinski | 0978f5f | 2016-05-19 17:23:38 -0600 | [diff] [blame] | 156 | enum DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 157 | |
| 158 | class Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 159 | public: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 160 | virtual ~Descriptor(){}; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 161 | virtual void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) = 0; |
| 162 | virtual void CopyUpdate(const Descriptor *) = 0; |
| Tobin Ehlis | 8020eea | 2016-08-17 11:10:41 -0600 | [diff] [blame] | 163 | // Create binding between resources of this descriptor and given cb_node |
| 164 | virtual void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) = 0; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 165 | virtual DescriptorClass GetClass() const { return descriptor_class; }; |
| 166 | // Special fast-path check for SamplerDescriptors that are immutable |
| 167 | virtual bool IsImmutableSampler() const { return false; }; |
| 168 | // Check for dynamic descriptor type |
| 169 | virtual bool IsDynamic() const { return false; }; |
| 170 | // Check for storage descriptor type |
| 171 | virtual bool IsStorage() const { return false; }; |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 172 | bool updated; // Has descriptor been updated? |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 173 | DescriptorClass descriptor_class; |
| 174 | }; |
| 175 | // Shared helper functions - These are useful because the shared sampler image descriptor type |
| 176 | // 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] | 177 | bool ValidateSampler(const VkSampler, const core_validation::layer_data *); |
| Tobin Ehlis | 75f04ec | 2016-10-06 17:43:11 -0600 | [diff] [blame] | 178 | bool ValidateImageUpdate(VkImageView, VkImageLayout, VkDescriptorType, const core_validation::layer_data *, |
| 179 | UNIQUE_VALIDATION_ERROR_CODE *, std::string *); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 180 | |
| 181 | class SamplerDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 182 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 183 | SamplerDescriptor(const VkSampler *); |
| 184 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 185 | void CopyUpdate(const Descriptor *) override; |
| Tobin Ehlis | 8020eea | 2016-08-17 11:10:41 -0600 | [diff] [blame] | 186 | void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 187 | virtual bool IsImmutableSampler() const override { return immutable_; }; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 188 | VkSampler GetSampler() const { return sampler_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 189 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 190 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 191 | // bool ValidateSampler(const VkSampler) const; |
| 192 | VkSampler sampler_; |
| 193 | bool immutable_; |
| Tobin Ehlis | 546326f | 2016-04-26 11:06:05 -0600 | [diff] [blame] | 194 | }; |
| 195 | |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 196 | class ImageSamplerDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 197 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 198 | ImageSamplerDescriptor(const VkSampler *); |
| 199 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 200 | void CopyUpdate(const Descriptor *) override; |
| Tobin Ehlis | 8020eea | 2016-08-17 11:10:41 -0600 | [diff] [blame] | 201 | void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | c962515 | 2016-05-24 16:47:36 -0600 | [diff] [blame] | 202 | virtual bool IsImmutableSampler() const override { return immutable_; }; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 203 | VkSampler GetSampler() const { return sampler_; } |
| 204 | VkImageView GetImageView() const { return image_view_; } |
| 205 | VkImageLayout GetImageLayout() const { return image_layout_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 206 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 207 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 208 | VkSampler sampler_; |
| 209 | bool immutable_; |
| 210 | VkImageView image_view_; |
| 211 | VkImageLayout image_layout_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | class ImageDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 215 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 216 | ImageDescriptor(const VkDescriptorType); |
| 217 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 218 | void CopyUpdate(const Descriptor *) override; |
| Tobin Ehlis | 8020eea | 2016-08-17 11:10:41 -0600 | [diff] [blame] | 219 | void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Norbert Nopper | 419a109 | 2016-05-15 19:19:41 +0200 | [diff] [blame] | 220 | virtual bool IsStorage() const override { return storage_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 221 | VkImageView GetImageView() const { return image_view_; } |
| 222 | VkImageLayout GetImageLayout() const { return image_layout_; } |
| 223 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 224 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 225 | bool storage_; |
| 226 | VkImageView image_view_; |
| 227 | VkImageLayout image_layout_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | class TexelDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 231 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 232 | TexelDescriptor(const VkDescriptorType); |
| 233 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 234 | void CopyUpdate(const Descriptor *) override; |
| Tobin Ehlis | 8020eea | 2016-08-17 11:10:41 -0600 | [diff] [blame] | 235 | void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | f490f2e | 2016-05-17 06:43:48 -0600 | [diff] [blame] | 236 | virtual bool IsStorage() const override { return storage_; } |
| 237 | VkBufferView GetBufferView() const { return buffer_view_; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 238 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 239 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 240 | VkBufferView buffer_view_; |
| 241 | bool storage_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | class BufferDescriptor : public Descriptor { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 245 | public: |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 246 | BufferDescriptor(const VkDescriptorType); |
| 247 | void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override; |
| 248 | void CopyUpdate(const Descriptor *) override; |
| Tobin Ehlis | 8020eea | 2016-08-17 11:10:41 -0600 | [diff] [blame] | 249 | void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 250 | virtual bool IsDynamic() const override { return dynamic_; } |
| 251 | virtual bool IsStorage() const override { return storage_; } |
| 252 | VkBuffer GetBuffer() const { return buffer_; } |
| 253 | VkDeviceSize GetOffset() const { return offset_; } |
| 254 | VkDeviceSize GetRange() const { return range_; } |
| 255 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 256 | private: |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 257 | bool storage_; |
| 258 | bool dynamic_; |
| 259 | VkBuffer buffer_; |
| 260 | VkDeviceSize offset_; |
| 261 | VkDeviceSize range_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 262 | }; |
| Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame] | 263 | // Structs to contain common elements that need to be shared between Validate* and Perform* calls below |
| 264 | struct AllocateDescriptorSetsData { |
| 265 | uint32_t required_descriptors_by_type[VK_DESCRIPTOR_TYPE_RANGE_SIZE]; |
| Tobin Ehlis | a8e46e7 | 2017-06-21 10:16:10 -0600 | [diff] [blame] | 266 | std::vector<std::shared_ptr<DescriptorSetLayout const>> layout_nodes; |
| Tobin Ehlis | 68d0adf | 2016-06-01 11:33:50 -0600 | [diff] [blame] | 267 | AllocateDescriptorSetsData(uint32_t); |
| 268 | }; |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 269 | // Helper functions for descriptor set functions that cross multiple sets |
| 270 | // "Validate" will make sure an update is ok without actually performing it |
| Tobin Ehlis | 6a72dc7 | 2016-06-01 16:41:17 -0600 | [diff] [blame] | 271 | bool ValidateUpdateDescriptorSets(const debug_report_data *, const core_validation::layer_data *, uint32_t, |
| Tobin Ehlis | 56a3094 | 2016-05-19 08:00:00 -0600 | [diff] [blame] | 272 | const VkWriteDescriptorSet *, uint32_t, const VkCopyDescriptorSet *); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 273 | // "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] | 274 | void PerformUpdateDescriptorSets(const core_validation::layer_data *, uint32_t, const VkWriteDescriptorSet *, uint32_t, |
| 275 | const VkCopyDescriptorSet *); |
| Mark Lobodzinski | 3d63a04 | 2017-03-09 16:24:13 -0700 | [diff] [blame] | 276 | // Similar to PerformUpdateDescriptorSets, this function will do the same for updating via templates |
| 277 | void PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *, VkDescriptorSet, std::unique_ptr<TEMPLATE_STATE> const &, |
| 278 | const void *); |
| Tobin Ehlis | f320b19 | 2017-03-14 11:22:50 -0600 | [diff] [blame] | 279 | // Update the common AllocateDescriptorSetsData struct which can then be shared between Validate* and Perform* funcs below |
| 280 | void UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *, |
| 281 | AllocateDescriptorSetsData *); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 282 | // Validate that Allocation state is ok |
| Tobin Ehlis | f320b19 | 2017-03-14 11:22:50 -0600 | [diff] [blame] | 283 | bool ValidateAllocateDescriptorSets(const core_validation::layer_data *, const VkDescriptorSetAllocateInfo *, |
| 284 | const AllocateDescriptorSetsData *); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 285 | // Update state based on allocating new descriptorsets |
| Tobin Ehlis | 997b258 | 2016-06-02 08:43:37 -0600 | [diff] [blame] | 286 | void PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *, const VkDescriptorSet *, const AllocateDescriptorSetsData *, |
| Tobin Ehlis | bd711bd | 2016-10-12 14:27:30 -0600 | [diff] [blame] | 287 | std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *, |
| Tobin Ehlis | 997b258 | 2016-06-02 08:43:37 -0600 | [diff] [blame] | 288 | std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *, |
| Tobin Ehlis | 4e38059 | 2016-06-02 12:41:47 -0600 | [diff] [blame] | 289 | const core_validation::layer_data *); |
| Tobin Ehlis | ee47146 | 2016-05-26 11:21:59 -0600 | [diff] [blame] | 290 | |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 291 | /* |
| 292 | * DescriptorSet class |
| 293 | * |
| 294 | * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set). |
| 295 | * A set has an underlying layout which defines the bindings in the set and the |
| 296 | * types and numbers of descriptors in each descriptor slot. Most of the layout |
| 297 | * interfaces are exposed through identically-named functions in the set class. |
| 298 | * Please refer to the DescriptorSetLayout comment above for a description of |
| 299 | * index, binding, and global index. |
| 300 | * |
| 301 | * At construction a vector of Descriptor* is created with types corresponding to the |
| 302 | * layout. The primary operation performed on the descriptors is to update them |
| 303 | * via write or copy updates, and validate that the update contents are correct. |
| 304 | * In order to validate update contents, the DescriptorSet stores a bunch of ptrs |
| 305 | * to data maps where various Vulkan objects can be looked up. The management of |
| 306 | * those maps is performed externally. The set class relies on their contents to |
| 307 | * be correct at the time of update. |
| 308 | */ |
| Tobin Ehlis | 05be5df | 2016-05-05 08:25:02 -0600 | [diff] [blame] | 309 | class DescriptorSet : public BASE_NODE { |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 310 | public: |
| Tobin Ehlis | 452b453 | 2017-06-21 09:56:13 -0600 | [diff] [blame] | 311 | DescriptorSet(const VkDescriptorSet, const VkDescriptorPool, const std::shared_ptr<DescriptorSetLayout const> &, |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 312 | const core_validation::layer_data *); |
| Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 313 | ~DescriptorSet(); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 314 | // 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] | 315 | uint32_t GetTotalDescriptorCount() const { return p_layout_->GetTotalDescriptorCount(); }; |
| 316 | uint32_t GetDynamicDescriptorCount() const { return p_layout_->GetDynamicDescriptorCount(); }; |
| 317 | uint32_t GetBindingCount() const { return p_layout_->GetBindingCount(); }; |
| 318 | VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return p_layout_->GetTypeFromIndex(index); }; |
| 319 | VkDescriptorType GetTypeFromGlobalIndex(const uint32_t index) const { return p_layout_->GetTypeFromGlobalIndex(index); }; |
| 320 | VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return p_layout_->GetTypeFromBinding(binding); }; |
| 321 | 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] | 322 | uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const { |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 323 | return p_layout_->GetDescriptorCountFromBinding(binding); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 324 | }; |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 325 | // Return index into dynamic offset array for given binding |
| 326 | int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const { |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 327 | return p_layout_->GetDynamicOffsetIndexFromBinding(binding); |
| Tobin Ehlis | a3525e0 | 2016-11-17 10:50:52 -0700 | [diff] [blame] | 328 | } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 329 | // Return true if given binding is present in this set |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 330 | bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 331 | // Is this set compatible with the given layout? |
| Tobin Ehlis | 6dc57dd | 2017-06-21 10:08:52 -0600 | [diff] [blame] | 332 | bool IsCompatible(DescriptorSetLayout const *const, std::string *) const; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 333 | // For given bindings validate state at time of draw is correct, returning false on error and writing error details into string* |
| Tobin Ehlis | c826645 | 2017-04-07 12:20:30 -0600 | [diff] [blame] | 334 | bool ValidateDrawState(const std::map<uint32_t, descriptor_req> &, const std::vector<uint32_t> &, const GLOBAL_CB_NODE *, |
| 335 | const char *caller, std::string *) const; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 336 | // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number |
| 337 | // of objects inserted |
| Tobin Ehlis | cebc4c0 | 2016-08-22 10:10:43 -0600 | [diff] [blame] | 338 | 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] | 339 | std::unordered_set<VkImageView> *) const; |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 340 | |
| 341 | // Descriptor Update functions. These functions validate state and perform update separately |
| 342 | // Validate contents of a WriteUpdate |
| Tobin Ehlis | 75f04ec | 2016-10-06 17:43:11 -0600 | [diff] [blame] | 343 | bool ValidateWriteUpdate(const debug_report_data *, const VkWriteDescriptorSet *, UNIQUE_VALIDATION_ERROR_CODE *, |
| 344 | std::string *); |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 345 | // Perform a WriteUpdate whose contents were just validated using ValidateWriteUpdate |
| 346 | void PerformWriteUpdate(const VkWriteDescriptorSet *); |
| 347 | // Validate contents of a CopyUpdate |
| Tobin Ehlis | 75f04ec | 2016-10-06 17:43:11 -0600 | [diff] [blame] | 348 | bool ValidateCopyUpdate(const debug_report_data *, const VkCopyDescriptorSet *, const DescriptorSet *, |
| 349 | UNIQUE_VALIDATION_ERROR_CODE *, std::string *); |
| Tobin Ehlis | 300888c | 2016-05-18 13:43:26 -0600 | [diff] [blame] | 350 | // Perform a CopyUpdate whose contents were just validated using ValidateCopyUpdate |
| 351 | void PerformCopyUpdate(const VkCopyDescriptorSet *, const DescriptorSet *); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 352 | |
| Tobin Ehlis | 452b453 | 2017-06-21 09:56:13 -0600 | [diff] [blame] | 353 | std::shared_ptr<DescriptorSetLayout const> const GetLayout() const { return p_layout_; }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 354 | VkDescriptorSet GetSet() const { return set_; }; |
| 355 | // 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] | 356 | std::unordered_set<GLOBAL_CB_NODE *> GetBoundCmdBuffers() const { return cb_bindings; } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 357 | // Bind given cmd_buffer to this descriptor set |
| Tobin Ehlis | 022528b | 2016-12-29 12:22:32 -0700 | [diff] [blame] | 358 | void BindCommandBuffer(GLOBAL_CB_NODE *, const std::map<uint32_t, descriptor_req> &); |
| Tobin Ehlis | 2556f5b | 2016-06-24 17:22:16 -0600 | [diff] [blame] | 359 | // If given cmd_buffer is in the cb_bindings set, remove it |
| 360 | void RemoveBoundCommandBuffer(GLOBAL_CB_NODE *cb_node) { cb_bindings.erase(cb_node); } |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 361 | VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const { |
| Tobin Ehlis | 7cd8c79 | 2017-06-20 08:30:39 -0600 | [diff] [blame] | 362 | return p_layout_->GetImmutableSamplerPtrFromBinding(index); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 363 | }; |
| 364 | // For a particular binding, get the global index |
| John Zulauf | c483f44 | 2017-12-15 14:02:06 -0700 | [diff] [blame^] | 365 | const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t binding) const { |
| 366 | return p_layout_->GetGlobalIndexRangeFromBinding(binding); |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 367 | }; |
| 368 | // Return true if any part of set has ever been updated |
| 369 | bool IsUpdated() const { return some_update_; }; |
| Józef Kucia | f0c94d4 | 2017-10-25 22:15:22 +0200 | [diff] [blame] | 370 | bool IsPushDescriptor() const { return p_layout_->IsPushDescriptor(); }; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 371 | |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 372 | private: |
| Tobin Ehlis | 75f04ec | 2016-10-06 17:43:11 -0600 | [diff] [blame] | 373 | bool VerifyWriteUpdateContents(const VkWriteDescriptorSet *, const uint32_t, UNIQUE_VALIDATION_ERROR_CODE *, |
| 374 | std::string *) const; |
| Tobin Ehlis | cbcf234 | 2016-05-24 13:07:12 -0600 | [diff] [blame] | 375 | bool VerifyCopyUpdateContents(const VkCopyDescriptorSet *, const DescriptorSet *, VkDescriptorType, uint32_t, |
| Tobin Ehlis | 75f04ec | 2016-10-06 17:43:11 -0600 | [diff] [blame] | 376 | UNIQUE_VALIDATION_ERROR_CODE *, std::string *) const; |
| Tobin Ehlis | 4668dce | 2016-11-16 09:30:23 -0700 | [diff] [blame] | 377 | bool ValidateBufferUsage(BUFFER_STATE const *, VkDescriptorType, UNIQUE_VALIDATION_ERROR_CODE *, std::string *) const; |
| Tobin Ehlis | 75f04ec | 2016-10-06 17:43:11 -0600 | [diff] [blame] | 378 | bool ValidateBufferUpdate(VkDescriptorBufferInfo const *, VkDescriptorType, UNIQUE_VALIDATION_ERROR_CODE *, |
| 379 | std::string *) const; |
| Tobin Ehlis | 9906d9d | 2016-05-17 14:23:46 -0600 | [diff] [blame] | 380 | // Private helper to set all bound cmd buffers to INVALID state |
| 381 | void InvalidateBoundCmdBuffers(); |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 382 | bool some_update_; // has any part of the set ever been updated? |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 383 | VkDescriptorSet set_; |
| Tobin Ehlis | 7ca20be | 2016-10-12 15:09:16 -0600 | [diff] [blame] | 384 | DESCRIPTOR_POOL_STATE *pool_state_; |
| Tobin Ehlis | 452b453 | 2017-06-21 09:56:13 -0600 | [diff] [blame] | 385 | const std::shared_ptr<DescriptorSetLayout const> p_layout_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 386 | std::vector<std::unique_ptr<Descriptor>> descriptors_; |
| Tobin Ehlis | 3d15c4a | 2016-06-02 13:04:47 -0600 | [diff] [blame] | 387 | // Ptr to device data used for various data look-ups |
| Tobin Ehlis | 94bc5d2 | 2016-06-02 07:46:52 -0600 | [diff] [blame] | 388 | const core_validation::layer_data *device_data_; |
| Tobin Ehlis | c3b6c4c | 2017-02-02 17:26:40 -0700 | [diff] [blame] | 389 | const VkPhysicalDeviceLimits limits_; |
| Tobin Ehlis | 0a43bde | 2016-05-03 08:31:08 -0600 | [diff] [blame] | 390 | }; |
| Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 391 | } |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 392 | #endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_ |