{-# LANGUAGE ImportQualifiedPost #-} {-# LANGUAGE BlockArguments #-} {-# LANGUAGE MonoLocalBinds #-} {-# LANGUAGE FlexibleContexts, UndecidableInstances #-} {-# LANGUAGE ViewPatterns #-} {-# LANGUAGE StandaloneDeriving #-} {-# OPTIONS_GHC -Wall -fno-warn-tabs #-} module Gpu.Vulkan.CommandPool.Middle.Internal ( C(..), CreateInfo(..), create, destroy, reset ) where import Foreign.Ptr import Foreign.Marshal.Alloc import Foreign.Storable import Foreign.Storable.PeekPoke (WithPoked, withPoked, withPoked', withPtrS) import Data.TypeLevel.Maybe qualified as TMaybe import Data.TypeLevel.ParMaybe qualified as TPMaybe import Gpu.Vulkan.Exception.Middle.Internal import Gpu.Vulkan.Exception.Enum import Gpu.Vulkan.CommandPool.Enum import Gpu.Vulkan.AllocationCallbacks.Middle.Internal qualified as AllocationCallbacks import qualified Gpu.Vulkan.Device.Middle.Types as Device import qualified Gpu.Vulkan.QueueFamily.EnumManual as QueueFamily import qualified Gpu.Vulkan.CommandPool.Core as C data CreateInfo mn = CreateInfo { createInfoNext :: TMaybe.M mn, createInfoFlags :: CreateFlags, createInfoQueueFamilyIndex :: QueueFamily.Index } deriving instance Show (TMaybe.M mn) => Show (CreateInfo mn) createInfoToCore :: WithPoked (TMaybe.M mn) => CreateInfo mn -> (Ptr C.CreateInfo -> IO a) -> IO () createInfoToCore CreateInfo { createInfoNext = mnxt, createInfoFlags = CreateFlagBits flgs, createInfoQueueFamilyIndex = QueueFamily.Index qfi } f = withPoked' mnxt \pnxt -> withPtrS pnxt \(castPtr -> pnxt') -> withPoked C.CreateInfo { C.createInfoSType = (), C.createInfoPNext = pnxt', C.createInfoFlags = flgs, C.createInfoQueueFamilyIndex = qfi } f newtype C = C C.C deriving Show create :: WithPoked (TMaybe.M mn) => Device.D -> CreateInfo mn -> TPMaybe.M AllocationCallbacks.A mc -> IO C create (Device.D dvc) ci mac = C <$> alloca \pc -> do createInfoToCore ci \pci -> AllocationCallbacks.mToCore mac \pac -> throwUnlessSuccess . Result =<< C.create dvc pci pac pc peek pc destroy :: Device.D -> C -> TPMaybe.M AllocationCallbacks.A md -> IO () destroy (Device.D dvc) (C c) mac = AllocationCallbacks.mToCore mac $ C.destroy dvc c reset :: Device.D -> C -> ResetFlags -> IO () reset (Device.D dv) (C c) (ResetFlagBits fs) = C.reset dv c fs