• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_buffer.h"
25 
26 #include "vk_common_entrypoints.h"
27 #include "vk_alloc.h"
28 #include "vk_device.h"
29 #include "vk_util.h"
30 
31 void
vk_buffer_init(struct vk_device * device,struct vk_buffer * buffer,const VkBufferCreateInfo * pCreateInfo)32 vk_buffer_init(struct vk_device *device,
33                struct vk_buffer *buffer,
34                const VkBufferCreateInfo *pCreateInfo)
35 {
36    vk_object_base_init(device, &buffer->base, VK_OBJECT_TYPE_BUFFER);
37 
38    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
39    assert(pCreateInfo->size > 0);
40 
41    buffer->create_flags = pCreateInfo->flags;
42    buffer->size = pCreateInfo->size;
43    buffer->usage = pCreateInfo->usage;
44 
45    const VkBufferUsageFlags2CreateInfoKHR *usage2_info =
46       vk_find_struct_const(pCreateInfo->pNext,
47                            BUFFER_USAGE_FLAGS_2_CREATE_INFO_KHR);
48    if (usage2_info != NULL)
49       buffer->usage = usage2_info->usage;
50 }
51 
52 void *
vk_buffer_create(struct vk_device * device,const VkBufferCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc,size_t size)53 vk_buffer_create(struct vk_device *device,
54                  const VkBufferCreateInfo *pCreateInfo,
55                  const VkAllocationCallbacks *alloc,
56                  size_t size)
57 {
58    struct vk_buffer *buffer =
59       vk_zalloc2(&device->alloc, alloc, size, 8,
60                  VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
61    if (buffer == NULL)
62       return NULL;
63 
64    vk_buffer_init(device, buffer, pCreateInfo);
65 
66    return buffer;
67 }
68 
69 void
vk_buffer_finish(struct vk_buffer * buffer)70 vk_buffer_finish(struct vk_buffer *buffer)
71 {
72    vk_object_base_finish(&buffer->base);
73 }
74 
75 void
vk_buffer_destroy(struct vk_device * device,const VkAllocationCallbacks * alloc,struct vk_buffer * buffer)76 vk_buffer_destroy(struct vk_device *device,
77                   const VkAllocationCallbacks *alloc,
78                   struct vk_buffer *buffer)
79 {
80    vk_object_free(device, alloc, buffer);
81 }
82 
83 VKAPI_ATTR void VKAPI_CALL
vk_common_GetBufferMemoryRequirements(VkDevice _device,VkBuffer buffer,VkMemoryRequirements * pMemoryRequirements)84 vk_common_GetBufferMemoryRequirements(VkDevice _device,
85                                       VkBuffer buffer,
86                                       VkMemoryRequirements *pMemoryRequirements)
87 {
88    VK_FROM_HANDLE(vk_device, device, _device);
89 
90    VkBufferMemoryRequirementsInfo2 info = {
91       .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
92       .buffer = buffer,
93    };
94    VkMemoryRequirements2 reqs = {
95       .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
96    };
97    device->dispatch_table.GetBufferMemoryRequirements2(_device, &info, &reqs);
98 
99    *pMemoryRequirements = reqs.memoryRequirements;
100 }
101 
102 VKAPI_ATTR void VKAPI_CALL
vk_common_GetBufferMemoryRequirements2(VkDevice _device,const VkBufferMemoryRequirementsInfo2 * pInfo,VkMemoryRequirements2 * pMemoryRequirements)103 vk_common_GetBufferMemoryRequirements2(VkDevice _device,
104                                        const VkBufferMemoryRequirementsInfo2 *pInfo,
105                                        VkMemoryRequirements2 *pMemoryRequirements)
106 {
107    VK_FROM_HANDLE(vk_device, device, _device);
108    VK_FROM_HANDLE(vk_buffer, buffer, pInfo->buffer);
109 
110    VkBufferCreateInfo pCreateInfo = {
111       .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
112       .pNext = NULL,
113       .usage = buffer->usage,
114       .size = buffer->size,
115       .flags = buffer->create_flags,
116       .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
117       .queueFamilyIndexCount = 0,
118       .pQueueFamilyIndices = NULL,
119    };
120    VkDeviceBufferMemoryRequirements info = {
121       .sType = VK_STRUCTURE_TYPE_DEVICE_BUFFER_MEMORY_REQUIREMENTS,
122       .pNext = NULL,
123       .pCreateInfo = &pCreateInfo,
124    };
125 
126    device->dispatch_table.GetDeviceBufferMemoryRequirements(_device, &info, pMemoryRequirements);
127 }
128 
129 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_BindBufferMemory(VkDevice _device,VkBuffer buffer,VkDeviceMemory memory,VkDeviceSize memoryOffset)130 vk_common_BindBufferMemory(VkDevice _device,
131                            VkBuffer buffer,
132                            VkDeviceMemory memory,
133                            VkDeviceSize memoryOffset)
134 {
135    VK_FROM_HANDLE(vk_device, device, _device);
136 
137    VkBindBufferMemoryInfo bind = {
138       .sType         = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
139       .buffer        = buffer,
140       .memory        = memory,
141       .memoryOffset  = memoryOffset,
142    };
143 
144    return device->dispatch_table.BindBufferMemory2(_device, 1, &bind);
145 }
146