Debian Codegraph @

src/kosmickrisp/vulkan/kk_bo.c

raw symbols SCIP
1 /*
2 * Copyright 2025 LunarG, Inc.
3 * Copyright 2025 Google LLC
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "kk_bo.h"
8
9 #include "kk_device.h"
10
11 #include "kosmickrisp/bridge/mtl_bridge.h"
12
13 #include "util/u_memory.h"
14
15 VkResult
16 kk_alloc_bo(struct kk_device *dev, struct vk_object_base *log_obj,
17 uint64_t size_B, uint64_t align_B, struct kk_bo **bo_out)
18 {
19 VkResult result = VK_SUCCESS;
20
21 // TODO_KOSMICKRISP: Probably requires handling the buffer maximum 256MB
22 uint64_t minimum_alignment = 0u;
23 mtl_heap_buffer_size_and_align_with_length(dev->mtl_handle, &size_B,
24 &minimum_alignment);
25 minimum_alignment = MAX2(minimum_alignment, align_B);
26 size_B = align64(size_B, minimum_alignment);
27 mtl_heap *handle =
28 mtl_new_heap(dev->mtl_handle, size_B, KK_MTL_RESOURCE_OPTIONS);
29 if (handle == NULL) {
30 result = vk_errorf(log_obj, VK_ERROR_OUT_OF_DEVICE_MEMORY, "%m");
31 goto fail_heap;
32 }
33
34 mtl_buffer *map = mtl_new_buffer_with_length(handle, size_B, 0u);
35 if (map == NULL) {
36 result = vk_errorf(log_obj, VK_ERROR_OUT_OF_DEVICE_MEMORY, "%m");
37 goto fail_map;
38 }
39
40 struct kk_bo *bo = CALLOC_STRUCT(kk_bo);
41
42 if (bo == NULL) {
43 result = vk_errorf(log_obj, VK_ERROR_OUT_OF_HOST_MEMORY, "%m");
44 goto fail_alloc;
45 }
46
47 bo->mtl_handle = handle;
48 bo->size_B = size_B;
49 bo->map = map;
50 bo->gpu = mtl_buffer_get_gpu_address(map);
51 bo->cpu = mtl_get_contents(map);
52
53 kk_device_add_heap_to_residency_set(dev, handle);
54
55 *bo_out = bo;
56 return result;
57
58 fail_alloc:
59 mtl_release(map);
60 fail_map:
61 mtl_release(handle);
62 fail_heap:
63 return result;
64 }
65
66 void
67 kk_destroy_bo(struct kk_device *dev, struct kk_bo *bo)
68 {
69 kk_device_remove_heap_from_residency_set(dev, bo->mtl_handle);
70 mtl_release(bo->map);
71 mtl_release(bo->mtl_handle);
72 FREE(bo);
73 }