{-# language CPP #-}
-- No documentation found for Chapter "DeviceInitialization"
module Vulkan.Core11.DeviceInitialization  (enumerateInstanceVersion) where

import Vulkan.Internal.Utils (traceAroundEvent)
import Control.Exception.Base (bracket)
import Control.Monad (unless)
import Control.Monad.IO.Class (liftIO)
import Foreign.Marshal.Alloc (callocBytes)
import Foreign.Marshal.Alloc (free)
import GHC.Base (when)
import GHC.IO (throwIO)
import Foreign.Ptr (castFunPtr)
import GHC.Ptr (nullFunPtr)
import Foreign.Ptr (nullPtr)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Cont (evalContT)
import Control.Monad.IO.Class (MonadIO)
import Foreign.Storable (Storable(peek))
import GHC.IO.Exception (IOErrorType(..))
import GHC.IO.Exception (IOException(..))
import Foreign.Ptr (FunPtr)
import Foreign.Ptr (Ptr)
import GHC.Ptr (Ptr(Ptr))
import Data.Word (Word32)
import Control.Monad.Trans.Cont (ContT(..))
import Vulkan.Dynamic (getInstanceProcAddr')
import Vulkan.NamedType ((:::))
import Vulkan.Core10.Enums.Result (Result)
import Vulkan.Core10.Enums.Result (Result(..))
import Vulkan.Exception (VulkanException(..))
import Vulkan.Core10.Enums.Result (Result(SUCCESS))
foreign import ccall
#if !defined(SAFE_FOREIGN_CALLS)
  unsafe
#endif
  "dynamic" mkVkEnumerateInstanceVersion
  :: FunPtr (Ptr Word32 -> IO Result) -> Ptr Word32 -> IO Result

-- | vkEnumerateInstanceVersion - Query instance-level version before
-- instance creation
--
-- = Description
--
-- Note
--
-- The intended behaviour of 'enumerateInstanceVersion' is that an
-- implementation /should/ not need to perform memory allocations and
-- /should/ unconditionally return 'Vulkan.Core10.Enums.Result.SUCCESS'.
-- The loader, and any enabled layers, /may/ return
-- 'Vulkan.Core10.Enums.Result.ERROR_OUT_OF_HOST_MEMORY' in the case of a
-- failed memory allocation.
--
-- == Return Codes
--
-- [<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#fundamentals-successcodes Success>]
--
--     -   'Vulkan.Core10.Enums.Result.SUCCESS'
--
-- [<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#fundamentals-errorcodes Failure>]
--
--     -   'Vulkan.Core10.Enums.Result.ERROR_OUT_OF_HOST_MEMORY'
--
-- = See Also
--
-- <https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VK_VERSION_1_1 VK_VERSION_1_1>
enumerateInstanceVersion :: forall io
                          . (MonadIO io)
                         => io (("apiVersion" ::: Word32))
enumerateInstanceVersion :: forall (io :: * -> *). MonadIO io => io ("apiVersion" ::: Word32)
enumerateInstanceVersion  = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT forall a b. (a -> b) -> a -> b
$ do
  FunPtr
  (("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)) -> IO Result)
vkEnumerateInstanceVersionPtr <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a b. FunPtr a -> FunPtr b
castFunPtr @_ @(("pApiVersion" ::: Ptr Word32) -> IO Result) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Instance_T -> ("pName" ::: Ptr CChar) -> IO PFN_vkVoidFunction
getInstanceProcAddr' forall a. Ptr a
nullPtr (forall a. Addr# -> Ptr a
Ptr Addr#
"vkEnumerateInstanceVersion"#)
  forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (FunPtr
  (("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)) -> IO Result)
vkEnumerateInstanceVersionPtr forall a. Eq a => a -> a -> Bool
/= forall a. FunPtr a
nullFunPtr) forall a b. (a -> b) -> a -> b
$
    forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError forall a. Maybe a
Nothing IOErrorType
InvalidArgument String
"" String
"The function pointer for vkEnumerateInstanceVersion is null" forall a. Maybe a
Nothing forall a. Maybe a
Nothing
  let vkEnumerateInstanceVersion' :: ("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)) -> IO Result
vkEnumerateInstanceVersion' = FunPtr
  (("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)) -> IO Result)
-> ("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)) -> IO Result
mkVkEnumerateInstanceVersion FunPtr
  (("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)) -> IO Result)
vkEnumerateInstanceVersionPtr
  "pApiVersion" ::: Ptr ("apiVersion" ::: Word32)
pPApiVersion <- forall {k} (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT forall a b. (a -> b) -> a -> b
$ forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (forall a. Int -> IO (Ptr a)
callocBytes @Word32 Int
4) forall a. Ptr a -> IO ()
free
  Result
r <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. String -> IO a -> IO a
traceAroundEvent String
"vkEnumerateInstanceVersion" (("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)) -> IO Result
vkEnumerateInstanceVersion'
                                                               ("pApiVersion" ::: Ptr ("apiVersion" ::: Word32)
pPApiVersion))
  forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Result
r forall a. Ord a => a -> a -> Bool
< Result
SUCCESS) (forall e a. Exception e => e -> IO a
throwIO (Result -> VulkanException
VulkanException Result
r))
  "apiVersion" ::: Word32
pApiVersion <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek @Word32 "pApiVersion" ::: Ptr ("apiVersion" ::: Word32)
pPApiVersion
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ("apiVersion" ::: Word32
pApiVersion)