blob: 396082b878ee1f97c08e84ea4a8359b629be3952 [file] [log] [blame]
Tobin Ehlis2d9deec2016-04-21 14:19:26 -06001/* 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 Lobodzinskia5efa732016-10-10 14:05:59 -060024#ifndef NOEXCEPT
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060025#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 Lobodzinskia5efa732016-10-10 14:05:59 -060044#endif
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060045
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060046#include "core_validation_error_enums.h"
Tobin Ehlisbf98b692016-10-06 12:58:06 -060047#include "vk_validation_error_messages.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060048#include "core_validation_types.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060049#include "vk_layer_logging.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050#include "vk_layer_utils.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060051#include "vk_safe_struct.h"
52#include "vulkan/vk_layer.h"
Mark Lobodzinski33826372017-04-13 11:10:11 -060053#include "vk_object_types.h"
Tobin Ehliscebc4c02016-08-22 10:10:43 -060054#include <map>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060055#include <memory>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060056#include <unordered_map>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060057#include <unordered_set>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060058#include <vector>
59
Tobin Ehlis58c884f2017-02-08 12:15:27 -070060using core_validation::layer_data;
61
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060062// Descriptor Data structures
63
64/*
65 * DescriptorSetLayout class
66 *
67 * Overview - This class encapsulates the Vulkan VkDescriptorSetLayout data (layout).
68 * A layout consists of some number of bindings, each of which has a binding#, a
69 * type, descriptor count, stage flags, and pImmutableSamplers.
70 *
71 * Index vs Binding - A layout is created with an array of VkDescriptorSetLayoutBinding
72 * where each array index will have a corresponding binding# that is defined in that struct.
Tobin Ehlis9637fb22016-12-12 15:59:34 -070073 * The binding#, then, is decoupled from VkDescriptorSetLayoutBinding index, which allows
74 * bindings to be defined out-of-order. This DescriptorSetLayout class, however, stores
75 * the bindings internally in-order. This is useful for operations which may "roll over"
76 * from a single binding to the next consecutive binding.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060077 *
Tobin Ehlis9637fb22016-12-12 15:59:34 -070078 * Note that although the bindings are stored in-order, there still may be "gaps" in the
79 * binding#. For example, if the binding creation order is 8, 7, 10, 3, 4, then the
80 * internal binding array will have five entries stored in binding order 3, 4, 7, 8, 10.
81 * To process all of the bindings in a layout you can iterate from 0 to GetBindingCount()
82 * and use the Get*FromIndex() functions for each index. To just process a single binding,
83 * use the Get*FromBinding() functions.
84 *
85 * Global Index - The binding vector index has as many indices as there are bindings.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060086 * This class also has the concept of a Global Index. For the global index functions,
87 * there are as many global indices as there are descriptors in the layout.
88 * For the global index, consider all of the bindings to be a flat array where
Tobin Ehlis9637fb22016-12-12 15:59:34 -070089 * descriptor 0 of of the lowest binding# is index 0 and each descriptor in the layout
90 * increments from there. So if the lowest binding# in this example had descriptorCount of
91 * 10, then the GlobalStartIndex of the 2nd lowest binding# will be 10 where 0-9 are the
92 * global indices for the lowest binding#.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060093 */
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060094namespace cvdescriptorset {
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060095class DescriptorSetLayout {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070096 public:
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060097 // Constructors and destructor
Tobin Ehlis154c2692016-10-25 09:36:53 -060098 DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info, const VkDescriptorSetLayout layout);
99 // Validate create info - should be called prior to creation
100 static bool ValidateCreateInfo(debug_report_data *, const VkDescriptorSetLayoutCreateInfo *);
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600101 // Straightforward Get functions
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600102 VkDescriptorSetLayout GetDescriptorSetLayout() const { return layout_; };
103 uint32_t GetTotalDescriptorCount() const { return descriptor_count_; };
104 uint32_t GetDynamicDescriptorCount() const { return dynamic_descriptor_count_; };
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700105 // For a given binding, return the number of descriptors in that binding and all successive bindings
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600106 uint32_t GetBindingCount() const { return binding_count_; };
107 // Fill passed-in set with bindings
108 void FillBindingSet(std::unordered_set<uint32_t> *) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600109 // Return true if given binding is present in this layout
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600110 bool HasBinding(const uint32_t binding) const { return binding_to_index_map_.count(binding) > 0; };
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600111 // Return true if this layout is compatible with passed in layout,
112 // else return false and update error_msg with description of incompatibility
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113 bool IsCompatible(const DescriptorSetLayout *, std::string *) const;
114 // Return true if binding 1 beyond given exists and has same type, stageFlags & immutable sampler use
115 bool IsNextBindingConsistent(const uint32_t) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600116 // Various Get functions that can either be passed a binding#, which will
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700117 // be automatically translated into the appropriate index, or the index# can be passed in directly
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600118 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t) const;
119 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t) const;
120 uint32_t GetDescriptorCountFromBinding(const uint32_t) const;
121 uint32_t GetDescriptorCountFromIndex(const uint32_t) const;
122 VkDescriptorType GetTypeFromBinding(const uint32_t) const;
123 VkDescriptorType GetTypeFromIndex(const uint32_t) const;
124 VkDescriptorType GetTypeFromGlobalIndex(const uint32_t) const;
125 VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t) const;
126 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t) const;
127 VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t) const;
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700128 // For a given binding and array index, return the corresponding index into the dynamic offset array
129 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
130 auto dyn_off = binding_to_dynamic_array_idx_map_.find(binding);
131 if (dyn_off == binding_to_dynamic_array_idx_map_.end()) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700132 assert(0); // Requesting dyn offset for invalid binding/array idx pair
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700133 return -1;
134 }
135 return dyn_off->second;
136 }
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600137 // For a particular binding, get the global index
Tobin Ehlis58c59582016-06-21 12:34:33 -0600138 // These calls should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600139 uint32_t GetGlobalStartIndexFromBinding(const uint32_t) const;
140 uint32_t GetGlobalEndIndexFromBinding(const uint32_t) const;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -0700141 // Helper function to get the next valid binding for a descriptor
142 uint32_t GetNextValidBinding(const uint32_t) const;
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600143 // For a particular binding starting at offset and having update_count descriptors
144 // updated, verify that for any binding boundaries crossed, the update is consistent
145 bool VerifyUpdateConsistency(uint32_t, uint32_t, uint32_t, const char *, const VkDescriptorSet, std::string *) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600146
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700147 private:
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600148 VkDescriptorSetLayout layout_;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700149 std::map<uint32_t, uint32_t> binding_to_index_map_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600150 std::unordered_map<uint32_t, uint32_t> binding_to_global_start_index_map_;
151 std::unordered_map<uint32_t, uint32_t> binding_to_global_end_index_map_;
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700152 // For a given binding map to associated index in the dynamic offset array
153 std::unordered_map<uint32_t, uint32_t> binding_to_dynamic_array_idx_map_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600154 // VkDescriptorSetLayoutCreateFlags flags_;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700155 uint32_t binding_count_; // # of bindings in this layout
Tobin Ehlis664e6012016-05-05 11:04:44 -0600156 std::vector<safe_VkDescriptorSetLayoutBinding> bindings_;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700157 uint32_t descriptor_count_; // total # descriptors in this layout
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600158 uint32_t dynamic_descriptor_count_;
159};
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600160
161/*
162 * Descriptor classes
163 * Descriptor is an abstract base class from which 5 separate descriptor types are derived.
164 * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per
165 * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*.
166 */
167
168// Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass"
Mark Lobodzinski0978f5f2016-05-19 17:23:38 -0600169enum DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600170
171class Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700172 public:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600173 virtual ~Descriptor(){};
Tobin Ehlis300888c2016-05-18 13:43:26 -0600174 virtual void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) = 0;
175 virtual void CopyUpdate(const Descriptor *) = 0;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600176 // Create binding between resources of this descriptor and given cb_node
177 virtual void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600178 virtual DescriptorClass GetClass() const { return descriptor_class; };
179 // Special fast-path check for SamplerDescriptors that are immutable
180 virtual bool IsImmutableSampler() const { return false; };
181 // Check for dynamic descriptor type
182 virtual bool IsDynamic() const { return false; };
183 // Check for storage descriptor type
184 virtual bool IsStorage() const { return false; };
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700185 bool updated; // Has descriptor been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600186 DescriptorClass descriptor_class;
187};
188// Shared helper functions - These are useful because the shared sampler image descriptor type
189// performs common functions with both sampler and image descriptors so they can share their common functions
Tobin Ehlise2f80292016-06-02 10:08:53 -0600190bool ValidateSampler(const VkSampler, const core_validation::layer_data *);
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600191bool ValidateImageUpdate(VkImageView, VkImageLayout, VkDescriptorType, const core_validation::layer_data *,
192 UNIQUE_VALIDATION_ERROR_CODE *, std::string *);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600193
194class SamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700195 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600196 SamplerDescriptor(const VkSampler *);
197 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
198 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600199 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600200 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600201 VkSampler GetSampler() const { return sampler_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600202
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700203 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600204 // bool ValidateSampler(const VkSampler) const;
205 VkSampler sampler_;
206 bool immutable_;
Tobin Ehlis546326f2016-04-26 11:06:05 -0600207};
208
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600209class ImageSamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700210 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600211 ImageSamplerDescriptor(const VkSampler *);
212 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
213 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600214 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlisc9625152016-05-24 16:47:36 -0600215 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600216 VkSampler GetSampler() const { return sampler_; }
217 VkImageView GetImageView() const { return image_view_; }
218 VkImageLayout GetImageLayout() const { return image_layout_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600219
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700220 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600221 VkSampler sampler_;
222 bool immutable_;
223 VkImageView image_view_;
224 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600225};
226
227class ImageDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700228 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600229 ImageDescriptor(const VkDescriptorType);
230 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
231 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600232 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Norbert Nopper419a1092016-05-15 19:19:41 +0200233 virtual bool IsStorage() const override { return storage_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600234 VkImageView GetImageView() const { return image_view_; }
235 VkImageLayout GetImageLayout() const { return image_layout_; }
236
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700237 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600238 bool storage_;
239 VkImageView image_view_;
240 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600241};
242
243class TexelDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700244 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600245 TexelDescriptor(const VkDescriptorType);
246 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
247 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600248 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlisf490f2e2016-05-17 06:43:48 -0600249 virtual bool IsStorage() const override { return storage_; }
250 VkBufferView GetBufferView() const { return buffer_view_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600251
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700252 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600253 VkBufferView buffer_view_;
254 bool storage_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600255};
256
257class BufferDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700258 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600259 BufferDescriptor(const VkDescriptorType);
260 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
261 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600262 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600263 virtual bool IsDynamic() const override { return dynamic_; }
264 virtual bool IsStorage() const override { return storage_; }
265 VkBuffer GetBuffer() const { return buffer_; }
266 VkDeviceSize GetOffset() const { return offset_; }
267 VkDeviceSize GetRange() const { return range_; }
268
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700269 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600270 bool storage_;
271 bool dynamic_;
272 VkBuffer buffer_;
273 VkDeviceSize offset_;
274 VkDeviceSize range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600275};
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600276// Structs to contain common elements that need to be shared between Validate* and Perform* calls below
277struct AllocateDescriptorSetsData {
278 uint32_t required_descriptors_by_type[VK_DESCRIPTOR_TYPE_RANGE_SIZE];
279 std::vector<cvdescriptorset::DescriptorSetLayout const *> layout_nodes;
280 AllocateDescriptorSetsData(uint32_t);
281};
Tobin Ehlisee471462016-05-26 11:21:59 -0600282// Helper functions for descriptor set functions that cross multiple sets
283// "Validate" will make sure an update is ok without actually performing it
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600284bool ValidateUpdateDescriptorSets(const debug_report_data *, const core_validation::layer_data *, uint32_t,
Tobin Ehlis56a30942016-05-19 08:00:00 -0600285 const VkWriteDescriptorSet *, uint32_t, const VkCopyDescriptorSet *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600286// "Perform" does the update with the assumption that ValidateUpdateDescriptorSets() has passed for the given update
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600287void PerformUpdateDescriptorSets(const core_validation::layer_data *, uint32_t, const VkWriteDescriptorSet *, uint32_t,
288 const VkCopyDescriptorSet *);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -0700289// Similar to PerformUpdateDescriptorSets, this function will do the same for updating via templates
290void PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *, VkDescriptorSet, std::unique_ptr<TEMPLATE_STATE> const &,
291 const void *);
Tobin Ehlisf320b192017-03-14 11:22:50 -0600292// Update the common AllocateDescriptorSetsData struct which can then be shared between Validate* and Perform* funcs below
293void UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *,
294 AllocateDescriptorSetsData *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600295// Validate that Allocation state is ok
Tobin Ehlisf320b192017-03-14 11:22:50 -0600296bool ValidateAllocateDescriptorSets(const core_validation::layer_data *, const VkDescriptorSetAllocateInfo *,
297 const AllocateDescriptorSetsData *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600298// Update state based on allocating new descriptorsets
Tobin Ehlis997b2582016-06-02 08:43:37 -0600299void PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *, const VkDescriptorSet *, const AllocateDescriptorSetsData *,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -0600300 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *,
Tobin Ehlis997b2582016-06-02 08:43:37 -0600301 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *,
Tobin Ehlis4e380592016-06-02 12:41:47 -0600302 const core_validation::layer_data *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600303
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600304/*
305 * DescriptorSet class
306 *
307 * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set).
308 * A set has an underlying layout which defines the bindings in the set and the
309 * types and numbers of descriptors in each descriptor slot. Most of the layout
310 * interfaces are exposed through identically-named functions in the set class.
311 * Please refer to the DescriptorSetLayout comment above for a description of
312 * index, binding, and global index.
313 *
314 * At construction a vector of Descriptor* is created with types corresponding to the
315 * layout. The primary operation performed on the descriptors is to update them
316 * via write or copy updates, and validate that the update contents are correct.
317 * In order to validate update contents, the DescriptorSet stores a bunch of ptrs
318 * to data maps where various Vulkan objects can be looked up. The management of
319 * those maps is performed externally. The set class relies on their contents to
320 * be correct at the time of update.
321 */
Tobin Ehlis05be5df2016-05-05 08:25:02 -0600322class DescriptorSet : public BASE_NODE {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700323 public:
Tobin Ehlis93f22372016-10-12 14:34:12 -0600324 DescriptorSet(const VkDescriptorSet, const VkDescriptorPool, const DescriptorSetLayout *, const core_validation::layer_data *);
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600325 ~DescriptorSet();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600326 // A number of common Get* functions that return data based on layout from which this set was created
327 uint32_t GetTotalDescriptorCount() const { return p_layout_ ? p_layout_->GetTotalDescriptorCount() : 0; };
328 uint32_t GetDynamicDescriptorCount() const { return p_layout_ ? p_layout_->GetDynamicDescriptorCount() : 0; };
329 uint32_t GetBindingCount() const { return p_layout_ ? p_layout_->GetBindingCount() : 0; };
330 VkDescriptorType GetTypeFromIndex(const uint32_t index) const {
331 return p_layout_ ? p_layout_->GetTypeFromIndex(index) : VK_DESCRIPTOR_TYPE_MAX_ENUM;
332 };
333 VkDescriptorType GetTypeFromGlobalIndex(const uint32_t index) const {
334 return p_layout_ ? p_layout_->GetTypeFromGlobalIndex(index) : VK_DESCRIPTOR_TYPE_MAX_ENUM;
335 };
336 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const {
337 return p_layout_ ? p_layout_->GetTypeFromBinding(binding) : VK_DESCRIPTOR_TYPE_MAX_ENUM;
338 };
339 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const {
340 return p_layout_ ? p_layout_->GetDescriptorCountFromIndex(index) : 0;
341 };
342 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
343 return p_layout_ ? p_layout_->GetDescriptorCountFromBinding(binding) : 0;
344 };
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700345 // Return index into dynamic offset array for given binding
346 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
347 return p_layout_ ? p_layout_->GetDynamicOffsetIndexFromBinding(binding) : -1;
348 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600349 // Return true if given binding is present in this set
350 bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); };
351 // Is this set compatible with the given layout?
352 bool IsCompatible(const DescriptorSetLayout *, std::string *) const;
353 // For given bindings validate state at time of draw is correct, returning false on error and writing error details into string*
Tobin Ehlisc8266452017-04-07 12:20:30 -0600354 bool ValidateDrawState(const std::map<uint32_t, descriptor_req> &, const std::vector<uint32_t> &, const GLOBAL_CB_NODE *,
355 const char *caller, std::string *) const;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600356 // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number
357 // of objects inserted
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600358 uint32_t GetStorageUpdates(const std::map<uint32_t, descriptor_req> &, std::unordered_set<VkBuffer> *,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600359 std::unordered_set<VkImageView> *) const;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600360
361 // Descriptor Update functions. These functions validate state and perform update separately
362 // Validate contents of a WriteUpdate
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600363 bool ValidateWriteUpdate(const debug_report_data *, const VkWriteDescriptorSet *, UNIQUE_VALIDATION_ERROR_CODE *,
364 std::string *);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600365 // Perform a WriteUpdate whose contents were just validated using ValidateWriteUpdate
366 void PerformWriteUpdate(const VkWriteDescriptorSet *);
367 // Validate contents of a CopyUpdate
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600368 bool ValidateCopyUpdate(const debug_report_data *, const VkCopyDescriptorSet *, const DescriptorSet *,
369 UNIQUE_VALIDATION_ERROR_CODE *, std::string *);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600370 // Perform a CopyUpdate whose contents were just validated using ValidateCopyUpdate
371 void PerformCopyUpdate(const VkCopyDescriptorSet *, const DescriptorSet *);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600372
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600373 const DescriptorSetLayout *GetLayout() const { return p_layout_; };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600374 VkDescriptorSet GetSet() const { return set_; };
375 // Return unordered_set of all command buffers that this set is bound to
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600376 std::unordered_set<GLOBAL_CB_NODE *> GetBoundCmdBuffers() const { return cb_bindings; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600377 // Bind given cmd_buffer to this descriptor set
Tobin Ehlis022528b2016-12-29 12:22:32 -0700378 void BindCommandBuffer(GLOBAL_CB_NODE *, const std::map<uint32_t, descriptor_req> &);
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600379 // If given cmd_buffer is in the cb_bindings set, remove it
380 void RemoveBoundCommandBuffer(GLOBAL_CB_NODE *cb_node) { cb_bindings.erase(cb_node); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600381 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const {
382 return p_layout_->GetImmutableSamplerPtrFromBinding(index);
383 };
384 // For a particular binding, get the global index
385 uint32_t GetGlobalStartIndexFromBinding(const uint32_t binding) const {
386 return p_layout_->GetGlobalStartIndexFromBinding(binding);
387 };
388 uint32_t GetGlobalEndIndexFromBinding(const uint32_t binding) const {
389 return p_layout_->GetGlobalEndIndexFromBinding(binding);
390 };
391 // Return true if any part of set has ever been updated
392 bool IsUpdated() const { return some_update_; };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600393
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700394 private:
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600395 bool VerifyWriteUpdateContents(const VkWriteDescriptorSet *, const uint32_t, UNIQUE_VALIDATION_ERROR_CODE *,
396 std::string *) const;
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600397 bool VerifyCopyUpdateContents(const VkCopyDescriptorSet *, const DescriptorSet *, VkDescriptorType, uint32_t,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600398 UNIQUE_VALIDATION_ERROR_CODE *, std::string *) const;
Tobin Ehlis4668dce2016-11-16 09:30:23 -0700399 bool ValidateBufferUsage(BUFFER_STATE const *, VkDescriptorType, UNIQUE_VALIDATION_ERROR_CODE *, std::string *) const;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600400 bool ValidateBufferUpdate(VkDescriptorBufferInfo const *, VkDescriptorType, UNIQUE_VALIDATION_ERROR_CODE *,
401 std::string *) const;
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600402 // Private helper to set all bound cmd buffers to INVALID state
403 void InvalidateBoundCmdBuffers();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700404 bool some_update_; // has any part of the set ever been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600405 VkDescriptorSet set_;
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600406 DESCRIPTOR_POOL_STATE *pool_state_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600407 const DescriptorSetLayout *p_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600408 std::vector<std::unique_ptr<Descriptor>> descriptors_;
Tobin Ehlis3d15c4a2016-06-02 13:04:47 -0600409 // Ptr to device data used for various data look-ups
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600410 const core_validation::layer_data *device_data_;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700411 const VkPhysicalDeviceLimits limits_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600412};
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600413}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700414#endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_