kronos_compute/implementation/
sync.rs1use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6use std::ptr;
7use std::sync::atomic::{AtomicU64, Ordering};
8use std::sync::Mutex;
9use std::collections::HashMap;
10
11static FENCE_COUNTER: AtomicU64 = AtomicU64::new(1);
13static SEMAPHORE_COUNTER: AtomicU64 = AtomicU64::new(1);
14static EVENT_COUNTER: AtomicU64 = AtomicU64::new(1);
15
16lazy_static::lazy_static! {
18 static ref FENCES: Mutex<HashMap<u64, FenceData>> = Mutex::new(HashMap::new());
19 static ref SEMAPHORES: Mutex<HashMap<u64, SemaphoreData>> = Mutex::new(HashMap::new());
20 static ref EVENTS: Mutex<HashMap<u64, EventData>> = Mutex::new(HashMap::new());
21}
22
23struct FenceData {
24 device: VkDevice,
25 signaled: bool,
26}
27
28struct SemaphoreData {
29 device: VkDevice,
30 signaled: bool,
31}
32
33struct EventData {
34 device: VkDevice,
35 signaled: bool,
36}
37
38#[no_mangle]
40pub unsafe extern "C" fn vkCreateFence(
41 device: VkDevice,
42 pCreateInfo: *const VkFenceCreateInfo,
43 _pAllocator: *const VkAllocationCallbacks,
44 pFence: *mut VkFence,
45) -> VkResult {
46 if device.is_null() || pCreateInfo.is_null() || pFence.is_null() {
47 return VkResult::ErrorInitializationFailed;
48 }
49
50 let create_info = &*pCreateInfo;
51 let handle = FENCE_COUNTER.fetch_add(1, Ordering::SeqCst);
52
53 let fence_data = FenceData {
54 device,
55 signaled: create_info.flags.contains(VkFenceCreateFlags::SIGNALED),
56 };
57
58 FENCES.lock().unwrap().insert(handle, fence_data);
59 *pFence = VkFence::from_raw(handle);
60
61 log::info!("Created fence {:?}", handle);
62 VkResult::Success
63}
64
65#[no_mangle]
67pub unsafe extern "C" fn vkDestroyFence(
68 device: VkDevice,
69 fence: VkFence,
70 _pAllocator: *const VkAllocationCallbacks,
71) {
72 if device.is_null() || fence.is_null() {
73 return;
74 }
75
76 let handle = fence.as_raw();
77 FENCES.lock().unwrap().remove(&handle);
78 log::info!("Destroyed fence {:?}", handle);
79}
80
81#[no_mangle]
83pub unsafe extern "C" fn vkWaitForFences(
84 device: VkDevice,
85 fenceCount: u32,
86 pFences: *const VkFence,
87 waitAll: VkBool32,
88 timeout: u64,
89) -> VkResult {
90 if device.is_null() || pFences.is_null() || fenceCount == 0 {
91 return VkResult::ErrorInitializationFailed;
92 }
93
94 VkResult::Success
97}
98
99#[no_mangle]
101pub unsafe extern "C" fn vkResetFences(
102 device: VkDevice,
103 fenceCount: u32,
104 pFences: *const VkFence,
105) -> VkResult {
106 if device.is_null() || pFences.is_null() || fenceCount == 0 {
107 return VkResult::ErrorInitializationFailed;
108 }
109
110 let mut fences = FENCES.lock().unwrap();
111 for i in 0..fenceCount {
112 let fence = *pFences.add(i as usize);
113 if let Some(fence_data) = fences.get_mut(&fence.as_raw()) {
114 fence_data.signaled = false;
115 }
116 }
117
118 VkResult::Success
119}
120
121#[no_mangle]
123pub unsafe extern "C" fn vkGetFenceStatus(
124 device: VkDevice,
125 fence: VkFence,
126) -> VkResult {
127 if device.is_null() || fence.is_null() {
128 return VkResult::ErrorDeviceLost;
129 }
130
131 let fences = FENCES.lock().unwrap();
132 if let Some(fence_data) = fences.get(&fence.as_raw()) {
133 if fence_data.signaled {
134 VkResult::Success
135 } else {
136 VkResult::NotReady
137 }
138 } else {
139 VkResult::ErrorDeviceLost
140 }
141}
142
143#[no_mangle]
145pub unsafe extern "C" fn vkCreateSemaphore(
146 device: VkDevice,
147 pCreateInfo: *const VkSemaphoreCreateInfo,
148 _pAllocator: *const VkAllocationCallbacks,
149 pSemaphore: *mut VkSemaphore,
150) -> VkResult {
151 if device.is_null() || pCreateInfo.is_null() || pSemaphore.is_null() {
152 return VkResult::ErrorInitializationFailed;
153 }
154
155 let handle = SEMAPHORE_COUNTER.fetch_add(1, Ordering::SeqCst);
156
157 let semaphore_data = SemaphoreData {
158 device,
159 signaled: false,
160 };
161
162 SEMAPHORES.lock().unwrap().insert(handle, semaphore_data);
163 *pSemaphore = VkSemaphore::from_raw(handle);
164
165 log::info!("Created semaphore {:?}", handle);
166 VkResult::Success
167}
168
169#[no_mangle]
171pub unsafe extern "C" fn vkDestroySemaphore(
172 device: VkDevice,
173 semaphore: VkSemaphore,
174 _pAllocator: *const VkAllocationCallbacks,
175) {
176 if device.is_null() || semaphore.is_null() {
177 return;
178 }
179
180 let handle = semaphore.as_raw();
181 SEMAPHORES.lock().unwrap().remove(&handle);
182 log::info!("Destroyed semaphore {:?}", handle);
183}
184
185#[no_mangle]
187pub unsafe extern "C" fn vkCreateEvent(
188 device: VkDevice,
189 pCreateInfo: *const VkEventCreateInfo,
190 _pAllocator: *const VkAllocationCallbacks,
191 pEvent: *mut VkEvent,
192) -> VkResult {
193 if device.is_null() || pCreateInfo.is_null() || pEvent.is_null() {
194 return VkResult::ErrorInitializationFailed;
195 }
196
197 let handle = EVENT_COUNTER.fetch_add(1, Ordering::SeqCst);
198
199 let event_data = EventData {
200 device,
201 signaled: false,
202 };
203
204 EVENTS.lock().unwrap().insert(handle, event_data);
205 *pEvent = VkEvent::from_raw(handle);
206
207 log::info!("Created event {:?}", handle);
208 VkResult::Success
209}
210
211#[no_mangle]
213pub unsafe extern "C" fn vkDestroyEvent(
214 device: VkDevice,
215 event: VkEvent,
216 _pAllocator: *const VkAllocationCallbacks,
217) {
218 if device.is_null() || event.is_null() {
219 return;
220 }
221
222 let handle = event.as_raw();
223 EVENTS.lock().unwrap().remove(&handle);
224 log::info!("Destroyed event {:?}", handle);
225}
226
227#[no_mangle]
229pub unsafe extern "C" fn vkGetEventStatus(
230 device: VkDevice,
231 event: VkEvent,
232) -> VkResult {
233 if device.is_null() || event.is_null() {
234 return VkResult::ErrorDeviceLost;
235 }
236
237 let events = EVENTS.lock().unwrap();
238 if let Some(event_data) = events.get(&event.as_raw()) {
239 if event_data.signaled {
240 VkResult::EventSet
241 } else {
242 VkResult::EventReset
243 }
244 } else {
245 VkResult::ErrorDeviceLost
246 }
247}
248
249#[no_mangle]
251pub unsafe extern "C" fn vkSetEvent(
252 device: VkDevice,
253 event: VkEvent,
254) -> VkResult {
255 if device.is_null() || event.is_null() {
256 return VkResult::ErrorDeviceLost;
257 }
258
259 let mut events = EVENTS.lock().unwrap();
260 if let Some(event_data) = events.get_mut(&event.as_raw()) {
261 event_data.signaled = true;
262 VkResult::Success
263 } else {
264 VkResult::ErrorDeviceLost
265 }
266}
267
268#[no_mangle]
270pub unsafe extern "C" fn vkResetEvent(
271 device: VkDevice,
272 event: VkEvent,
273) -> VkResult {
274 if device.is_null() || event.is_null() {
275 return VkResult::ErrorDeviceLost;
276 }
277
278 let mut events = EVENTS.lock().unwrap();
279 if let Some(event_data) = events.get_mut(&event.as_raw()) {
280 event_data.signaled = false;
281 VkResult::Success
282 } else {
283 VkResult::ErrorDeviceLost
284 }
285}
286
287#[no_mangle]
289pub unsafe extern "C" fn vkQueueSubmit(
290 queue: VkQueue,
291 submitCount: u32,
292 pSubmits: *const VkSubmitInfo,
293 fence: VkFence,
294) -> VkResult {
295 if queue.is_null() || (submitCount > 0 && pSubmits.is_null()) {
296 return VkResult::ErrorInitializationFailed;
297 }
298
299 if !fence.is_null() {
304 if let Some(fence_data) = FENCES.lock().unwrap().get_mut(&fence.as_raw()) {
305 fence_data.signaled = true;
306 }
307 }
308
309 log::info!("Submitted {} batches to queue {:?}", submitCount, queue.as_raw());
310 VkResult::Success
311}
312
313#[no_mangle]
315pub unsafe extern "C" fn vkQueueWaitIdle(
316 queue: VkQueue,
317) -> VkResult {
318 if queue.is_null() {
319 return VkResult::ErrorInitializationFailed;
320 }
321
322 log::info!("Queue {:?} idle", queue.as_raw());
324 VkResult::Success
325}
326
327#[no_mangle]
329pub unsafe extern "C" fn vkDeviceWaitIdle(
330 device: VkDevice,
331) -> VkResult {
332 if device.is_null() {
333 return VkResult::ErrorInitializationFailed;
334 }
335
336 log::info!("Device {:?} idle", device.as_raw());
338 VkResult::Success
339}