xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_pipeline_layout.c (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 
24 #include "vk_pipeline_layout.h"
25 
26 #include "vk_alloc.h"
27 #include "vk_common_entrypoints.h"
28 #include "vk_descriptor_set_layout.h"
29 #include "vk_device.h"
30 #include "vk_log.h"
31 
32 #include "util/mesa-sha1.h"
33 
34 static void
vk_pipeline_layout_init(struct vk_device * device,struct vk_pipeline_layout * layout,const VkPipelineLayoutCreateInfo * pCreateInfo)35 vk_pipeline_layout_init(struct vk_device *device,
36                         struct vk_pipeline_layout *layout,
37                         const VkPipelineLayoutCreateInfo *pCreateInfo)
38 {
39    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
40    assert(pCreateInfo->setLayoutCount <= MESA_VK_MAX_DESCRIPTOR_SETS);
41 
42    vk_object_base_init(device, &layout->base, VK_OBJECT_TYPE_PIPELINE_LAYOUT);
43 
44    layout->ref_cnt = 1;
45    layout->create_flags = pCreateInfo->flags;
46    layout->set_count = pCreateInfo->setLayoutCount;
47    layout->destroy = vk_pipeline_layout_destroy;
48 
49    for (uint32_t s = 0; s < pCreateInfo->setLayoutCount; s++) {
50       VK_FROM_HANDLE(vk_descriptor_set_layout, set_layout,
51                      pCreateInfo->pSetLayouts[s]);
52 
53       if (set_layout != NULL)
54          layout->set_layouts[s] = vk_descriptor_set_layout_ref(set_layout);
55       else
56          layout->set_layouts[s] = NULL;
57    }
58 
59    assert(pCreateInfo->pushConstantRangeCount <
60           MESA_VK_MAX_PUSH_CONSTANT_RANGES);
61    layout->push_range_count = pCreateInfo->pushConstantRangeCount;
62    for (uint32_t r = 0; r < pCreateInfo->pushConstantRangeCount; r++)
63       layout->push_ranges[r] = pCreateInfo->pPushConstantRanges[r];
64 }
65 
66 void *
vk_pipeline_layout_zalloc(struct vk_device * device,size_t size,const VkPipelineLayoutCreateInfo * pCreateInfo)67 vk_pipeline_layout_zalloc(struct vk_device *device, size_t size,
68                           const VkPipelineLayoutCreateInfo *pCreateInfo)
69 {
70    /* Because we're reference counting and lifetimes may not be what the
71     * client expects, these have to be allocated off the device and not as
72     * their own object.
73     */
74    struct vk_pipeline_layout *layout =
75       vk_zalloc(&device->alloc, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
76    if (layout == NULL)
77       return NULL;
78 
79    vk_pipeline_layout_init(device, layout, pCreateInfo);
80    return layout;
81 }
82 
83 void *
vk_pipeline_layout_multizalloc(struct vk_device * device,struct vk_multialloc * ma,const VkPipelineLayoutCreateInfo * pCreateInfo)84 vk_pipeline_layout_multizalloc(struct vk_device *device,
85                                struct vk_multialloc *ma,
86                                const VkPipelineLayoutCreateInfo *pCreateInfo)
87 {
88    struct vk_pipeline_layout *layout =
89       vk_multialloc_zalloc(ma, &device->alloc,
90                            VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
91    if (layout == NULL)
92       return NULL;
93 
94    vk_pipeline_layout_init(device, layout, pCreateInfo);
95    return layout;
96 }
97 
98 
99 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_CreatePipelineLayout(VkDevice _device,const VkPipelineLayoutCreateInfo * pCreateInfo,UNUSED const VkAllocationCallbacks * pAllocator,VkPipelineLayout * pPipelineLayout)100 vk_common_CreatePipelineLayout(VkDevice _device,
101                                const VkPipelineLayoutCreateInfo *pCreateInfo,
102                                UNUSED const VkAllocationCallbacks *pAllocator,
103                                VkPipelineLayout *pPipelineLayout)
104 {
105    VK_FROM_HANDLE(vk_device, device, _device);
106 
107    struct vk_pipeline_layout *layout =
108       vk_pipeline_layout_zalloc(device, sizeof(struct vk_pipeline_layout),
109                                 pCreateInfo);
110    if (layout == NULL)
111       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
112 
113    *pPipelineLayout = vk_pipeline_layout_to_handle(layout);
114 
115    return VK_SUCCESS;
116 }
117 
118 void
vk_pipeline_layout_destroy(struct vk_device * device,struct vk_pipeline_layout * layout)119 vk_pipeline_layout_destroy(struct vk_device *device,
120                            struct vk_pipeline_layout *layout)
121 {
122    assert(layout && layout->ref_cnt == 0);
123 
124    for (uint32_t s = 0; s < layout->set_count; s++) {
125       if (layout->set_layouts[s] != NULL)
126          vk_descriptor_set_layout_unref(device, layout->set_layouts[s]);
127    }
128 
129    vk_object_free(device, NULL, layout);
130 }
131 
132 VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyPipelineLayout(VkDevice _device,VkPipelineLayout pipelineLayout,UNUSED const VkAllocationCallbacks * pAllocator)133 vk_common_DestroyPipelineLayout(VkDevice _device,
134                                 VkPipelineLayout pipelineLayout,
135                                 UNUSED const VkAllocationCallbacks *pAllocator)
136 {
137    VK_FROM_HANDLE(vk_device, device, _device);
138    VK_FROM_HANDLE(vk_pipeline_layout, layout, pipelineLayout);
139 
140    if (layout == NULL)
141       return;
142 
143    vk_pipeline_layout_unref(device, layout);
144 }
145