redo semaphore impl on metal

This commit is contained in:
Greg Wells
2025-07-11 17:08:28 -04:00
parent 2def510f69
commit 8445c0553b
5 changed files with 71 additions and 57 deletions

View File

@@ -1,11 +1,17 @@
#pragma once
#include "synchronization/semaphore/gryphn_semaphore.h"
#import <Metal/MTLEvent.h>
#import <Metal/MTLCommandBuffer.h>
#import <stdatomic.h>
typedef struct gnPlatformSemaphore_t {
id<MTLEvent> event;
bool eventTriggered;
atomic_uint_fast64_t currentValue;
} gnPlatformSemaphore;
gnReturnCode createMetalSemaphore(gnSemaphore semaphore, gnOutputDevice device);
void destroyMetalSemaphore(gnSemaphore semaphore);
void mtlSignalSemaphore(gnSemaphore semaphore, id<MTLCommandBuffer> cmdBuf);
void mtlWaitSemaphore(gnSemaphore semaphore, id<MTLCommandBuffer> cmdBuf);

View File

@@ -4,10 +4,20 @@
gnReturnCode createMetalSemaphore(gnSemaphore semaphore, gnOutputDevice device) {
semaphore->semaphore = malloc(sizeof(gnPlatformSemaphore));
semaphore->semaphore->event = [device->outputDevice->device newEvent];
semaphore->semaphore->currentValue = 0;
return GN_SUCCESS;
}
void destroyMetalSemaphore(gnSemaphore semaphore) {
[semaphore->semaphore->event release];
free(semaphore->semaphore);
}
void mtlSignalSemaphore(gnSemaphore semaphore, id<MTLCommandBuffer> cmdBuf) {
uint64_t val = atomic_fetch_add_explicit(&semaphore->semaphore->currentValue, 1, memory_order_acq_rel) + 1;
[cmdBuf encodeSignalEvent:semaphore->semaphore->event value:val];
}
void mtlWaitSemaphore(gnSemaphore semaphore, id<MTLCommandBuffer> cmdBuf) {
uint64_t val = atomic_load_explicit(&semaphore->semaphore->currentValue, memory_order_acquire);
[cmdBuf encodeWaitForEvent:semaphore->semaphore->event value:val];
}