xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_descriptor_set_layout.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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/mesa-blake3.h"
29 #include "util/u_atomic.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 struct vk_descriptor_set_layout {
36    struct vk_object_base base;
37 
38    /* BLAKE3 hash of the descriptor set layout.  This is used by the common
39     * pipeline code to properly cache shaders, including handling pipeline
40     * layouts.  It must be populated by the driver or you risk pipeline cache
41     * collisions.
42     */
43    blake3_hash blake3;
44 
45    void (*destroy)(struct vk_device *device,
46                    struct vk_descriptor_set_layout *layout);
47 
48    /** Reference count
49     *
50     * It's often necessary to store a pointer to the descriptor set layout in
51     * the descriptor so that any entrypoint which has access to a descriptor
52     * set also has the layout.  While layouts are often passed into various
53     * entrypoints, they're notably missing from vkUpdateDescriptorSets().  In
54     * order to implement descriptor writes, you either need to stash a pointer
55     * to the descriptor set layout in the descriptor set or you need to copy
56     * all of the relevant information.  Storing a pointer is a lot cheaper.
57     *
58     * Because descriptor set layout lifetimes and descriptor set lifetimes are
59     * not guaranteed to coincide, we have to reference count if we're going to
60     * do this.
61     */
62    uint32_t ref_cnt;
63 };
64 
65 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_descriptor_set_layout, base,
66                                VkDescriptorSetLayout,
67                                VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT);
68 
69 void *vk_descriptor_set_layout_zalloc(struct vk_device *device, size_t size);
70 
71 void *vk_descriptor_set_layout_multizalloc(struct vk_device *device,
72                                            struct vk_multialloc *ma);
73 
74 void vk_descriptor_set_layout_destroy(struct vk_device *device,
75                                       struct vk_descriptor_set_layout *layout);
76 
77 static inline struct vk_descriptor_set_layout *
vk_descriptor_set_layout_ref(struct vk_descriptor_set_layout * layout)78 vk_descriptor_set_layout_ref(struct vk_descriptor_set_layout *layout)
79 {
80    assert(layout && layout->ref_cnt >= 1);
81    p_atomic_inc(&layout->ref_cnt);
82    return layout;
83 }
84 
85 static inline void
vk_descriptor_set_layout_unref(struct vk_device * device,struct vk_descriptor_set_layout * layout)86 vk_descriptor_set_layout_unref(struct vk_device *device,
87                                struct vk_descriptor_set_layout *layout)
88 {
89    assert(layout && layout->ref_cnt >= 1);
90    if (p_atomic_dec_zero(&layout->ref_cnt))
91       layout->destroy(device, layout);
92 }
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif /* VK_DESCRIPTOR_SET_LAYOUT_H */
99 
100