yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
a70113c42
master
1// metal-fence.cpp 2#include "metal-fence.h" 3 4#include "metal-device.h" 5 6namespace gfx 7{ 8 9using namespace Slang ; 10 11namespace metal 12{ 13 14FenceImpl ::~FenceImpl () {} 15 16Result FenceImpl ::init (DeviceImpl * device ,const IFence ::Desc & desc ) 17{ 18m_device = device ; 19m_event = NS ::TransferPtr (m_device -> m_device -> newSharedEvent ()); 20if (!m_event ) 21 { 22return SLANG_FAIL ; 23 } 24m_event -> setSignaledValue (desc .initialValue ); 25 26m_eventListener = NS ::TransferPtr (MTL ::SharedEventListener ::alloc ()-> init ()); 27if (!m_eventListener ) 28 { 29return SLANG_FAIL ; 30 } 31 32return SLANG_OK ; 33} 34 35Result FenceImpl ::getCurrentValue (uint64_t * outValue ) 36{ 37* outValue = m_event -> signaledValue (); 38return SLANG_OK ; 39} 40 41Result FenceImpl ::setCurrentValue (uint64_t value ) 42{ 43m_event -> setSignaledValue (value ); 44return SLANG_OK ; 45} 46 47Result FenceImpl ::getSharedHandle (InteropHandle * outHandle ) 48{ 49return SLANG_E_NOT_AVAILABLE ; 50} 51 52Result FenceImpl ::getNativeHandle (InteropHandle * outNativeHandle ) 53{ 54outNativeHandle -> api = InteropHandleAPI ::Metal ; 55outNativeHandle -> handleValue = reinterpret_cast < intptr_t > (m_event .get ()); 56return SLANG_OK ; 57} 58 59bool FenceImpl ::waitForFence (uint64_t value ,uint64_t timeout ) 60{ 61// Create a semaphore to synchronize the notification 62dispatch_semaphore_t semaphore = dispatch_semaphore_create (0 ); 63 64// Create and store the notification block 65MTL ::SharedEventNotificationBlock block = ^( MTL ::SharedEvent * event, uint64_t eventValue) { 66dispatch_semaphore_signal (semaphore); 67}; 68 69// Set up notification handler 70m_event -> notifyListener (m_eventListener. get (), value, block); 71 72// Wait for the notification or timeout 73if (timeout & ( 1LLU << 63 )) 74{ 75timeout = DISPATCH_TIME_FOREVER ; 76} 77else 78{ 79timeout = dispatch_time ( DISPATCH_TIME_NOW , timeout); 80} 81 82long result = dispatch_semaphore_wait (semaphore, timeout); 83dispatch_release (semaphore); 84 85return result == 0 ; 86} 87 88} // namespace metal 89} // namespace gfx