packages feed

keid-core-0.2.0.0: src/Engine/Frame.hs

module Engine.Frame
  ( Frame(..)
  , initial
  , run
  , advance
  , drain
  , queueSubmit

  , initialRecycledResources
  , timeoutError
  ) where

import RIO

import Control.Monad.Trans.Resource (ResourceT, MonadResource, allocate, release)
import Control.Monad.Trans.Resource qualified as ResourceT
import GHC.IO.Exception (IOErrorType(TimeExpired), IOException(IOError))
import RIO.App (appEnv)
import RIO.Text qualified as Text
import RIO.Vector qualified as Vector
import UnliftIO.Resource qualified as Resource
import Vulkan.Core10 qualified as Vk
import Vulkan.Core12.Promoted_From_VK_KHR_timeline_semaphore qualified as Vk12
import Vulkan.CStruct.Extends (SomeStruct(..), pattern (:&), pattern (::&))
import Vulkan.Utils.Frame (allocateCommandPool, allocateTimelineSemaphore, waitTwice)
import Vulkan.Utils.QueueAssignment (QueueFamilyIndex(..))
import Vulkan.Utils.Queues (Queues(..))
import Vulkan.Utils.RefCounted (newRefCounted)
import Vulkan.Utils.VulkanContext (RecycledResources(..))
import Vulkan.Zero (zero)

import Engine.DataRecycler (DumpResource, WaitResource)
import Engine.Setup.Window qualified as Window
import Engine.Types (GlobalHandles(..), StageRIO, Stage(..), Frame(..), GPUWork)
import Engine.Types.Options (optionsPresent, optionsMsaa)
import Engine.Vulkan.Swapchain (SwapchainResources, allocSwapchainResources, recreateSwapchainResources)
import Engine.Vulkan.Types (HasSwapchain(..), HasVulkan(..), MonadVulkan, Queue(..), withQueue)
import Render.Graph (HasSliceRegistry)
import Render.Target (RenderTarget(..))

initial
  :: Maybe SwapchainResources
  -> DumpResource (RecycledResources rr)
  -> Stage rt p rr st
  -> StageRIO st (Frame rt p rr)
initial oldSR dumpResource Stage{..} = do
  logDebug "Making initial frame"

  GlobalHandles{..} <- asks appEnv
  let device = ghDevice

  let
    fPresent = optionsPresent ghOptions
    fMSAA = optionsMsaa ghOptions

  sfSwapchainResources <- case oldSR of
    Nothing -> do
      windowSize <- liftIO $ Window.getExtent2D ghWindow
      let oldSwapchain = Vk.NULL_HANDLE
      allocSwapchainResources
        fPresent
        fMSAA
        oldSwapchain
        windowSize
        ghSurface
    Just old ->
      pure old

  {- XXX:
    Create this resource object at the global level so it's closed correctly on exception.
  -}
  (stageKey, stageResources) <- allocate ResourceT.createInternalState ResourceT.closeInternalState
  stageRefCounted <- newRefCounted $ release stageKey
  semiFrame <- flip ResourceT.runInternalState stageResources do
    {- XXX:
      Stages appearing on the top of the stage stack are to create their swapchain-derived resources.
      Don't keep the release keys, all resources here are refcounted and live for the lifetime of the stage.
      Resources will be released when the stage is finished or suspended and all the frames are done.
    -}

    debugAlloc <- toIO . logDebug $ "Allocating inside stage " <> display sTitle
    debugRelease <- toIO . logDebug $ "Releasing inside stage " <> display sTitle
    void $! ResourceT.allocate_ debugAlloc debugRelease

    -- For each render target:
    sfTargets <- sAllocateTargets sfSwapchainResources

    -- TODO: Recreate this if the swapchain format changes
    sfPipelines <- sAllocateP sfSwapchainResources sfTargets

    (_, sfRenderFinishedHostSemaphore) <- allocateTimelineSemaphore device 0

    logDebug $ "Creating initial recycled resources for stage " <> display sTitle
    sfRecycledResources <- initialRecycledResources sInitialRR sfTargets sfPipelines
    replicateM_ (INFLIGHT_FRAMES - 1) do
      resources <- initialRecycledResources sInitialRR sfTargets sfPipelines
      liftIO $ dumpResource resources

    releaseDataDebug <- toIO . logDebug $ "Releasing recycled resources for stage " <> display sTitle
    void $! Resource.register releaseDataDebug

    pure
      ( sfSwapchainResources
      , sfTargets
      , sfPipelines
      , sfRenderFinishedHostSemaphore
      , sfRecycledResources
      )

  let
    (fSwapchainResources, fTargets, fPipelines, fRenderFinishedHostSemaphore, fRecycledResources) = semiFrame

  {- XXX:
    Create this resource object at the global level so it's closed correctly on exception.
    Recycled frame resources can linger for a bit longer after its stage is gone, thus 'RefCounted'.
  -}
  fResources <- allocate ResourceT.createInternalState ResourceT.closeInternalState

  fGPUWork <- liftIO $ newIORef mempty

  pure Frame
    { fIndex          = 1
    , fWindow         = ghWindow
    , fSurface        = ghSurface
    , fStageResources = (stageRefCounted, stageResources)
    , ..
    }

