{-# language CPP #-}
module Graphics.Vulkan.Core11.DeviceInitialization  (enumerateInstanceVersion) where

import Control.Exception.Base (bracket)
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 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 Foreign.Ptr (FunPtr)
import Foreign.Ptr (Ptr)
import GHC.Ptr (Ptr(Ptr))
import Data.Word (Word32)
import Control.Monad.Trans.Cont (ContT(..))
import Graphics.Vulkan.Dynamic (getInstanceProcAddr')
import Graphics.Vulkan.NamedType ((:::))
import Graphics.Vulkan.Core10.Enums.Result (Result)
import Graphics.Vulkan.Core10.Enums.Result (Result(..))
import Graphics.Vulkan.Exception (VulkanException(..))
import Graphics.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
--
-- = Parameters
--
-- -   @pApiVersion@ is a pointer to a @uint32_t@, which is the version of
--     Vulkan supported by instance-level functionality, encoded as
--     described in
--     <https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#extendingvulkan-coreversions-versionnumbers>.
--
-- = Description
--
-- Note
--
-- The intended behaviour of 'enumerateInstanceVersion' is that an
-- implementation /should/ not need to perform memory allocations and
-- /should/ unconditionally return
-- 'Graphics.Vulkan.Core10.Enums.Result.SUCCESS'. The loader, and any
-- enabled layers, /may/ return
-- 'Graphics.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>]
--
--     -   'Graphics.Vulkan.Core10.Enums.Result.SUCCESS'
--
-- [<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#fundamentals-errorcodes Failure>]
--
--     -   'Graphics.Vulkan.Core10.Enums.Result.ERROR_OUT_OF_HOST_MEMORY'
--
-- = See Also
--
-- No cross-references are available
enumerateInstanceVersion :: forall io . MonadIO io => io (("apiVersion" ::: Word32))
enumerateInstanceVersion  = liftIO . evalContT $ do
  vkEnumerateInstanceVersion' <- lift $ mkVkEnumerateInstanceVersion . castFunPtr @_ @(("pApiVersion" ::: Ptr Word32) -> IO Result) <$> getInstanceProcAddr' nullPtr (Ptr "vkEnumerateInstanceVersion"#)
  pPApiVersion <- ContT $ bracket (callocBytes @Word32 4) free
  r <- lift $ vkEnumerateInstanceVersion' (pPApiVersion)
  lift $ when (r < SUCCESS) (throwIO (VulkanException r))
  pApiVersion <- lift $ peek @Word32 pPApiVersion
  pure $ (pApiVersion)