Debian Codegraph @

src/kosmickrisp/vulkan/kk_sync.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_sync.h"
8
9 #include "kk_device.h"
10
11 #include "kosmickrisp/bridge/mtl_bridge.h"
12
13 static VkResult
14 kk_timeline_init(struct vk_device *device, struct vk_sync *sync,
15 uint64_t initial_value)
16 {
17 struct kk_sync_timeline *timeline =
18 container_of(sync, struct kk_sync_timeline, base);
19
20 struct kk_device *dev = container_of(device, struct kk_device, vk);
21 timeline->mtl_handle = mtl_new_shared_event(dev->mtl_handle);
22 mtl_shared_event_set_signaled_value(timeline->mtl_handle, initial_value);
23
24 return VK_SUCCESS;
25 }
26
27 static void
28 kk_timeline_finish(struct vk_device *device, struct vk_sync *sync)
29 {
30 struct kk_sync_timeline *timeline =
31 container_of(sync, struct kk_sync_timeline, base);
32 mtl_release(timeline->mtl_handle);
33 }
34
35 static VkResult
36 kk_timeline_signal(struct vk_device *device, struct vk_sync *sync,
37 uint64_t value)
38 {
39 struct kk_sync_timeline *timeline =
40 container_of(sync, struct kk_sync_timeline, base);
41 mtl_shared_event_set_signaled_value(timeline->mtl_handle, value);
42 return VK_SUCCESS;
43 }
44
45 static VkResult
46 kk_timeline_get_value(struct vk_device *device, struct vk_sync *sync,
47 uint64_t *value)
48 {
49 struct kk_sync_timeline *timeline =
50 container_of(sync, struct kk_sync_timeline, base);
51 *value = mtl_shared_event_get_signaled_value(timeline->mtl_handle);
52 return VK_SUCCESS;
53 }
54
55 static VkResult
56 kk_timeline_wait(struct vk_device *device, struct vk_sync *sync,
57 uint64_t wait_value, enum vk_sync_wait_flags wait_flags,
58 uint64_t abs_timeout_ns)
59 {
60 struct kk_sync_timeline *timeline =
61 container_of(sync, struct kk_sync_timeline, base);
62
63 /* abs_timeout_ns is the point in time when we should stop waiting, not the
64 * absolute time in ns. Therefore, we need to compute the delta from now to
65 * when we should stop waiting and convert to ms for Metal to be happy
66 * (Similar to what dzn does).
67 */
68 uint64_t timeout_ms = 0u;
69 if (abs_timeout_ns == OS_TIMEOUT_INFINITE) {
70 timeout_ms = OS_TIMEOUT_INFINITE;
71 } else {
72 uint64_t cur_time = os_time_get_nano();
73 uint64_t rel_timeout_ns =
74 abs_timeout_ns > cur_time ? abs_timeout_ns - cur_time : 0;
75
76 timeout_ms =
77 (rel_timeout_ns / 1000000) + (rel_timeout_ns % 1000000 ? 1 : 0);
78 }
79 int completed = mtl_shared_event_wait_until_signaled_value(
80 timeline->mtl_handle, wait_value, timeout_ms);
81
82 return completed != 0 ? VK_SUCCESS : VK_TIMEOUT;
83 }
84
85 const struct vk_sync_type kk_sync_type = {
86 .size = sizeof(struct kk_sync_timeline),
87 .features = VK_SYNC_FEATURE_TIMELINE | VK_SYNC_FEATURE_GPU_WAIT |
88 VK_SYNC_FEATURE_CPU_WAIT | VK_SYNC_FEATURE_CPU_SIGNAL |
89 VK_SYNC_FEATURE_WAIT_PENDING |
90 VK_SYNC_FEATURE_WAIT_BEFORE_SIGNAL,
91 .init = kk_timeline_init,
92 .finish = kk_timeline_finish,
93 .signal = kk_timeline_signal,
94 .get_value = kk_timeline_get_value,
95 .reset = NULL,
96 .move = NULL,
97 .wait = kk_timeline_wait,
98 .wait_many = NULL,
99 .import_opaque_fd = NULL,
100 .export_opaque_fd = NULL,
101 .import_sync_file = NULL,
102 .export_sync_file = NULL,
103 .import_win32_handle = NULL,
104 .export_win32_handle = NULL,
105 .set_win32_export_params = NULL,
106 };