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_DESCRIPTOR_SET_LAYOUT_H
24 #define VK_DESCRIPTOR_SET_LAYOUT_H
25
26 #include "vk_object.h"
27
28 #include "util/u_atomic.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 struct vk_descriptor_set_layout {
35 struct vk_object_base base;
36
37 /** Reference count
38 *
39 * It's often necessary to store a pointer to the descriptor set layout in
40 * the descriptor so that any entrypoint which has access to a descriptor
41 * set also has the layout. While layouts are often passed into various
42 * entrypoints, they're notably missing from vkUpdateDescriptorSets(). In
43 * order to implement descriptor writes, you either need to stash a pointer
44 * to the descriptor set layout in the descriptor set or you need to copy
45 * all of the relevant information. Storing a pointer is a lot cheaper.
46 *
47 * Because descriptor set layout lifetimes and descriptor set lifetimes are
48 * not guaranteed to coincide, we have to reference count if we're going to
49 * do this.
50 */
51 uint32_t ref_cnt;
52 };
53
54 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_descriptor_set_layout, base,
55 VkDescriptorSetLayout,
56 VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT);
57
58 void *vk_descriptor_set_layout_zalloc(struct vk_device *device, size_t size);
59
60 void *vk_descriptor_set_layout_multizalloc(struct vk_device *device,
61 struct vk_multialloc *ma);
62
63 static inline struct vk_descriptor_set_layout *
vk_descriptor_set_layout_ref(struct vk_descriptor_set_layout * layout)64 vk_descriptor_set_layout_ref(struct vk_descriptor_set_layout *layout)
65 {
66 assert(layout && layout->ref_cnt >= 1);
67 p_atomic_inc(&layout->ref_cnt);
68 return layout;
69 }
70
71 static inline void
vk_descriptor_set_layout_unref(struct vk_device * device,struct vk_descriptor_set_layout * layout)72 vk_descriptor_set_layout_unref(struct vk_device *device,
73 struct vk_descriptor_set_layout *layout)
74 {
75 assert(layout && layout->ref_cnt >= 1);
76 if (p_atomic_dec_zero(&layout->ref_cnt))
77 vk_object_free(device, NULL, layout);
78 }
79
80 #ifdef __cplusplus
81 }
82 #endif
83
84 #endif /* VK_DESCRIPTOR_SET_LAYOUT_H */
85
86