1/*
2 * Copyright © 2022 Collabora Ltd
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23#ifndef VK_PIPELINE_LAYOUT_H
24#define VK_PIPELINE_LAYOUT_H
25
26#include "vk_object.h"
27
28#include "util/u_atomic.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34struct vk_descriptor_set_layout;
35
36#define VK_MESA_PIPELINE_LAYOUT_MAX_SETS 32
37
38struct vk_pipeline_layout {
39   struct vk_object_base base;
40
41   /** Reference count
42    *
43    * It's often necessary to store a pointer to the descriptor set layout in
44    * the descriptor so that any entrypoint which has access to a descriptor
45    * set also has the layout.  While layouts are often passed into various
46    * entrypoints, they're notably missing from vkUpdateDescriptorSets().  In
47    * order to implement descriptor writes, you either need to stash a pointer
48    * to the descriptor set layout in the descriptor set or you need to copy
49    * all of the relevant information.  Storing a pointer is a lot cheaper.
50    *
51    * Because descriptor set layout lifetimes and descriptor set lifetimes are
52    * not guaranteed to coincide, we have to reference count if we're going to
53    * do this.
54    */
55   uint32_t ref_cnt;
56
57   /** VkPipelineLayoutCreateInfo::flags */
58   VkPipelineLayoutCreateFlagBits create_flags;
59
60   /** Number of descriptor set layouts in this pipeline layout */
61   uint32_t set_count;
62
63   /** Array of pointers to descriptor set layouts, indexed by set index */
64   const struct vk_descriptor_set_layout *set_layouts[VK_MESA_PIPELINE_LAYOUT_MAX_SETS];
65
66   /** Destroy callback
67    *
68    * Will be initially set to vk_pipeline_layout_destroy() but may be set to
69    * a driver-specific callback which does driver-specific clean-up and then
70    * calls vk_pipeline_layout_destroy().
71    */
72   void (*destroy)(struct vk_device *device,
73                   struct vk_pipeline_layout *layout);
74};
75
76VK_DEFINE_NONDISP_HANDLE_CASTS(vk_pipeline_layout, base, VkPipelineLayout,
77                               VK_OBJECT_TYPE_PIPELINE_LAYOUT);
78
79void *
80vk_pipeline_layout_zalloc(struct vk_device *device, size_t size,
81                          const VkPipelineLayoutCreateInfo *pCreateInfo);
82
83void *
84vk_pipeline_layout_multizalloc(struct vk_device *device,
85                               struct vk_multialloc *ma,
86                               const VkPipelineLayoutCreateInfo *pCreateInfo);
87
88void vk_pipeline_layout_destroy(struct vk_device *device,
89                                struct vk_pipeline_layout *layout);
90
91static inline struct vk_pipeline_layout *
92vk_pipeline_layout_ref(struct vk_pipeline_layout *layout)
93{
94   assert(layout && layout->ref_cnt >= 1);
95   p_atomic_inc(&layout->ref_cnt);
96   return layout;
97}
98
99static inline void
100vk_pipeline_layout_unref(struct vk_device *device,
101                         struct vk_pipeline_layout *layout)
102{
103   assert(layout && layout->ref_cnt >= 1);
104   if (p_atomic_dec_zero(&layout->ref_cnt))
105      layout->destroy(device, layout);
106}
107
108#ifdef __cplusplus
109}
110#endif
111
112#endif /* VK_PIPELINE_LAYOUT_H */
113
114