1 /*
2 * Copyright © 2017 Intel Corporation
3 * Copyright © 2022 Collabora, Ltd
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "vk_descriptor_update_template.h"
26
27 #include "vk_alloc.h"
28 #include "vk_common_entrypoints.h"
29 #include "vk_device.h"
30 #include "vk_log.h"
31
32 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_CreateDescriptorUpdateTemplate(VkDevice _device,const VkDescriptorUpdateTemplateCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDescriptorUpdateTemplate * pDescriptorUpdateTemplate)33 vk_common_CreateDescriptorUpdateTemplate(VkDevice _device,
34 const VkDescriptorUpdateTemplateCreateInfo *pCreateInfo,
35 const VkAllocationCallbacks *pAllocator,
36 VkDescriptorUpdateTemplate *pDescriptorUpdateTemplate)
37 {
38 VK_FROM_HANDLE(vk_device, device, _device);
39 struct vk_descriptor_update_template *template;
40
41 uint32_t entry_count = 0;
42 for (uint32_t i = 0; i < pCreateInfo->descriptorUpdateEntryCount; i++) {
43 if (pCreateInfo->pDescriptorUpdateEntries[i].descriptorCount > 0)
44 entry_count++;
45 }
46
47 size_t size = sizeof(*template) + entry_count * sizeof(template->entries[0]);
48
49 /* Because we're reference counting and lifetimes may not be what the
50 * client expects, these have to be allocated off the device and not as
51 * their own object.
52 */
53 template = vk_zalloc(&device->alloc, size, 8,
54 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
55 if (template == NULL)
56 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
57
58 vk_object_base_init(device, &template->base,
59 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE);
60
61 template->type = pCreateInfo->templateType;
62 template->bind_point = pCreateInfo->pipelineBindPoint;
63 template->ref_cnt = 1;
64
65 if (template->type == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
66 template->set = pCreateInfo->set;
67
68 uint32_t entry_idx = 0;
69 template->entry_count = entry_count;
70 for (uint32_t i = 0; i < pCreateInfo->descriptorUpdateEntryCount; i++) {
71 const VkDescriptorUpdateTemplateEntry *pEntry =
72 &pCreateInfo->pDescriptorUpdateEntries[i];
73
74 if (pEntry->descriptorCount == 0)
75 continue;
76
77 template->entries[entry_idx++] = (struct vk_descriptor_template_entry) {
78 .type = pEntry->descriptorType,
79 .binding = pEntry->dstBinding,
80 .array_element = pEntry->dstArrayElement,
81 .array_count = pEntry->descriptorCount,
82 .offset = pEntry->offset,
83 .stride = pEntry->stride,
84 };
85 }
86 assert(entry_idx == entry_count);
87
88 *pDescriptorUpdateTemplate =
89 vk_descriptor_update_template_to_handle(template);
90
91 return VK_SUCCESS;
92 }
93
94 VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyDescriptorUpdateTemplate(VkDevice _device,VkDescriptorUpdateTemplate descriptorUpdateTemplate,const VkAllocationCallbacks * pAllocator)95 vk_common_DestroyDescriptorUpdateTemplate(VkDevice _device,
96 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
97 const VkAllocationCallbacks *pAllocator)
98 {
99 VK_FROM_HANDLE(vk_device, device, _device);
100 VK_FROM_HANDLE(vk_descriptor_update_template, template,
101 descriptorUpdateTemplate);
102
103 if (!template)
104 return;
105
106 vk_descriptor_update_template_unref(device, template);
107 }
108