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    void (*destroy)(struct vk_device *device,
38                    struct vk_descriptor_set_layout *layout);
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 
57 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_descriptor_set_layout, base,
58                                VkDescriptorSetLayout,
59                                VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT);
60 
61 void *vk_descriptor_set_layout_zalloc(struct vk_device *device, size_t size);
62 
63 void *vk_descriptor_set_layout_multizalloc(struct vk_device *device,
64                                            struct vk_multialloc *ma);
65 
66 void vk_descriptor_set_layout_destroy(struct vk_device *device,
67                                       struct vk_descriptor_set_layout *layout);
68 
69 static inline struct vk_descriptor_set_layout *
vk_descriptor_set_layout_ref(struct vk_descriptor_set_layout * layout)70 vk_descriptor_set_layout_ref(struct vk_descriptor_set_layout *layout)
71 {
72    assert(layout && layout->ref_cnt >= 1);
73    p_atomic_inc(&layout->ref_cnt);
74    return layout;
75 }
76 
77 static inline void
vk_descriptor_set_layout_unref(struct vk_device * device,struct vk_descriptor_set_layout * layout)78 vk_descriptor_set_layout_unref(struct vk_device *device,
79                                struct vk_descriptor_set_layout *layout)
80 {
81    assert(layout && layout->ref_cnt >= 1);
82    if (p_atomic_dec_zero(&layout->ref_cnt))
83       layout->destroy(device, layout);
84 }
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #endif /* VK_DESCRIPTOR_SET_LAYOUT_H */
91 
92