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_descriptor_set_layout.h"
25 
26 #include "vk_alloc.h"
27 #include "vk_common_entrypoints.h"
28 #include "vk_device.h"
29 
30 static void
vk_descriptor_set_layout_init(struct vk_device * device,struct vk_descriptor_set_layout * layout)31 vk_descriptor_set_layout_init(struct vk_device *device,
32                               struct vk_descriptor_set_layout *layout)
33 {
34    vk_object_base_init(device, &layout->base,
35                        VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT);
36 
37    layout->ref_cnt = 1;
38    layout->destroy = vk_descriptor_set_layout_destroy;
39 }
40 
41 void *
vk_descriptor_set_layout_zalloc(struct vk_device * device,size_t size)42 vk_descriptor_set_layout_zalloc(struct vk_device *device, size_t size)
43 {
44    /* Because we're reference counting and lifetimes may not be what the
45     * client expects, these have to be allocated off the device and not as
46     * their own object.
47     */
48    struct vk_descriptor_set_layout *layout =
49       vk_zalloc(&device->alloc, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
50    if (!layout)
51       return NULL;
52 
53    vk_descriptor_set_layout_init(device, layout);
54 
55    return layout;
56 }
57 
58 void *
vk_descriptor_set_layout_multizalloc(struct vk_device * device,struct vk_multialloc * ma)59 vk_descriptor_set_layout_multizalloc(struct vk_device *device,
60                                      struct vk_multialloc *ma)
61 {
62    /* Because we're reference counting and lifetimes may not be what the
63     * client expects, these have to be allocated off the device and not as
64     * their own object.
65     */
66    struct vk_descriptor_set_layout *layout =
67       vk_multialloc_zalloc(ma, &device->alloc,
68                            VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
69    if (!layout)
70       return NULL;
71 
72    vk_descriptor_set_layout_init(device, layout);
73 
74    return layout;
75 }
76 
77 void
vk_descriptor_set_layout_destroy(struct vk_device * device,struct vk_descriptor_set_layout * layout)78 vk_descriptor_set_layout_destroy(struct vk_device *device,
79                                  struct vk_descriptor_set_layout *layout)
80 {
81    vk_object_free(device, NULL, layout);
82 }
83 
84 VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyDescriptorSetLayout(VkDevice _device,VkDescriptorSetLayout descriptorSetLayout,UNUSED const VkAllocationCallbacks * pAllocator)85 vk_common_DestroyDescriptorSetLayout(VkDevice _device,
86                                      VkDescriptorSetLayout descriptorSetLayout,
87                                      UNUSED const VkAllocationCallbacks *pAllocator)
88 {
89    VK_FROM_HANDLE(vk_device, device, _device);
90    VK_FROM_HANDLE(vk_descriptor_set_layout, layout, descriptorSetLayout);
91 
92    if (layout == NULL)
93       return;
94 
95    vk_descriptor_set_layout_unref(device, layout);
96 }
97