pattern INFLIGHT_FRAMES :: (Eq a, Num a) => a
pattern INFLIGHT_FRAMES = 2 -- XXX: up to two frames submitted for rendering

-- | Derive next frame
advance
  :: ( HasLogFunc env
     , HasVulkan env
     , HasSliceRegistry env
     , MonadResource (RIO env)
     , RenderTarget rt
     )
  => WaitResource (RecycledResources rr)
  -> Frame rt p rr
  -> Bool
  -> RIO env (Frame rt p rr)
advance waitDumped f needsNewSwapchain = do
  -- Wait for a prior frame to finish, then we can steal it's resources!
  fRecycledResources <- takeRecycled waitDumped

  (fSwapchainResources, fTargets) <- getNext f

  -- The per-frame resource helpers need to be created fresh
  fGPUWork   <- liftIO $ newIORef mempty
  fResources <- allocate ResourceT.createInternalState ResourceT.closeInternalState

  pure Frame
    { fIndex                       = fIndex f + 1
    , fWindow                      = fWindow f
    , fSurface                     = fSurface f
    , fPipelines                   = fPipelines f
    , fRenderFinishedHostSemaphore = fRenderFinishedHostSemaphore f
    , fStageResources              = fStageResources f
    , fPresent                     = fPresent f
    , fMSAA                        = fMSAA f
    , fSwapchainResources
    , fTargets
    , fGPUWork
    , fResources
    , fRecycledResources
    }
  where
    getNext Frame{..} = do
      if needsNewSwapchain then do
        graphics <- asks (qGraphics . getQueues)
        withQueue graphics Vk.queueWaitIdleSafe
        framebufferExtent <- liftIO $ Window.getExtent2D fWindow
        newResources <- recreateSwapchainResources fMSAA framebufferExtent fSwapchainResources

        let
          formatMatch =
            getSurfaceFormat newResources ==
            getSurfaceFormat fSwapchainResources
        unless formatMatch do
          logWarn "Swapchain changed format"
          throwString "TODO: Handle swapchain changing formats"

        newTargets <- updateTarget newResources fTargets

        pure
          ( newResources
          , newTargets
          )
      else
        pure
          ( fSwapchainResources
          , fTargets
          )

drain
  :: HasLogFunc env
  => WaitResource (RecycledResources rr)
  -> Frame rt p rr
  -> RIO env ()
drain waitDumped Frame{fResources} = do
  release (fst fResources)
  replicateM_ (INFLIGHT_FRAMES - 1) . void $
    takeRecycled waitDumped

takeRecycled
  :: HasLogFunc env
  => WaitResource a
  -> RIO env a
takeRecycled waitDumped =
  -- Handle mvar indefinite timeout exception here:
  -- https://github.com/expipiplus1/vulkan/issues/236
  liftIO waitDumped >>= \case
    Left block ->
      timeout 15e6 (liftIO block) >>= \case
        Nothing -> do
          logError . display $ Text.unwords
            [ "Timed out waiting for recycled resources."
            , "A recycler thread is stuck on timeline semaphore or something."
            , "Try running with --recycler-wait 15000 or a similar value."
            ]
          exitFailure
        Just resources ->
          pure resources
    Right resources ->
      pure resources

