Recover init and shutdown from withVulkan (#40)

This commit is contained in:
Alexander Bondarenko 2021-03-11 12:00:30 +03:00 committed by GitHub
parent 007b3cccb8
commit 2eddbdfa04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,8 @@ Vulkan backend for Dear ImGui.
module DearImGui.Vulkan module DearImGui.Vulkan
( InitInfo(..) ( InitInfo(..)
, withVulkan , withVulkan
, vulkanInit
, vulkanShutdown
, vulkanNewFrame , vulkanNewFrame
, vulkanRenderDrawData , vulkanRenderDrawData
, vulkanCreateFontsTexture , vulkanCreateFontsTexture
@ -28,7 +30,7 @@ import Data.Word
import Foreign.Marshal.Alloc import Foreign.Marshal.Alloc
( alloca ) ( alloca )
import Foreign.Ptr import Foreign.Ptr
( Ptr, freeHaskellFunPtr, nullPtr ) ( FunPtr, Ptr, freeHaskellFunPtr, nullPtr )
import Foreign.Storable import Foreign.Storable
( Storable(poke) ) ( Storable(poke) )
@ -83,7 +85,15 @@ data InitInfo =
-- | Wraps @ImGui_ImplVulkan_Init@ and @ImGui_ImplVulkan_Shutdown@. -- | Wraps @ImGui_ImplVulkan_Init@ and @ImGui_ImplVulkan_Shutdown@.
withVulkan :: MonadUnliftIO m => InitInfo -> Vulkan.RenderPass -> ( Bool -> m a ) -> m a withVulkan :: MonadUnliftIO m => InitInfo -> Vulkan.RenderPass -> ( Bool -> m a ) -> m a
withVulkan ( InitInfo {..} ) renderPass action = do withVulkan initInfo renderPass action =
bracket
( vulkanInit initInfo renderPass )
vulkanShutdown
( \ ( _, initResult ) -> action initResult )
-- | Wraps @ImGui_ImplVulkan_Init@
vulkanInit :: MonadUnliftIO m => InitInfo -> Vulkan.RenderPass -> m (FunPtr (Vulkan.Result -> IO ()), Bool)
vulkanInit ( InitInfo {..} ) renderPass = do
let let
instancePtr :: Ptr Vulkan.Instance_T instancePtr :: Ptr Vulkan.Instance_T
instancePtr = Vulkan.instanceHandle instance' instancePtr = Vulkan.instanceHandle instance'
@ -97,8 +107,7 @@ withVulkan ( InitInfo {..} ) renderPass action = do
withCallbacks f = case mbAllocator of withCallbacks f = case mbAllocator of
Nothing -> f nullPtr Nothing -> f nullPtr
Just callbacks -> alloca ( \ ptr -> poke ptr callbacks *> f ptr ) Just callbacks -> alloca ( \ ptr -> poke ptr callbacks *> f ptr )
bracket liftIO do
( liftIO do
checkResultFunPtr <- $( C.mkFunPtr [t| Vulkan.Result -> IO () |] ) checkResult checkResultFunPtr <- $( C.mkFunPtr [t| Vulkan.Result -> IO () |] ) checkResult
initResult <- withCallbacks \ callbacksPtr -> initResult <- withCallbacks \ callbacksPtr ->
[C.block| bool { [C.block| bool {
@ -123,12 +132,11 @@ withVulkan ( InitInfo {..} ) renderPass action = do
return ImGui_ImplVulkan_Init(&initInfo, $(VkRenderPass renderPass) ); return ImGui_ImplVulkan_Init(&initInfo, $(VkRenderPass renderPass) );
}|] }|]
pure ( checkResultFunPtr, initResult /= 0 ) pure ( checkResultFunPtr, initResult /= 0 )
)
( \ ( checkResultFunPtr, _ ) -> liftIO do vulkanShutdown :: MonadIO m => (FunPtr a, b) -> m ()
vulkanShutdown ( checkResultFunPtr, _ ) = liftIO do
[C.exp| void { ImGui_ImplVulkan_Shutdown(); } |] [C.exp| void { ImGui_ImplVulkan_Shutdown(); } |]
freeHaskellFunPtr checkResultFunPtr freeHaskellFunPtr checkResultFunPtr
)
( \ ( _, initResult ) -> action initResult )
-- | Wraps @ImGui_ImplVulkan_NewFrame@. -- | Wraps @ImGui_ImplVulkan_NewFrame@.
vulkanNewFrame :: MonadIO m => m () vulkanNewFrame :: MonadIO m => m ()