1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2022 Collabora Ltd
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "vk_pipeline_layout.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "vk_alloc.h"
27bf215546Sopenharmony_ci#include "vk_common_entrypoints.h"
28bf215546Sopenharmony_ci#include "vk_descriptor_set_layout.h"
29bf215546Sopenharmony_ci#include "vk_device.h"
30bf215546Sopenharmony_ci#include "vk_log.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "util/mesa-sha1.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_cistatic void
35bf215546Sopenharmony_civk_pipeline_layout_init(struct vk_device *device,
36bf215546Sopenharmony_ci                        struct vk_pipeline_layout *layout,
37bf215546Sopenharmony_ci                        const VkPipelineLayoutCreateInfo *pCreateInfo)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
40bf215546Sopenharmony_ci   assert(pCreateInfo->setLayoutCount <= VK_MESA_PIPELINE_LAYOUT_MAX_SETS);
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   vk_object_base_init(device, &layout->base, VK_OBJECT_TYPE_PIPELINE_LAYOUT);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   layout->ref_cnt = 1;
45bf215546Sopenharmony_ci   layout->create_flags = pCreateInfo->flags;
46bf215546Sopenharmony_ci   layout->set_count = pCreateInfo->setLayoutCount;
47bf215546Sopenharmony_ci   layout->destroy = vk_pipeline_layout_destroy;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   for (uint32_t s = 0; s < pCreateInfo->setLayoutCount; s++) {
50bf215546Sopenharmony_ci      VK_FROM_HANDLE(vk_descriptor_set_layout, set_layout,
51bf215546Sopenharmony_ci                     pCreateInfo->pSetLayouts[s]);
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci      if (set_layout != NULL)
54bf215546Sopenharmony_ci         layout->set_layouts[s] = vk_descriptor_set_layout_ref(set_layout);
55bf215546Sopenharmony_ci      else
56bf215546Sopenharmony_ci         layout->set_layouts[s] = NULL;
57bf215546Sopenharmony_ci   }
58bf215546Sopenharmony_ci}
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_civoid *
61bf215546Sopenharmony_civk_pipeline_layout_zalloc(struct vk_device *device, size_t size,
62bf215546Sopenharmony_ci                          const VkPipelineLayoutCreateInfo *pCreateInfo)
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci   /* Because we're reference counting and lifetimes may not be what the
65bf215546Sopenharmony_ci    * client expects, these have to be allocated off the device and not as
66bf215546Sopenharmony_ci    * their own object.
67bf215546Sopenharmony_ci    */
68bf215546Sopenharmony_ci   struct vk_pipeline_layout *layout =
69bf215546Sopenharmony_ci      vk_zalloc(&device->alloc, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
70bf215546Sopenharmony_ci   if (layout == NULL)
71bf215546Sopenharmony_ci      return NULL;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   vk_pipeline_layout_init(device, layout, pCreateInfo);
74bf215546Sopenharmony_ci   return layout;
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_civoid *
78bf215546Sopenharmony_civk_pipeline_layout_multizalloc(struct vk_device *device,
79bf215546Sopenharmony_ci                               struct vk_multialloc *ma,
80bf215546Sopenharmony_ci                               const VkPipelineLayoutCreateInfo *pCreateInfo)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   struct vk_pipeline_layout *layout =
83bf215546Sopenharmony_ci      vk_multialloc_zalloc(ma, &device->alloc,
84bf215546Sopenharmony_ci                           VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
85bf215546Sopenharmony_ci   if (layout == NULL)
86bf215546Sopenharmony_ci      return NULL;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   vk_pipeline_layout_init(device, layout, pCreateInfo);
89bf215546Sopenharmony_ci   return layout;
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ciVKAPI_ATTR VkResult VKAPI_CALL
94bf215546Sopenharmony_civk_common_CreatePipelineLayout(VkDevice _device,
95bf215546Sopenharmony_ci                               const VkPipelineLayoutCreateInfo *pCreateInfo,
96bf215546Sopenharmony_ci                               UNUSED const VkAllocationCallbacks *pAllocator,
97bf215546Sopenharmony_ci                               VkPipelineLayout *pPipelineLayout)
98bf215546Sopenharmony_ci{
99bf215546Sopenharmony_ci   VK_FROM_HANDLE(vk_device, device, _device);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   struct vk_pipeline_layout *layout =
102bf215546Sopenharmony_ci      vk_pipeline_layout_zalloc(device, sizeof(struct vk_pipeline_layout),
103bf215546Sopenharmony_ci                                pCreateInfo);
104bf215546Sopenharmony_ci   if (layout == NULL)
105bf215546Sopenharmony_ci      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   *pPipelineLayout = vk_pipeline_layout_to_handle(layout);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   return VK_SUCCESS;
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_civoid
113bf215546Sopenharmony_civk_pipeline_layout_destroy(struct vk_device *device,
114bf215546Sopenharmony_ci                           struct vk_pipeline_layout *layout)
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci   assert(layout && layout->ref_cnt == 0);
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   for (uint32_t s = 0; s < layout->set_count; s++) {
119bf215546Sopenharmony_ci      if (layout->set_layouts[s] != NULL)
120bf215546Sopenharmony_ci         vk_descriptor_set_layout_unref(device, (void *)layout->set_layouts[s]);
121bf215546Sopenharmony_ci   }
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   vk_object_free(device, NULL, layout);
124bf215546Sopenharmony_ci}
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ciVKAPI_ATTR void VKAPI_CALL
127bf215546Sopenharmony_civk_common_DestroyPipelineLayout(VkDevice _device,
128bf215546Sopenharmony_ci                                VkPipelineLayout pipelineLayout,
129bf215546Sopenharmony_ci                                UNUSED const VkAllocationCallbacks *pAllocator)
130bf215546Sopenharmony_ci{
131bf215546Sopenharmony_ci   VK_FROM_HANDLE(vk_device, device, _device);
132bf215546Sopenharmony_ci   VK_FROM_HANDLE(vk_pipeline_layout, layout, pipelineLayout);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   if (layout == NULL)
135bf215546Sopenharmony_ci      return;
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   vk_pipeline_layout_unref(device, layout);
138bf215546Sopenharmony_ci}
139