run
  :: ( HasLogFunc env
     , HasVulkan env
     , MonadResource (RIO env)
     )
  => (RecycledResources rr -> IO ())
  -> Maybe Int
  -> RIO (env, Frame rt p rr) a
  -> Frame rt p rr
  -> RIO env a
run recycle recyclerWait render frame@Frame{..} = do
  env <- ask
  runRIO (env, frame) render `finally` void (spawn flush)
  where
    flush = do
      device <- asks getDevice
      waits <- readIORef fGPUWork
      let tenSecondsKhr = 10e9
      -- logDebug $ "Waiting Frame " <> displayShow fIndex

      unless (null waits) do
        traverse_ threadDelay recyclerWait
        let
          waitInfo = zero
            { Vk12.semaphores = Vector.fromList (map fst waits)
            , Vk12.values     = Vector.fromList (map snd waits)
            }
        liftIO (waitTwice device waitInfo tenSecondsKhr) >>= \case
          Vk.TIMEOUT -> do
            logError "Time out (10s) waiting for frame to finish on Device"
            timeoutError "Time out (10s) waiting for frame to finish on Device"
            {-
              XXX: recycler thread will crash now, never recycling its resources,
              resulting in an indefinite MVar block.
            -}
          Vk.SUCCESS ->
            pure ()
          huh ->
            logWarn $ "waitTwice returned " <> displayShow huh

      -- logDebug $ "Flushing Frame " <> displayShow fIndex

      -- Free resources wanted elsewhere now, all those in RecycledResources
      for_ (rrCommandPools fRecycledResources) \commandPool ->
        Vk.resetCommandPool device commandPool Vk.COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT

      release (fst fResources) `finally` liftIO (recycle fRecycledResources)

-- | 'queueSubmit' and add wait for the timeline 'Semaphore' before retiring the frame.
queueSubmit
  :: MonadVulkan env m
  => Queue
  -> Vector (SomeStruct Vk.SubmitInfo)
  -> IORef [GPUWork]
  -> Vk.Semaphore
  -> Word64
  -> m ()
queueSubmit queue submits gpuWork hostSemaphore frameIndex = do
  {-
    Make sure we don't get interrupted between submitting the work and
    recording the wait.
  -}
  mask \_ -> withQueue queue \q -> do
    Vk.queueSubmit q submits Vk.NULL_HANDLE
    atomicModifyIORef' gpuWork \waits ->
      ( (hostSemaphore, frameIndex) : waits
      , ()
      )

initialRecycledResources
  :: ( Resource.MonadResource (RIO env)
     , HasVulkan env
     , HasLogFunc env
     )
  => (Queues Vk.CommandPool -> rt -> p -> ResourceT (RIO env) rr)
  -> rt
  -> p
  -> ResourceT (RIO env) (RecycledResources rr)
initialRecycledResources initialRecycledData rts pipes = do
  device <- asks getDevice

  (_iaKey, rrImageAvailable) <- Vk.withSemaphore
    device
    (zero ::& Vk12.SemaphoreTypeCreateInfo Vk12.SEMAPHORE_TYPE_BINARY 0 :& ())
    Nothing
    allocate

  queues <- asks getQueues
  rrCommandPools <- for queues \Queue{family = QueueFamilyIndex ix} -> do
    cpDebug <- toIO . logDebug $ "Release time for command pool for queue " <> display ix
    void $! ResourceT.register cpDebug
    allocateCommandPool device ix

  rrData <- initialRecycledData rrCommandPools rts pipes

  pure RecycledResources{..}

timeoutError :: MonadThrow m => String -> m a
timeoutError message =
  throwM $ IOError Nothing TimeExpired "" message Nothing Nothing

spawn :: (MonadUnliftIO m, MonadResource m) => m a -> m (Async a)
spawn action = do
  actionIO <- toIO action
  {-
    If we don't remove the release key when the thread is done it'll leak,
    remove it at the end of the async action when the thread is going to
    die anyway.

    Mask this so there's no chance we're inturrupted before writing the mvar.
  -}
  kv  <- newEmptyMVar
  mask $ \_ -> do
    (k, r) <- allocate
      (asyncWithUnmask \unmask ->
        unmask $ actionIO <* (Resource.unprotect =<< liftIO (readMVar kv))
      )
      uninterruptibleCancel
    putMVar kv k
    pure r