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