blob: fb08e3dc2b2ab61f070ec4ae41dbe8b1b45497a9 [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
24#if defined(__clang__)
25#if __has_feature(cxx_noexcept)
26#define HAS_NOEXCEPT
27#endif
28#else
29#if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46
30#define HAS_NOEXCEPT
31#else
32#if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026 && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS
33#define HAS_NOEXCEPT
34#endif
35#endif
36#endif
37
38#ifdef HAS_NOEXCEPT
39#define NOEXCEPT noexcept
40#else
41#define NOEXCEPT
42#endif
43
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060044#include "core_validation_error_enums.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060045#include "core_validation_types.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060046#include "vk_layer_logging.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060047#include "vk_layer_utils.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060048#include "vk_safe_struct.h"
49#include "vulkan/vk_layer.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050#include <memory>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060051#include <unordered_map>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060052#include <unordered_set>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060053#include <vector>
54
55// Descriptor Data structures
56
57/*
58 * DescriptorSetLayout class
59 *
60 * Overview - This class encapsulates the Vulkan VkDescriptorSetLayout data (layout).
61 * A layout consists of some number of bindings, each of which has a binding#, a
62 * type, descriptor count, stage flags, and pImmutableSamplers.
63 *
64 * Index vs Binding - A layout is created with an array of VkDescriptorSetLayoutBinding
65 * where each array index will have a corresponding binding# that is defined in that struct.
66 * This class, therefore, provides utility functions where you can retrieve data for
67 * layout bindings based on either the original index into the pBindings array, or based
68 * on the binding#.
69 * Typically if you want to cover all of the bindings in a layout, you can do that by
70 * iterating over GetBindingCount() bindings and using the Get*FromIndex() functions.
71 * Otherwise, you can use the Get*FromBinding() functions to just grab binding info
72 * for a particular binding#.
73 *
74 * Global Index - The "Index" referenced above is the index into the original pBindings
75 * array. So there are as many indices as there are bindings.
76 * This class also has the concept of a Global Index. For the global index functions,
77 * there are as many global indices as there are descriptors in the layout.
78 * For the global index, consider all of the bindings to be a flat array where
79 * descriptor 0 of pBinding[0] is index 0 and each descriptor in the layout increments
80 * from there. So if pBinding[0] in this example had descriptorCount of 10, then
81 * the GlobalStartIndex of pBinding[1] will be 10 where 0-9 are the global indices
82 * for pBinding[0].
83 */
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060084namespace cvdescriptorset {
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060085class DescriptorSetLayout {
86 public:
87 // Constructors and destructor
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060088 DescriptorSetLayout(debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *p_create_info,
89 const VkDescriptorSetLayout layout);
90 ~DescriptorSetLayout();
91 // Straightforward Get functions
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060092 VkDescriptorSetLayout GetDescriptorSetLayout() const { return layout_; };
93 uint32_t GetTotalDescriptorCount() const { return descriptor_count_; };
94 uint32_t GetDynamicDescriptorCount() const { return dynamic_descriptor_count_; };
95 uint32_t GetBindingCount() const { return binding_count_; };
96 // Fill passed-in set with bindings
97 void FillBindingSet(std::unordered_set<uint32_t> *) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060098 // Return true if given binding is present in this layout
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060099 bool HasBinding(const uint32_t binding) const { return binding_to_index_map_.count(binding) > 0; };
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600100 // Return true if this layout is compatible with passed in layout,
101 // else return false and update error_msg with description of incompatibility
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600102 bool IsCompatible(const DescriptorSetLayout *, std::string *) const;
103 // Return true if binding 1 beyond given exists and has same type, stageFlags & immutable sampler use
104 bool IsNextBindingConsistent(const uint32_t) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600105 // Various Get functions that can either be passed a binding#, which will
106 // be automatically translated into the appropriate index from the original
107 // pBindings array, or the index# can be passed in directly
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600108 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t) const;
109 VkDescriptorSetLayoutBinding const *GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t) const;
110 uint32_t GetDescriptorCountFromBinding(const uint32_t) const;
111 uint32_t GetDescriptorCountFromIndex(const uint32_t) const;
112 VkDescriptorType GetTypeFromBinding(const uint32_t) const;
113 VkDescriptorType GetTypeFromIndex(const uint32_t) const;
114 VkDescriptorType GetTypeFromGlobalIndex(const uint32_t) const;
115 VkShaderStageFlags GetStageFlagsFromBinding(const uint32_t) const;
116 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t) const;
117 VkSampler const *GetImmutableSamplerPtrFromIndex(const uint32_t) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600118 // For a particular binding, get the global index
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600119 uint32_t GetGlobalStartIndexFromBinding(const uint32_t) const;
120 uint32_t GetGlobalEndIndexFromBinding(const uint32_t) const;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600121
122 private:
123 VkDescriptorSetLayout layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600124 std::unordered_map<uint32_t, uint32_t> binding_to_index_map_;
125 std::unordered_map<uint32_t, uint32_t> binding_to_global_start_index_map_;
126 std::unordered_map<uint32_t, uint32_t> binding_to_global_end_index_map_;
127 // VkDescriptorSetLayoutCreateFlags flags_;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600128 uint32_t binding_count_; // # of bindings in this layout
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600129 std::vector<safe_VkDescriptorSetLayoutBinding *> bindings_;
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600130 uint32_t descriptor_count_; // total # descriptors in this layout
131 uint32_t dynamic_descriptor_count_;
132};
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600133
134/*
135 * Descriptor classes
136 * Descriptor is an abstract base class from which 5 separate descriptor types are derived.
137 * This allows the WriteUpdate() and CopyUpdate() operations to be specialized per
138 * descriptor type, but all descriptors in a set can be accessed via the common Descriptor*.
139 */
140
141// Slightly broader than type, each c++ "class" will has a corresponding "DescriptorClass"
142typedef enum _DescriptorClass { PlainSampler, ImageSampler, Image, TexelBuffer, GeneralBuffer } DescriptorClass;
143
144class Descriptor {
145 public:
146 virtual ~Descriptor(){};
147 virtual bool WriteUpdate(const VkWriteDescriptorSet *, const uint32_t, std::string *) = 0;
148 virtual bool CopyUpdate(const Descriptor *, std::string *) = 0;
149 virtual DescriptorClass GetClass() const { return descriptor_class; };
150 // Special fast-path check for SamplerDescriptors that are immutable
151 virtual bool IsImmutableSampler() const { return false; };
152 // Check for dynamic descriptor type
153 virtual bool IsDynamic() const { return false; };
154 // Check for storage descriptor type
155 virtual bool IsStorage() const { return false; };
156 bool updated; // Has descriptor been updated?
157 DescriptorClass descriptor_class;
158};
159// Shared helper functions - These are useful because the shared sampler image descriptor type
160// performs common functions with both sampler and image descriptors so they can share their common functions
161bool ValidateSampler(const VkSampler, const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *);
162bool ValidateImageUpdate(const VkImageView, const VkImageLayout, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *,
163 const std::unordered_map<VkImage, IMAGE_NODE> *, const std::unordered_map<VkImage, VkSwapchainKHR> *,
164 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *, std::string *);
165
166class SamplerDescriptor : public Descriptor {
167 public:
168 SamplerDescriptor(const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *);
169 SamplerDescriptor(const VkSampler *, const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *);
170 bool WriteUpdate(const VkWriteDescriptorSet *, const uint32_t, std::string *) override;
171 bool CopyUpdate(const Descriptor *, std::string *) override;
172 virtual bool IsImmutableSampler() const override { return immutable_; };
173
174 private:
175 // bool ValidateSampler(const VkSampler) const;
176 VkSampler sampler_;
177 bool immutable_;
178 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map_;
Tobin Ehlis546326f2016-04-26 11:06:05 -0600179};
180
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600181class ImageSamplerDescriptor : public Descriptor {
182 public:
183 ImageSamplerDescriptor(const std::unordered_map<VkImageView, VkImageViewCreateInfo> *,
184 const std::unordered_map<VkImage, IMAGE_NODE> *, const std::unordered_map<VkImage, VkSwapchainKHR> *,
185 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *,
186 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *);
187 ImageSamplerDescriptor(const VkSampler *, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *,
188 const std::unordered_map<VkImage, IMAGE_NODE> *, const std::unordered_map<VkImage, VkSwapchainKHR> *,
189 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *,
190 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *);
191 bool WriteUpdate(const VkWriteDescriptorSet *, const uint32_t, std::string *) override;
192 bool CopyUpdate(const Descriptor *, std::string *) override;
193
194 private:
195 VkSampler sampler_;
196 bool immutable_;
197 VkImageView image_view_;
198 VkImageLayout image_layout_;
199 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map_;
200 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map_;
201 const std::unordered_map<VkImage, IMAGE_NODE> *image_map_;
202 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map_;
203 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map_;
204};
205
206class ImageDescriptor : public Descriptor {
207 public:
208 ImageDescriptor(const VkDescriptorType, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *,
209 const std::unordered_map<VkImage, IMAGE_NODE> *, const std::unordered_map<VkImage, VkSwapchainKHR> *,
210 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *);
211 bool WriteUpdate(const VkWriteDescriptorSet *, const uint32_t, std::string *) override;
212 bool CopyUpdate(const Descriptor *, std::string *) override;
213 VkImageView GetImageView() const { return image_view_; }
214 VkImageLayout GetImageLayout() const { return image_layout_; }
215
216 private:
217 bool storage_;
218 VkImageView image_view_;
219 VkImageLayout image_layout_;
220 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map_;
221 const std::unordered_map<VkImage, IMAGE_NODE> *image_map_;
222 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map_;
223 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map_;
224};
225
226class TexelDescriptor : public Descriptor {
227 public:
228 TexelDescriptor(const VkDescriptorType, const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *);
229 bool WriteUpdate(const VkWriteDescriptorSet *, const uint32_t, std::string *) override;
230 bool CopyUpdate(const Descriptor *, std::string *) override;
231 VkBufferView GetBufferView() const { return buffer_view_; };
232
233 private:
234 VkBufferView buffer_view_;
235 bool storage_;
236 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map_;
237};
238
239class BufferDescriptor : public Descriptor {
240 public:
241 BufferDescriptor(const VkDescriptorType, const std::unordered_map<VkBuffer, BUFFER_NODE> *);
242 bool WriteUpdate(const VkWriteDescriptorSet *, const uint32_t, std::string *) override;
243 bool CopyUpdate(const Descriptor *, std::string *) override;
244 virtual bool IsDynamic() const override { return dynamic_; }
245 virtual bool IsStorage() const override { return storage_; }
246 VkBuffer GetBuffer() const { return buffer_; }
247 VkDeviceSize GetOffset() const { return offset_; }
248 VkDeviceSize GetRange() const { return range_; }
249
250 private:
251 bool storage_;
252 bool dynamic_;
253 VkBuffer buffer_;
254 VkDeviceSize offset_;
255 VkDeviceSize range_;
256 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map_;
257};
258/*
259 * DescriptorSet class
260 *
261 * Overview - This class encapsulates the Vulkan VkDescriptorSet data (set).
262 * A set has an underlying layout which defines the bindings in the set and the
263 * types and numbers of descriptors in each descriptor slot. Most of the layout
264 * interfaces are exposed through identically-named functions in the set class.
265 * Please refer to the DescriptorSetLayout comment above for a description of
266 * index, binding, and global index.
267 *
268 * At construction a vector of Descriptor* is created with types corresponding to the
269 * layout. The primary operation performed on the descriptors is to update them
270 * via write or copy updates, and validate that the update contents are correct.
271 * In order to validate update contents, the DescriptorSet stores a bunch of ptrs
272 * to data maps where various Vulkan objects can be looked up. The management of
273 * those maps is performed externally. The set class relies on their contents to
274 * be correct at the time of update.
275 */
276class DescriptorSet {
277 public:
278 DescriptorSet(const VkDescriptorSet, const DescriptorSetLayout *, const std::unordered_map<VkBuffer, BUFFER_NODE> *,
279 const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *,
280 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *,
281 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *,
282 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *, const std::unordered_map<VkImage, IMAGE_NODE> *,
283 const std::unordered_map<VkImage, VkSwapchainKHR> *,
284 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *);
285 ~DescriptorSet(){};
286 // A number of common Get* functions that return data based on layout from which this set was created
287 uint32_t GetTotalDescriptorCount() const { return p_layout_ ? p_layout_->GetTotalDescriptorCount() : 0; };
288 uint32_t GetDynamicDescriptorCount() const { return p_layout_ ? p_layout_->GetDynamicDescriptorCount() : 0; };
289 uint32_t GetBindingCount() const { return p_layout_ ? p_layout_->GetBindingCount() : 0; };
290 VkDescriptorType GetTypeFromIndex(const uint32_t index) const {
291 return p_layout_ ? p_layout_->GetTypeFromIndex(index) : VK_DESCRIPTOR_TYPE_MAX_ENUM;
292 };
293 VkDescriptorType GetTypeFromGlobalIndex(const uint32_t index) const {
294 return p_layout_ ? p_layout_->GetTypeFromGlobalIndex(index) : VK_DESCRIPTOR_TYPE_MAX_ENUM;
295 };
296 VkDescriptorType GetTypeFromBinding(const uint32_t binding) const {
297 return p_layout_ ? p_layout_->GetTypeFromBinding(binding) : VK_DESCRIPTOR_TYPE_MAX_ENUM;
298 };
299 uint32_t GetDescriptorCountFromIndex(const uint32_t index) const {
300 return p_layout_ ? p_layout_->GetDescriptorCountFromIndex(index) : 0;
301 };
302 uint32_t GetDescriptorCountFromBinding(const uint32_t binding) const {
303 return p_layout_ ? p_layout_->GetDescriptorCountFromBinding(binding) : 0;
304 };
305 // Return true if given binding is present in this set
306 bool HasBinding(const uint32_t binding) const { return p_layout_->HasBinding(binding); };
307 // Is this set compatible with the given layout?
308 bool IsCompatible(const DescriptorSetLayout *, std::string *) const;
309 // For given bindings validate state at time of draw is correct, returning false on error and writing error details into string*
310 bool ValidateDrawState(const std::unordered_set<uint32_t> &, const std::vector<uint32_t> &, std::string *) const;
311 // For given set of bindings, add any buffers and images that will be updated to their respective unordered_sets & return number
312 // of objects inserted
313 uint32_t GetStorageUpdates(const std::unordered_set<uint32_t> &, std::unordered_set<VkBuffer> *,
314 std::unordered_set<VkImageView> *) const;
315 // For all descriptors in a set, add any buffers and images that may be updated to their respective unordered_sets & return
316 // number of objects inserted
317 uint32_t GetAllStorageUpdates(std::unordered_set<VkBuffer> *, std::unordered_set<VkImageView> *) const;
318 // Perform write update based on update struct
319 bool WriteUpdate(debug_report_data *, const VkWriteDescriptorSet *, std::string *);
320 // Perform copy update, using 'this' set as the dest and the passed-in DescriptorSet as the src
321 bool CopyUpdate(debug_report_data *, const VkCopyDescriptorSet *, const DescriptorSet *, std::string *);
322
323 VkDescriptorSet GetSet() const { return set_; };
324 // Return unordered_set of all command buffers that this set is bound to
325 std::unordered_set<VkCommandBuffer> GetBoundCmdBuffers() const { return bound_cmd_buffers_; }
326 // Bind given cmd_buffer to this descriptor set
327 void BindCommandBuffer(const VkCommandBuffer cmd_buffer) { bound_cmd_buffers_.insert(cmd_buffer); }
328 // If given cmd_buffer is in the bound_cmd_buffers_ set, remove it
329 void RemoveBoundCommandBuffer(const VkCommandBuffer cmd_buffer) { bound_cmd_buffers_.erase(cmd_buffer); }
330 VkSampler const *GetImmutableSamplerPtrFromBinding(const uint32_t index) const {
331 return p_layout_->GetImmutableSamplerPtrFromBinding(index);
332 };
333 // For a particular binding, get the global index
334 uint32_t GetGlobalStartIndexFromBinding(const uint32_t binding) const {
335 return p_layout_->GetGlobalStartIndexFromBinding(binding);
336 };
337 uint32_t GetGlobalEndIndexFromBinding(const uint32_t binding) const {
338 return p_layout_->GetGlobalEndIndexFromBinding(binding);
339 };
340 // Return true if any part of set has ever been updated
341 bool IsUpdated() const { return some_update_; };
342 // Return true if the binding at the given global index has been updated
343 bool IsUpdated(const uint32_t global_index) const;
344
345 private:
346 bool ValidateUpdate(const VkWriteDescriptorSet *, const uint32_t, std::string *) const;
347 bool VerifyUpdateConsistency(uint32_t, uint32_t, uint32_t, const char *, std::string *) const;
348 bool some_update_; // has any part of the set ever been updated?
349 bool full_update_; // has every descriptor in the set been updated?
350 VkDescriptorSet set_;
351 uint32_t descriptor_count_; // Count of all descriptors in this set
352 const DescriptorSetLayout *p_layout_;
353 std::unordered_set<VkCommandBuffer> bound_cmd_buffers_;
354 std::vector<std::unique_ptr<Descriptor>> descriptors_;
355 // Ptrs to object containers to verify bound data
356 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map_;
357 const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *memory_map_;
358 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map_;
359 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map_;
360 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map_;
361 // TODO : For next 3 maps all we really need (currently) is an image to format mapping
362 const std::unordered_map<VkImage, IMAGE_NODE> *image_map_;
363 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map_;
364 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map_;
365};
Tobin Ehlis2d9deec2016-04-21 14:19:26 -0600366}
Chris Forbes6f6844a2016-04-27 14:00:44 +1200367#endif // CORE_VALIDATION_DESCRIPTOR_SETS_H_