blob: 1ac2d0aa185d851d20d42a6ef73d2a7f32a5d19b [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>
John Zulaufc483f442017-12-15 14:02:06 -070019 * John Zulauf <jzulauf@lunarg.com>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060020 */
21#ifndef CORE_VALIDATION_DESCRIPTOR_SETS_H_
22#define CORE_VALIDATION_DESCRIPTOR_SETS_H_
23
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060024#include "core_validation_error_enums.h"
Tobin Ehlisbf98b692016-10-06 12:58:06 -060025#include "vk_validation_error_messages.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060026#include "core_validation_types.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060027#include "vk_layer_logging.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060028#include "vk_layer_utils.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060029#include "vk_safe_struct.h"
30#include "vulkan/vk_layer.h"
Mark Lobodzinski33826372017-04-13 11:10:11 -060031#include "vk_object_types.h"
Tobin Ehliscebc4c02016-08-22 10:10:43 -060032#include <map>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060033#include <memory>
John Zulaufb6d71202017-12-22 16:47:09 -070034#include <set>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060035#include <unordered_map>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060036#include <unordered_set>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060037#include <vector>
38
Tobin Ehlis58c884f2017-02-08 12:15:27 -070039using core_validation::layer_data;
40
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060041// Descriptor Data structures
John Zulaufc483f442017-12-15 14:02:06 -070042namespace cvdescriptorset {
43
44// Index range for global indices below, end is exclusive, i.e. [start,end)
45struct IndexRange {
46 IndexRange(uint32_t start_in, uint32_t end_in) : start(start_in), end(end_in) {}
47 IndexRange() = default;
48 uint32_t start;
49 uint32_t end;
50};
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060051
52/*
53 * DescriptorSetLayout class
54 *
55 * Overview - This class encapsulates the Vulkan VkDescriptorSetLayout data (layout).
56 * A layout consists of some number of bindings, each of which has a binding#, a
57 * type, descriptor count, stage flags, and pImmutableSamplers.
58 *
59 * Index vs Binding - A layout is created with an array of VkDescriptorSetLayoutBinding
60 * where each array index will have a corresponding binding# that is defined in that struct.
Tobin Ehlis9637fb22016-12-12 15:59:34 -070061 * The binding#, then, is decoupled from VkDescriptorSetLayoutBinding index, which allows
62 * bindings to be defined out-of-order. This DescriptorSetLayout class, however, stores
63 * the bindings internally in-order. This is useful for operations which may "roll over"
64 * from a single binding to the next consecutive binding.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060065 *
Tobin Ehlis9637fb22016-12-12 15:59:34 -070066 * Note that although the bindings are stored in-order, there still may be "gaps" in the
67 * binding#. For example, if the binding creation order is 8, 7, 10, 3, 4, then the
68 * internal binding array will have five entries stored in binding order 3, 4, 7, 8, 10.
69 * To process all of the bindings in a layout you can iterate from 0 to GetBindingCount()
70 * and use the Get*FromIndex() functions for each index. To just process a single binding,
71 * use the Get*FromBinding() functions.
72 *
73 * Global Index - The binding vector index has as many indices as there are bindings.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060074 * This class also has the concept of a Global Index. For the global index functions,
75 * there are as many global indices as there are descriptors in the layout.
76 * For the global index, consider all of the bindings to be a flat array where
Tobin Ehlis9637fb22016-12-12 15:59:34 -070077 * descriptor 0 of of the lowest binding# is index 0 and each descriptor in the layout
78 * increments from there. So if the lowest binding# in this example had descriptorCount of
79 * 10, then the GlobalStartIndex of the 2nd lowest binding# will be 10 where 0-9 are the
80 * global indices for the lowest binding#.
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060081 */
82class DescriptorSetLayout {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070083 public:
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060084 // Constructors and destructor
Tobin Ehlis154c2692016-10-25 09:36:53 -060085 DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info, const VkDescriptorSetLayout layout);
86 // Validate create info - should be called prior to creation
87 static bool ValidateCreateInfo(debug_report_data *, const VkDescriptorSetLayoutCreateInfo *);
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060088 // Straightforward Get functions
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060089 VkDescriptorSetLayout GetDescriptorSetLayout() const { return layout_; };
90 uint32_t GetTotalDescriptorCount() const { return descriptor_count_; };
91 uint32_t GetDynamicDescriptorCount() const { return dynamic_descriptor_count_; };
Józef Kucia1bb00972017-09-10 11:24:08 +020092 VkDescriptorSetLayoutCreateFlags GetCreateFlags() const { return flags_; }
Tobin Ehlisf922ef82016-11-30 10:19:14 -070093 // For a given binding, return the number of descriptors in that binding and all successive bindings
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060094 uint32_t GetBindingCount() const { return binding_count_; };
John Zulaufb6d71202017-12-22 16:47:09 -070095 // Non-empty binding numbers in order
96 const std::set<uint32_t> &GetSortedBindingSet() const { return non_empty_bindings_; }
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060097 // Return true if given binding is present in this layout
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060098 bool HasBinding(const uint32_t binding) const { return binding_to_index_map_.count(binding) > 0; };
Tobin Ehlisb1861d92017-07-06 16:50:10 -060099 // Return true if this layout is compatible with passed in layout from a pipelineLayout,
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600100 // else return false and update error_msg with description of incompatibility
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600101 bool IsCompatible(DescriptorSetLayout const *const, std::string *) const;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600102 // Return true if binding 1 beyond given exists and has same type, stageFlags & immutable sampler use
103 bool IsNextBindingConsistent(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700104 uint32_t GetIndexFromBinding(uint32_t binding) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600105 // Various Get functions that can either be passed a binding#, which will
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700106 // be automatically translated into the appropriate index, or the index# can be passed in directly
John Zulaufb6d71202017-12-22 16:47:09 -0700107 uint32_t GetMaxBinding() const { return bindings_[bindings_.size() - 1].binding; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600108 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700109 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(uint32_t binding) const {
110 return GetDescriptorSetLayoutBindingPtrFromIndex(GetIndexFromBinding(binding));
111 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600112 uint32_t GetDescriptorCountFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700113 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
114 return GetDescriptorCountFromIndex(GetIndexFromBinding(binding));
115 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600116 VkDescriptorType GetTypeFromIndex(const uint32_t) const;
John Zulaufb6d71202017-12-22 16:47:09 -0700117 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return GetTypeFromIndex(GetIndexFromBinding(binding)); }
118 VkShaderStageFlags GetStageFlagsFromIndex(const uint32_t) const;
119 VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t binding) const {
120 return GetStageFlagsFromIndex(GetIndexFromBinding(binding));
121 }
122 uint32_t GetIndexFromGlobalIndex(const uint32_t global_index) const;
123 VkDescriptorType GetTypeFromGlobalIndex(const uint32_t global_index) const {
124 return GetTypeFromIndex(GetIndexFromGlobalIndex(global_index));
125 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600126 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 }
John Zulaufc483f442017-12-15 14:02:06 -0700137 // For a particular binding, get the global index range
138 // This call should be guarded by a call to "HasBinding(binding)" to verify that the given binding exists
139 const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t) const;
140
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;
Józef Kuciaf0c94d42017-10-25 22:15:22 +0200146 bool IsPushDescriptor() const { return GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR; };
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600147
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700148 private:
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600149 VkDescriptorSetLayout layout_;
John Zulaufb6d71202017-12-22 16:47:09 -0700150 std::set<uint32_t> non_empty_bindings_; // Containing non-emtpy bindings in numerical order
151 std::unordered_map<uint32_t, uint32_t> binding_to_index_map_;
152 // The following map allows an non-iterative lookup of a binding from a global index...
153 std::map<uint32_t, uint32_t> global_start_to_index_map_; // The index corresponding for a starting global (descriptor) index
John Zulaufc483f442017-12-15 14:02:06 -0700154 std::unordered_map<uint32_t, IndexRange> binding_to_global_index_range_map_; // range is exclusive of .end
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700155 // For a given binding map to associated index in the dynamic offset array
156 std::unordered_map<uint32_t, uint32_t> binding_to_dynamic_array_idx_map_;
Józef Kucia1bb00972017-09-10 11:24:08 +0200157 VkDescriptorSetLayoutCreateFlags flags_;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700158 uint32_t binding_count_; // # of bindings in this layout
Tobin Ehlis664e6012016-05-05 11:04:44 -0600159 std::vector<safe_VkDescriptorSetLayoutBinding> bindings_;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700160 uint32_t descriptor_count_; // total # descriptors in this layout
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600161 uint32_t dynamic_descriptor_count_;
162};
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600163
164/*
165 * Descriptor classes
166 * Descriptor is an abstract base class from which 5 separate descriptor types are derived.
167 * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per
168 * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*.
169 */
170
171// Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass"
Mark Lobodzinski0978f5f2016-05-19 17:23:38 -0600172enum DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600173
174class Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700175 public:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600176 virtual ~Descriptor(){};
Tobin Ehlis300888c2016-05-18 13:43:26 -0600177 virtual void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) = 0;
178 virtual void CopyUpdate(const Descriptor *) = 0;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600179 // Create binding between resources of this descriptor and given cb_node
180 virtual void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600181 virtual DescriptorClass GetClass() const { return descriptor_class; };
182 // Special fast-path check for SamplerDescriptors that are immutable
183 virtual bool IsImmutableSampler() const { return false; };
184 // Check for dynamic descriptor type
185 virtual bool IsDynamic() const { return false; };
186 // Check for storage descriptor type
187 virtual bool IsStorage() const { return false; };
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700188 bool updated; // Has descriptor been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600189 DescriptorClass descriptor_class;
190};
191// Shared helper functions - These are useful because the shared sampler image descriptor type
192// performs common functions with both sampler and image descriptors so they can share their common functions
Tobin Ehlise2f80292016-06-02 10:08:53 -0600193bool ValidateSampler(const VkSampler, const core_validation::layer_data *);
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600194bool ValidateImageUpdate(VkImageView, VkImageLayout, VkDescriptorType, const core_validation::layer_data *,
195 UNIQUE_VALIDATION_ERROR_CODE *, std::string *);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600196
197class SamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700198 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600199 SamplerDescriptor(const VkSampler *);
200 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
201 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600202 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600203 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600204 VkSampler GetSampler() const { return sampler_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600205
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700206 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600207 // bool ValidateSampler(const VkSampler) const;
208 VkSampler sampler_;
209 bool immutable_;
Tobin Ehlis546326f2016-04-26 11:06:05 -0600210};
211
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600212class ImageSamplerDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700213 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600214 ImageSamplerDescriptor(const VkSampler *);
215 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
216 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600217 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlisc9625152016-05-24 16:47:36 -0600218 virtual bool IsImmutableSampler() const override { return immutable_; };
Tobin Ehlis300888c2016-05-18 13:43:26 -0600219 VkSampler GetSampler() const { return sampler_; }
220 VkImageView GetImageView() const { return image_view_; }
221 VkImageLayout GetImageLayout() const { return image_layout_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600222
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700223 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600224 VkSampler sampler_;
225 bool immutable_;
226 VkImageView image_view_;
227 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600228};
229
230class ImageDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700231 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600232 ImageDescriptor(const VkDescriptorType);
233 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
234 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600235 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Norbert Nopper419a1092016-05-15 19:19:41 +0200236 virtual bool IsStorage() const override { return storage_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600237 VkImageView GetImageView() const { return image_view_; }
238 VkImageLayout GetImageLayout() const { return image_layout_; }
239
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700240 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600241 bool storage_;
242 VkImageView image_view_;
243 VkImageLayout image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600244};
245
246class TexelDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700247 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600248 TexelDescriptor(const VkDescriptorType);
249 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
250 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600251 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlisf490f2e2016-05-17 06:43:48 -0600252 virtual bool IsStorage() const override { return storage_; }
253 VkBufferView GetBufferView() const { return buffer_view_; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600254
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700255 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600256 VkBufferView buffer_view_;
257 bool storage_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600258};
259
260class BufferDescriptor : public Descriptor {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700261 public:
Tobin Ehlis300888c2016-05-18 13:43:26 -0600262 BufferDescriptor(const VkDescriptorType);
263 void WriteUpdate(const VkWriteDescriptorSet *, const uint32_t) override;
264 void CopyUpdate(const Descriptor *) override;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600265 void BindCommandBuffer(const core_validation::layer_data *, GLOBAL_CB_NODE *) override;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600266 virtual bool IsDynamic() const override { return dynamic_; }
267 virtual bool IsStorage() const override { return storage_; }
268 VkBuffer GetBuffer() const { return buffer_; }
269 VkDeviceSize GetOffset() const { return offset_; }
270 VkDeviceSize GetRange() const { return range_; }
271
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700272 private:
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600273 bool storage_;
274 bool dynamic_;
275 VkBuffer buffer_;
276 VkDeviceSize offset_;
277 VkDeviceSize range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600278};
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600279// Structs to contain common elements that need to be shared between Validate* and Perform* calls below
280struct AllocateDescriptorSetsData {
281 uint32_t required_descriptors_by_type[VK_DESCRIPTOR_TYPE_RANGE_SIZE];
Tobin Ehlisa8e46e72017-06-21 10:16:10 -0600282 std::vector<std::shared_ptr<DescriptorSetLayout const>> layout_nodes;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600283 AllocateDescriptorSetsData(uint32_t);
284};
Tobin Ehlisee471462016-05-26 11:21:59 -0600285// Helper functions for descriptor set functions that cross multiple sets
286// "Validate" will make sure an update is ok without actually performing it
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600287bool ValidateUpdateDescriptorSets(const debug_report_data *, const core_validation::layer_data *, uint32_t,
Tobin Ehlis56a30942016-05-19 08:00:00 -0600288 const VkWriteDescriptorSet *, uint32_t, const VkCopyDescriptorSet *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600289// "Perform" does the update with the assumption that ValidateUpdateDescriptorSets() has passed for the given update
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600290void PerformUpdateDescriptorSets(const core_validation::layer_data *, uint32_t, const VkWriteDescriptorSet *, uint32_t,
291 const VkCopyDescriptorSet *);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -0700292// Similar to PerformUpdateDescriptorSets, this function will do the same for updating via templates
293void PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *, VkDescriptorSet, std::unique_ptr<TEMPLATE_STATE> const &,
294 const void *);
Tobin Ehlisf320b192017-03-14 11:22:50 -0600295// Update the common AllocateDescriptorSetsData struct which can then be shared between Validate* and Perform* funcs below
296void UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *,
297 AllocateDescriptorSetsData *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600298// Validate that Allocation state is ok
Tobin Ehlisf320b192017-03-14 11:22:50 -0600299bool ValidateAllocateDescriptorSets(const core_validation::layer_data *, const VkDescriptorSetAllocateInfo *,
300 const AllocateDescriptorSetsData *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600301// Update state based on allocating new descriptorsets
Tobin Ehlis997b2582016-06-02 08:43:37 -0600302void PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *, const VkDescriptorSet *, const AllocateDescriptorSetsData *,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -0600303 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *,
Tobin Ehlis997b2582016-06-02 08:43:37 -0600304 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *,
Tobin Ehlis4e380592016-06-02 12:41:47 -0600305 const core_validation::layer_data *);
Tobin Ehlisee471462016-05-26 11:21:59 -0600306
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600307/*
308 * DescriptorSet class
309 *
310 * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set).
311 * A set has an underlying layout which defines the bindings in the set and the
312 * types and numbers of descriptors in each descriptor slot. Most of the layout
313 * interfaces are exposed through identically-named functions in the set class.
314 * Please refer to the DescriptorSetLayout comment above for a description of
315 * index, binding, and global index.
316 *
317 * At construction a vector of Descriptor* is created with types corresponding to the
318 * layout. The primary operation performed on the descriptors is to update them
319 * via write or copy updates, and validate that the update contents are correct.
320 * In order to validate update contents, the DescriptorSet stores a bunch of ptrs
321 * to data maps where various Vulkan objects can be looked up. The management of
322 * those maps is performed externally. The set class relies on their contents to
323 * be correct at the time of update.
324 */
Tobin Ehlis05be5df2016-05-05 08:25:02 -0600325class DescriptorSet : public BASE_NODE {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700326 public:
Tobin Ehlis452b4532017-06-21 09:56:13 -0600327 DescriptorSet(const VkDescriptorSet, const VkDescriptorPool, const std::shared_ptr<DescriptorSetLayout const> &,
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600328 const core_validation::layer_data *);
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600329 ~DescriptorSet();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600330 // A number of common Get* functions that return data based on layout from which this set was created
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600331 uint32_t GetTotalDescriptorCount() const { return p_layout_->GetTotalDescriptorCount(); };
332 uint32_t GetDynamicDescriptorCount() const { return p_layout_->GetDynamicDescriptorCount(); };
333 uint32_t GetBindingCount() const { return p_layout_->GetBindingCount(); };
334 VkDescriptorType GetTypeFromIndex(const uint32_t index) const { return p_layout_->GetTypeFromIndex(index); };
335 VkDescriptorType GetTypeFromGlobalIndex(const uint32_t index) const { return p_layout_->GetTypeFromGlobalIndex(index); };
336 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const { return p_layout_->GetTypeFromBinding(binding); };
337 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const { return p_layout_->GetDescriptorCountFromIndex(index); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600338 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600339 return p_layout_->GetDescriptorCountFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600340 };
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700341 // Return index into dynamic offset array for given binding
342 int32_t GetDynamicOffsetIndexFromBinding(uint32_t binding) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600343 return p_layout_->GetDynamicOffsetIndexFromBinding(binding);
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700344 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600345 // Return true if given binding is present in this set
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600346 bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600347 // Is this set compatible with the given layout?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600348 bool IsCompatible(DescriptorSetLayout const *const, std::string *) const;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600349 // 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 -0600350 bool ValidateDrawState(const std::map<uint32_t, descriptor_req> &, const std::vector<uint32_t> &, const GLOBAL_CB_NODE *,
351 const char *caller, std::string *) const;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600352 // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number
353 // of objects inserted
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600354 uint32_t GetStorageUpdates(const std::map<uint32_t, descriptor_req> &, std::unordered_set<VkBuffer> *,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600355 std::unordered_set<VkImageView> *) const;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600356
357 // Descriptor Update functions. These functions validate state and perform update separately
358 // Validate contents of a WriteUpdate
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600359 bool ValidateWriteUpdate(const debug_report_data *, const VkWriteDescriptorSet *, UNIQUE_VALIDATION_ERROR_CODE *,
360 std::string *);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600361 // Perform a WriteUpdate whose contents were just validated using ValidateWriteUpdate
362 void PerformWriteUpdate(const VkWriteDescriptorSet *);
363 // Validate contents of a CopyUpdate
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600364 bool ValidateCopyUpdate(const debug_report_data *, const VkCopyDescriptorSet *, const DescriptorSet *,
365 UNIQUE_VALIDATION_ERROR_CODE *, std::string *);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600366 // Perform a CopyUpdate whose contents were just validated using ValidateCopyUpdate
367 void PerformCopyUpdate(const VkCopyDescriptorSet *, const DescriptorSet *);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600368
Tobin Ehlis452b4532017-06-21 09:56:13 -0600369 std::shared_ptr<DescriptorSetLayout const> const GetLayout() const { return p_layout_; };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600370 VkDescriptorSet GetSet() const { return set_; };
371 // Return unordered_set of all command buffers that this set is bound to
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600372 std::unordered_set<GLOBAL_CB_NODE *> GetBoundCmdBuffers() const { return cb_bindings; }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600373 // Bind given cmd_buffer to this descriptor set
Tobin Ehlis022528b2016-12-29 12:22:32 -0700374 void BindCommandBuffer(GLOBAL_CB_NODE *, const std::map<uint32_t, descriptor_req> &);
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600375 // If given cmd_buffer is in the cb_bindings set, remove it
376 void RemoveBoundCommandBuffer(GLOBAL_CB_NODE *cb_node) { cb_bindings.erase(cb_node); }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600377 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600378 return p_layout_->GetImmutableSamplerPtrFromBinding(index);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600379 };
380 // For a particular binding, get the global index
John Zulaufc483f442017-12-15 14:02:06 -0700381 const IndexRange &GetGlobalIndexRangeFromBinding(const uint32_t binding) const {
382 return p_layout_->GetGlobalIndexRangeFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600383 };
384 // Return true if any part of set has ever been updated
385 bool IsUpdated() const { return some_update_; };
Józef Kuciaf0c94d42017-10-25 22:15:22 +0200386 bool IsPushDescriptor() const { return p_layout_->IsPushDescriptor(); };
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600387
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700388 private:
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600389 bool VerifyWriteUpdateContents(const VkWriteDescriptorSet *, const uint32_t, UNIQUE_VALIDATION_ERROR_CODE *,
390 std::string *) const;
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600391 bool VerifyCopyUpdateContents(const VkCopyDescriptorSet *, const DescriptorSet *, VkDescriptorType, uint32_t,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600392 UNIQUE_VALIDATION_ERROR_CODE *, std::string *) const;
Tobin Ehlis4668dce2016-11-16 09:30:23 -0700393 bool ValidateBufferUsage(BUFFER_STATE const *, VkDescriptorType, UNIQUE_VALIDATION_ERROR_CODE *, std::string *) const;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600394 bool ValidateBufferUpdate(VkDescriptorBufferInfo const *, VkDescriptorType, UNIQUE_VALIDATION_ERROR_CODE *,
395 std::string *) const;
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600396 // Private helper to set all bound cmd buffers to INVALID state
397 void InvalidateBoundCmdBuffers();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700398 bool some_update_; // has any part of the set ever been updated?
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600399 VkDescriptorSet set_;
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600400 DESCRIPTOR_POOL_STATE *pool_state_;
Tobin Ehlis452b4532017-06-21 09:56:13 -0600401 const std::shared_ptr<DescriptorSetLayout const> p_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600402 std::vector<std::unique_ptr<Descriptor>> descriptors_;
Tobin Ehlis3d15c4a2016-06-02 13:04:47 -0600403 // Ptr to device data used for various data look-ups
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600404 const core_validation::layer_data *device_data_;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700405 const VkPhysicalDeviceLimits limits_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600406};
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600407}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700408#endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_