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 <= VK_MESA_PIPELINE_LAYOUT_MAX_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
60 void *
vk_pipeline_layout_zalloc(struct vk_device * device,size_t size,const VkPipelineLayoutCreateInfo * pCreateInfo)61 vk_pipeline_layout_zalloc(struct vk_device *device, size_t size,
62 const VkPipelineLayoutCreateInfo *pCreateInfo)
63 {
64 /* Because we're reference counting and lifetimes may not be what the
65 * client expects, these have to be allocated off the device and not as
66 * their own object.
67 */
68 struct vk_pipeline_layout *layout =
69 vk_zalloc(&device->alloc, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
70 if (layout == NULL)
71 return NULL;
72
73 vk_pipeline_layout_init(device, layout, pCreateInfo);
74 return layout;
75 }
76
77 void *
vk_pipeline_layout_multizalloc(struct vk_device * device,struct vk_multialloc * ma,const VkPipelineLayoutCreateInfo * pCreateInfo)78 vk_pipeline_layout_multizalloc(struct vk_device *device,
79 struct vk_multialloc *ma,
80 const VkPipelineLayoutCreateInfo *pCreateInfo)
81 {
82 struct vk_pipeline_layout *layout =
83 vk_multialloc_zalloc(ma, &device->alloc,
84 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
85 if (layout == NULL)
86 return NULL;
87
88 vk_pipeline_layout_init(device, layout, pCreateInfo);
89 return layout;
90 }
91
92
93 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_CreatePipelineLayout(VkDevice _device,const VkPipelineLayoutCreateInfo * pCreateInfo,UNUSED const VkAllocationCallbacks * pAllocator,VkPipelineLayout * pPipelineLayout)94 vk_common_CreatePipelineLayout(VkDevice _device,
95 const VkPipelineLayoutCreateInfo *pCreateInfo,
96 UNUSED const VkAllocationCallbacks *pAllocator,
97 VkPipelineLayout *pPipelineLayout)
98 {
99 VK_FROM_HANDLE(vk_device, device, _device);
100
101 struct vk_pipeline_layout *layout =
102 vk_pipeline_layout_zalloc(device, sizeof(struct vk_pipeline_layout),
103 pCreateInfo);
104 if (layout == NULL)
105 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
106
107 *pPipelineLayout = vk_pipeline_layout_to_handle(layout);
108
109 return VK_SUCCESS;
110 }
111
112 void
vk_pipeline_layout_destroy(struct vk_device * device,struct vk_pipeline_layout * layout)113 vk_pipeline_layout_destroy(struct vk_device *device,
114 struct vk_pipeline_layout *layout)
115 {
116 assert(layout && layout->ref_cnt == 0);
117
118 for (uint32_t s = 0; s < layout->set_count; s++) {
119 if (layout->set_layouts[s] != NULL)
120 vk_descriptor_set_layout_unref(device, (void *)layout->set_layouts[s]);
121 }
122
123 vk_object_free(device, NULL, layout);
124 }
125
126 VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyPipelineLayout(VkDevice _device,VkPipelineLayout pipelineLayout,UNUSED const VkAllocationCallbacks * pAllocator)127 vk_common_DestroyPipelineLayout(VkDevice _device,
128 VkPipelineLayout pipelineLayout,
129 UNUSED const VkAllocationCallbacks *pAllocator)
130 {
131 VK_FROM_HANDLE(vk_device, device, _device);
132 VK_FROM_HANDLE(vk_pipeline_layout, layout, pipelineLayout);
133
134 if (layout == NULL)
135 return;
136
137 vk_pipeline_layout_unref(device, layout);
138 }
139