redo semaphore impl on metal
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user