module Graphics.Caramia.Sync
(
fence
, waitFence
, isFenceSignalled
, Fence() )
where
import Graphics.Caramia.Internal.OpenGLCApi
import Graphics.Caramia.Prelude
import Graphics.Caramia.Resource
import Control.Exception
newtype Fence = Fence (Resource GLsync)
deriving ( Eq, Typeable )
fence :: IO Fence
fence = mask_ $ do
resource <-
newResource createFence
glDeleteSync
(return ())
return $ Fence resource
where
createFence = glFenceSync gl_SYNC_GPU_COMMANDS_COMPLETE 0
waitFence :: Int
-> Fence
-> IO Bool
waitFence useconds (Fence resource) =
withResource resource $ \fencesync -> do
ret <- glClientWaitSync fencesync gl_SYNC_FLUSH_COMMANDS_BIT
(fromIntegral actual_seconds)
if | ret == gl_ALREADY_SIGNALED -> return True
| ret == gl_TIMEOUT_EXPIRED -> return False
| ret == gl_CONDITION_SATISFIED -> return True
| ret == gl_WAIT_FAILED -> return True
where
actual_seconds :: Word64
actual_seconds =
if useconds * 1000 < useconds
then maxBound
else safeFromIntegral $ useconds * 1000
isFenceSignalled :: Fence -> IO Bool
isFenceSignalled = waitFence 0