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_limits.h"
27 #include "vk_object.h"
28
29 #include "util/u_atomic.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 struct vk_descriptor_set_layout;
36
37 struct vk_pipeline_layout {
38 struct vk_object_base base;
39
40 /** Reference count
41 *
42 * It's often necessary to store a pointer to the descriptor set layout in
43 * the descriptor so that any entrypoint which has access to a descriptor
44 * set also has the layout. While layouts are often passed into various
45 * entrypoints, they're notably missing from vkUpdateDescriptorSets(). In
46 * order to implement descriptor writes, you either need to stash a pointer
47 * to the descriptor set layout in the descriptor set or you need to copy
48 * all of the relevant information. Storing a pointer is a lot cheaper.
49 *
50 * Because descriptor set layout lifetimes and descriptor set lifetimes are
51 * not guaranteed to coincide, we have to reference count if we're going to
52 * do this.
53 */
54 uint32_t ref_cnt;
55
56 /** VkPipelineLayoutCreateInfo::flags */
57 VkPipelineLayoutCreateFlagBits create_flags;
58
59 /** Number of descriptor set layouts in this pipeline layout */
60 uint32_t set_count;
61
62 /** Array of pointers to descriptor set layouts, indexed by set index */
63 struct vk_descriptor_set_layout *set_layouts[MESA_VK_MAX_DESCRIPTOR_SETS];
64
65 /** Number of push constant ranges in this pipeline layout */
66 uint32_t push_range_count;
67
68 /** Array of push constant ranges */
69 VkPushConstantRange push_ranges[MESA_VK_MAX_PUSH_CONSTANT_RANGES];
70
71 /** Destroy callback
72 *
73 * Will be initially set to vk_pipeline_layout_destroy() but may be set to
74 * a driver-specific callback which does driver-specific clean-up and then
75 * calls vk_pipeline_layout_destroy().
76 */
77 void (*destroy)(struct vk_device *device,
78 struct vk_pipeline_layout *layout);
79 };
80
81 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_pipeline_layout, base, VkPipelineLayout,
82 VK_OBJECT_TYPE_PIPELINE_LAYOUT);
83
84 void *
85 vk_pipeline_layout_zalloc(struct vk_device *device, size_t size,
86 const VkPipelineLayoutCreateInfo *pCreateInfo);
87
88 void *
89 vk_pipeline_layout_multizalloc(struct vk_device *device,
90 struct vk_multialloc *ma,
91 const VkPipelineLayoutCreateInfo *pCreateInfo);
92
93 void vk_pipeline_layout_destroy(struct vk_device *device,
94 struct vk_pipeline_layout *layout);
95
96 static inline struct vk_pipeline_layout *
vk_pipeline_layout_ref(struct vk_pipeline_layout * layout)97 vk_pipeline_layout_ref(struct vk_pipeline_layout *layout)
98 {
99 assert(layout && layout->ref_cnt >= 1);
100 p_atomic_inc(&layout->ref_cnt);
101 return layout;
102 }
103
104 static inline void
vk_pipeline_layout_unref(struct vk_device * device,struct vk_pipeline_layout * layout)105 vk_pipeline_layout_unref(struct vk_device *device,
106 struct vk_pipeline_layout *layout)
107 {
108 assert(layout && layout->ref_cnt >= 1);
109 if (p_atomic_dec_zero(&layout->ref_cnt))
110 layout->destroy(device, layout);
111 }
112
113 #ifdef __cplusplus
114 }
115 #endif
116
117 #endif /* VK_PIPELINE_LAYOUT_H */
118
119