Debian Codegraph @

src/kosmickrisp/vulkan/kk_device_memory.c

raw symbols SCIP
1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * Copyright 2025 LunarG, Inc.
4 * Copyright 2025 Google LLC
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "kk_device_memory.h"
9
10 #include "kk_device.h"
11 #include "kk_entrypoints.h"
12 #include "kk_physical_device.h"
13
14 #include "kosmickrisp/bridge/mtl_bridge.h"
15
16 #include "vulkan/vulkan_metal.h"
17
18 #include "util/u_atomic.h"
19 #include "util/u_memory.h"
20
21 #include <inttypes.h>
22 #include <sys/mman.h>
23
24 /* Supports mtlheap only */
25 const VkExternalMemoryProperties kk_mtlheap_mem_props = {
26 .externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
27 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT,
28 .exportFromImportedHandleTypes =
29 VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT,
30 .compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT,
31 };
32
33 #ifdef VK_USE_PLATFORM_METAL_EXT
34 VKAPI_ATTR VkResult VKAPI_CALL
35 kk_GetMemoryMetalHandlePropertiesEXT(
36 VkDevice device, VkExternalMemoryHandleTypeFlagBits handleType,
37 const void *pHandle,
38 VkMemoryMetalHandlePropertiesEXT *pMemoryMetalHandleProperties)
39 {
40 VK_FROM_HANDLE(kk_device, dev, device);
41 struct kk_physical_device *pdev = kk_device_physical(dev);
42
43 /* We only support heaps since that's the backing for all our memory and
44 * simplifies implementation */
45 switch (handleType) {
46 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT:
47 break;
48 default:
49 return vk_error(dev, VK_ERROR_INVALID_EXTERNAL_HANDLE);
50 }
51 pMemoryMetalHandleProperties->memoryTypeBits =
52 BITFIELD_MASK(pdev->mem_type_count);
53
54 return VK_SUCCESS;
55 }
56 #endif /* VK_USE_PLATFORM_METAL_EXT */
57
58 VKAPI_ATTR VkResult VKAPI_CALL
59 kk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
60 const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMem)
61 {
62 VK_FROM_HANDLE(kk_device, dev, device);
63 struct kk_physical_device *pdev = kk_device_physical(dev);
64 struct kk_device_memory *mem;
65 VkResult result = VK_SUCCESS;
66 const VkImportMemoryMetalHandleInfoEXT *metal_info = vk_find_struct_const(
67 pAllocateInfo->pNext, IMPORT_MEMORY_METAL_HANDLE_INFO_EXT);
68 const VkMemoryType *type = &pdev->mem_types[pAllocateInfo->memoryTypeIndex];
69
70 // TODO_KOSMICKRISP Do the actual memory allocation with alignment requirements
71 uint32_t alignment = (1ULL << 12);
72
73 const uint64_t aligned_size =
74 align64(pAllocateInfo->allocationSize, alignment);
75
76 mem = vk_device_memory_create(&dev->vk, pAllocateInfo, pAllocator,
77 sizeof(*mem));
78 if (!mem)
79 return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
80
81 if (metal_info && metal_info->handleType) {
82 /* We only support heaps since that's the backing for all our memory and
83 * simplifies implementation */
84 assert(metal_info->handleType ==
85 VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT);
86 mem->bo = CALLOC_STRUCT(kk_bo);
87 if (!mem->bo) {
88 result = vk_errorf(&dev->vk.base, VK_ERROR_OUT_OF_DEVICE_MEMORY, "%m");
89 goto fail_alloc;
90 }
91 mem->bo->mtl_handle = mtl_retain(metal_info->handle);
92 mem->bo->map =
93 mtl_new_buffer_with_length(mem->bo->mtl_handle, mem->vk.size, 0u);
94 mem->bo->gpu = mtl_buffer_get_gpu_address(mem->bo->map);
95 mem->bo->cpu = mtl_get_contents(mem->bo->map);
96 mem->bo->size_B = mtl_heap_get_size(mem->bo->mtl_handle);
97 kk_device_add_heap_to_residency_set(dev, mem->bo->mtl_handle);
98 } else {
99 result =
100 kk_alloc_bo(dev, &dev->vk.base, aligned_size, alignment, &mem->bo);
101 if (result != VK_SUCCESS)
102 goto fail_alloc;
103 }
104
105 struct kk_memory_heap *heap = &pdev->mem_heaps[type->heapIndex];
106 p_atomic_add(&heap->used, mem->bo->size_B);
107
108 *pMem = kk_device_memory_to_handle(mem);
109
110 return VK_SUCCESS;
111
112 fail_alloc:
113 vk_device_memory_destroy(&dev->vk, pAllocator, &mem->vk);
114 return result;
115 }
116
117 VKAPI_ATTR void VKAPI_CALL
118 kk_FreeMemory(VkDevice device, VkDeviceMemory _mem,
119 const VkAllocationCallbacks *pAllocator)
120 {
121 VK_FROM_HANDLE(kk_device, dev, device);
122 VK_FROM_HANDLE(kk_device_memory, mem, _mem);
123 struct kk_physical_device *pdev = kk_device_physical(dev);
124
125 if (!mem)
126 return;
127
128 const VkMemoryType *type = &pdev->mem_types[mem->vk.memory_type_index];
129 struct kk_memory_heap *heap = &pdev->mem_heaps[type->heapIndex];
130 p_atomic_add(&heap->used, -((int64_t)mem->bo->size_B));
131
132 kk_destroy_bo(dev, mem->bo);
133
134 vk_device_memory_destroy(&dev->vk, pAllocator, &mem->vk);
135 }
136
137 VKAPI_ATTR VkResult VKAPI_CALL
138 kk_MapMemory2KHR(VkDevice device, const VkMemoryMapInfoKHR *pMemoryMapInfo,
139 void **ppData)
140 {
141 VK_FROM_HANDLE(kk_device, dev, device);
142 VK_FROM_HANDLE(kk_device_memory, mem, pMemoryMapInfo->memory);
143 VkResult result = VK_SUCCESS;
144
145 if (mem == NULL) {
146 *ppData = NULL;
147 return VK_SUCCESS;
148 }
149
150 const VkDeviceSize offset = pMemoryMapInfo->offset;
151 const VkDeviceSize size = vk_device_memory_range(
152 &mem->vk, pMemoryMapInfo->offset, pMemoryMapInfo->size);
153
154 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
155 *
156 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
157 * assert(size != 0);
158 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
159 * equal to the size of the memory minus offset
160 */
161 assert(size > 0);
162 assert(offset + size <= mem->bo->size_B);
163
164 if (size != (size_t)size) {
165 return vk_errorf(dev, VK_ERROR_MEMORY_MAP_FAILED,
166 "requested size 0x%" PRIx64 " does not fit in %u bits",
167 size, (unsigned)(sizeof(size_t) * 8));
168 }
169
170 /* From the Vulkan 1.2.194 spec:
171 *
172 * "memory must not be currently host mapped"
173 */
174 if (mem->map != NULL) {
175 return vk_errorf(dev, VK_ERROR_MEMORY_MAP_FAILED,
176 "Memory object already mapped.");
177 }
178
179 // TODO_KOSMICKRISP Use mmap here to so we can support VK_EXT_map_memory_placed
180 mem->map = mem->bo->cpu;
181
182 *ppData = mem->map + offset;
183
184 return result;
185 }
186
187 VKAPI_ATTR VkResult VKAPI_CALL
188 kk_UnmapMemory2KHR(VkDevice device,
189 const VkMemoryUnmapInfoKHR *pMemoryUnmapInfo)
190 {
191 VK_FROM_HANDLE(kk_device_memory, mem, pMemoryUnmapInfo->memory);
192
193 if (mem == NULL)
194 return VK_SUCCESS;
195
196 // TODO_KOSMICKRISP Use unmap here to so we can support
197 // VK_EXT_map_memory_placed
198 mem->map = NULL;
199
200 return VK_SUCCESS;
201 }
202
203 VKAPI_ATTR VkResult VKAPI_CALL
204 kk_FlushMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount,
205 const VkMappedMemoryRange *pMemoryRanges)
206 {
207 return VK_SUCCESS;
208 }
209
210 VKAPI_ATTR VkResult VKAPI_CALL
211 kk_InvalidateMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount,
212 const VkMappedMemoryRange *pMemoryRanges)
213 {
214 return VK_SUCCESS;
215 }
216
217 VKAPI_ATTR void VKAPI_CALL
218 kk_GetDeviceMemoryCommitment(VkDevice device, VkDeviceMemory _mem,
219 VkDeviceSize *pCommittedMemoryInBytes)
220 {
221 VK_FROM_HANDLE(kk_device_memory, mem, _mem);
222
223 *pCommittedMemoryInBytes = mem->bo->size_B;
224 }
225
226 #ifdef VK_USE_PLATFORM_METAL_EXT
227 VKAPI_ATTR VkResult VKAPI_CALL
228 kk_GetMemoryMetalHandleEXT(
229 VkDevice device, const VkMemoryGetMetalHandleInfoEXT *pGetMetalHandleInfo,
230 void **pHandle)
231 {
232 /* We only support heaps since that's the backing for all our memory and
233 * simplifies implementation */
234 assert(pGetMetalHandleInfo->handleType ==
235 VK_EXTERNAL_MEMORY_HANDLE_TYPE_MTLHEAP_BIT_EXT);
236 VK_FROM_HANDLE(kk_device_memory, mem, pGetMetalHandleInfo->memory);
237
238 /* From the Vulkan spec of vkGetMemoryMetalHandleEXT:
239 *
240 * "Unless the app retains the handle object returned by the call,
241 * the lifespan will be the same as the associated VkDeviceMemory"
242 */
243 *pHandle = mem->bo->mtl_handle;
244 return VK_SUCCESS;
245 }
246 #endif /* VK_USE_PLATFORM_METAL_EXT */
247
248 VKAPI_ATTR uint64_t VKAPI_CALL
249 kk_GetDeviceMemoryOpaqueCaptureAddress(
250 UNUSED VkDevice device, const VkDeviceMemoryOpaqueCaptureAddressInfo *pInfo)
251 {
252 VK_FROM_HANDLE(kk_device_memory, mem, pInfo->memory);
253
254 return mem->bo->gpu;
255 }