module Resource.Buffer where

import RIO

import Data.Bits ((.|.))
import Data.Vector.Storable qualified as VectorS
import Foreign (Storable)
import Foreign qualified
import UnliftIO.Resource qualified as Resource
import Vulkan.Core10 qualified as Vk
import Vulkan.NamedType ((:::))
import Vulkan.Zero (zero)
import VulkanMemoryAllocator qualified as VMA

import Engine.Types (StageRIO)
import Engine.Vulkan.Types (HasVulkan(getAllocator), Queues(qTransfer))
import Engine.Worker qualified as Worker
import Resource.CommandBuffer (oneshot_)

data Store = Staged | Coherent

data Allocated (s :: Store) a = Allocated
  { forall (s :: Store) a. Allocated s a -> Buffer
aBuffer         :: Vk.Buffer
  , forall (s :: Store) a. Allocated s a -> Allocation
aAllocation     :: VMA.Allocation
  , forall (s :: Store) a. Allocated s a -> AllocationInfo
aAllocationInfo :: VMA.AllocationInfo
  , forall (s :: Store) a. Allocated s a -> Int
aCapacity       :: Int
  , forall (s :: Store) a. Allocated s a -> Word32
aUsed           :: Word32
  , forall (s :: Store) a. Allocated s a -> BufferUsageFlagBits
aUsage          :: Vk.BufferUsageFlagBits
  } deriving (Int -> Allocated s a -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (s :: Store) a. Int -> Allocated s a -> ShowS
forall (s :: Store) a. [Allocated s a] -> ShowS
forall (s :: Store) a. Allocated s a -> String
showList :: [Allocated s a] -> ShowS
$cshowList :: forall (s :: Store) a. [Allocated s a] -> ShowS
show :: Allocated s a -> String
$cshow :: forall (s :: Store) a. Allocated s a -> String
showsPrec :: Int -> Allocated s a -> ShowS
$cshowsPrec :: forall (s :: Store) a. Int -> Allocated s a -> ShowS
Show)

allocateCoherent
  :: (Resource.MonadResource m, Storable a, HasVulkan context)
  => context
  -> Vk.BufferUsageFlagBits
  -> "initial size" ::: Int
  -> VectorS.Vector a
  -> m (Resource.ReleaseKey, Allocated 'Coherent a)
allocateCoherent :: forall (m :: * -> *) a context.
(MonadResource m, Storable a, HasVulkan context) =>
context
-> BufferUsageFlagBits
-> Int
-> Vector a
-> m (ReleaseKey, Allocated 'Coherent a)
allocateCoherent context
context BufferUsageFlagBits
usage Int
initialSize Vector a
xs =
  forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
Resource.allocate
    (forall a context (io :: * -> *).
(Storable a, HasVulkan context, MonadUnliftIO io) =>
context
-> BufferUsageFlagBits
-> Int
-> Vector a
-> io (Allocated 'Coherent a)
createCoherent context
context BufferUsageFlagBits
usage Int
initialSize Vector a
xs)
    (forall (io :: * -> *) context (s :: Store) a.
(MonadUnliftIO io, HasVulkan context) =>
context -> Allocated s a -> io ()
destroy context
context)

createCoherent
  :: forall a context io . (Storable a, HasVulkan context, MonadUnliftIO io)
  => context
  -> Vk.BufferUsageFlagBits
  -> "initial size" ::: Int
  -> VectorS.Vector a
  -> io (Allocated 'Coherent a)
createCoherent :: forall a context (io :: * -> *).
(Storable a, HasVulkan context, MonadUnliftIO io) =>
context
-> BufferUsageFlagBits
-> Int
-> Vector a
-> io (Allocated 'Coherent a)
createCoherent context
context BufferUsageFlagBits
usage Int
initialSize Vector a
xs = do
  (Buffer
aBuffer, Allocation
aAllocation, AllocationInfo
aAllocationInfo) <- forall (a :: [*]) (io :: * -> *).
(Extendss BufferCreateInfo a, PokeChain a, MonadIO io) =>
Allocator
-> BufferCreateInfo a
-> AllocationCreateInfo
-> io (Buffer, Allocation, AllocationInfo)
VMA.createBuffer (forall a. HasVulkan a => a -> Allocator
getAllocator context
context) BufferCreateInfo '[]
bci AllocationCreateInfo
aci

  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len forall a. Eq a => a -> a -> Bool
/= Int
0) forall a b. (a -> b) -> a -> b
$
    if AllocationInfo -> Ptr ()
VMA.mappedData AllocationInfo
aAllocationInfo forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
Foreign.nullPtr then
      forall a. HasCallStack => String -> a
error String
"TODO: recover from unmapped data and flush manually"
    else
      forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
VectorS.unsafeWith Vector a
xs \Ptr a
src ->
        forall a. Ptr a -> Ptr a -> Int -> IO ()
Foreign.copyBytes (forall a b. Ptr a -> Ptr b
Foreign.castPtr forall a b. (a -> b) -> a -> b
$ AllocationInfo -> Ptr ()
VMA.mappedData AllocationInfo
aAllocationInfo) Ptr a
src Int
lenBytes

  pure Allocated
    { $sel:aCapacity:Allocated :: Int
aCapacity = forall a. Ord a => a -> a -> a
max Int
initialSize Int
len
    , $sel:aUsed:Allocated :: Word32
aUsed     = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len
    , $sel:aUsage:Allocated :: BufferUsageFlagBits
aUsage    = BufferUsageFlagBits
usage
    , Buffer
Allocation
AllocationInfo
aAllocationInfo :: AllocationInfo
aAllocation :: Allocation
aBuffer :: Buffer
$sel:aAllocationInfo:Allocated :: AllocationInfo
$sel:aAllocation:Allocated :: Allocation
$sel:aBuffer:Allocated :: Buffer
..
    }
  where

    len :: Int
len = forall a. Storable a => Vector a -> Int
VectorS.length Vector a
xs
    lenBytes :: Int
lenBytes = forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. HasCallStack => a
undefined :: a) forall a. Num a => a -> a -> a
* Int
len

    sizeBytes :: Int
sizeBytes = forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. HasCallStack => a
undefined :: a) forall a. Num a => a -> a -> a
* forall a. Ord a => a -> a -> a
max Int
initialSize Int
len

    bci :: BufferCreateInfo '[]
bci = forall a. Zero a => a
zero
      { $sel:size:BufferCreateInfo :: DeviceSize
Vk.size        = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sizeBytes
      , $sel:usage:BufferCreateInfo :: BufferUsageFlagBits
Vk.usage       = BufferUsageFlagBits
usage
      , $sel:sharingMode:BufferCreateInfo :: SharingMode
Vk.sharingMode = SharingMode
Vk.SHARING_MODE_EXCLUSIVE
      }

    flags :: MemoryPropertyFlagBits
flags =
      MemoryPropertyFlagBits
Vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT forall a. Bits a => a -> a -> a
.|.
      MemoryPropertyFlagBits
Vk.MEMORY_PROPERTY_HOST_COHERENT_BIT

    aci :: AllocationCreateInfo
aci = forall a. Zero a => a
zero
      { $sel:flags:AllocationCreateInfo :: AllocationCreateFlags
VMA.flags          = AllocationCreateFlags
VMA.ALLOCATION_CREATE_MAPPED_BIT
      , $sel:usage:AllocationCreateInfo :: MemoryUsage
VMA.usage          = MemoryUsage
VMA.MEMORY_USAGE_GPU_ONLY
      , $sel:requiredFlags:AllocationCreateInfo :: MemoryPropertyFlagBits
VMA.requiredFlags  = MemoryPropertyFlagBits
flags
      , $sel:preferredFlags:AllocationCreateInfo :: MemoryPropertyFlagBits
VMA.preferredFlags = MemoryPropertyFlagBits
Vk.MEMORY_PROPERTY_DEVICE_LOCAL_BIT
      }

createStaged
  :: forall a context io . (Storable a, HasVulkan context, MonadUnliftIO io)
  => context
  -> Queues Vk.CommandPool
  -> Vk.BufferUsageFlagBits
  -> Int
  -> VectorS.Vector a
  -> io (Allocated 'Staged a)
createStaged :: forall a context (io :: * -> *).
(Storable a, HasVulkan context, MonadUnliftIO io) =>
context
-> Queues CommandPool
-> BufferUsageFlagBits
-> Int
-> Vector a
-> io (Allocated 'Staged a)
createStaged context
context Queues CommandPool
commandQueues BufferUsageFlagBits
usage Int
initialSize Vector a
xs = do
  (Buffer
aBuffer, Allocation
aAllocation, AllocationInfo
aAllocationInfo) <- forall (a :: [*]) (io :: * -> *).
(Extendss BufferCreateInfo a, PokeChain a, MonadIO io) =>
Allocator
-> BufferCreateInfo a
-> AllocationCreateInfo
-> io (Buffer, Allocation, AllocationInfo)
VMA.createBuffer Allocator
vma BufferCreateInfo '[]
bci AllocationCreateInfo
aci

  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len forall a. Eq a => a -> a -> Bool
/= Int
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
    forall (a :: [*]) (io :: * -> *) r.
(Extendss BufferCreateInfo a, PokeChain a, MonadIO io) =>
Allocator
-> BufferCreateInfo a
-> AllocationCreateInfo
-> (io (Buffer, Allocation, AllocationInfo)
    -> ((Buffer, Allocation, AllocationInfo) -> io ()) -> r)
-> r
VMA.withBuffer Allocator
vma BufferCreateInfo '[]
stageCI AllocationCreateInfo
stageAI forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket \(Buffer
staging, Allocation
_stage, AllocationInfo
stageInfo) ->
      if AllocationInfo -> Ptr ()
VMA.mappedData AllocationInfo
stageInfo forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
Foreign.nullPtr then
        forall a. HasCallStack => String -> a
error String
"TODO: recover from unmapped data and flush manually"
      else do
        forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
VectorS.unsafeWith Vector a
xs \Ptr a
src ->
          forall a. Ptr a -> Ptr a -> Int -> IO ()
Foreign.copyBytes (forall a b. Ptr a -> Ptr b
Foreign.castPtr forall a b. (a -> b) -> a -> b
$ AllocationInfo -> Ptr ()
VMA.mappedData AllocationInfo
stageInfo) Ptr a
src Int
lenBytes
        forall (io :: * -> *) context.
(MonadUnliftIO io, HasVulkan context) =>
context
-> Queues CommandPool -> Buffer -> Buffer -> DeviceSize -> io ()
copyBuffer_ context
context Queues CommandPool
commandQueues Buffer
aBuffer Buffer
staging (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sizeBytes)

  pure Allocated
    { $sel:aCapacity:Allocated :: Int
aCapacity = forall a. Ord a => a -> a -> a
max Int
initialSize Int
len
    , $sel:aUsed:Allocated :: Word32
aUsed     = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len
    , $sel:aUsage:Allocated :: BufferUsageFlagBits
aUsage    = BufferUsageFlagBits
usage
    , Buffer
Allocation
AllocationInfo
aAllocationInfo :: AllocationInfo
aAllocation :: Allocation
aBuffer :: Buffer
$sel:aAllocationInfo:Allocated :: AllocationInfo
$sel:aAllocation:Allocated :: Allocation
$sel:aBuffer:Allocated :: Buffer
..
    }
  where
    vma :: Allocator
vma = forall a. HasVulkan a => a -> Allocator
getAllocator context
context

    len :: Int
len = forall a. Storable a => Vector a -> Int
VectorS.length Vector a
xs
    lenBytes :: Int
lenBytes = forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. HasCallStack => a
undefined :: a) forall a. Num a => a -> a -> a
* Int
len

    sizeBytes :: Int
sizeBytes = forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. HasCallStack => a
undefined :: a) forall a. Num a => a -> a -> a
* forall a. Ord a => a -> a -> a
max Int
initialSize Int
len

    bci :: BufferCreateInfo '[]
bci = forall a. Zero a => a
zero
      { $sel:size:BufferCreateInfo :: DeviceSize
Vk.size        = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sizeBytes
      , $sel:usage:BufferCreateInfo :: BufferUsageFlagBits
Vk.usage       = BufferUsageFlagBits
Vk.BUFFER_USAGE_TRANSFER_DST_BIT forall a. Bits a => a -> a -> a
.|. BufferUsageFlagBits
usage
      , $sel:sharingMode:BufferCreateInfo :: SharingMode
Vk.sharingMode = SharingMode
Vk.SHARING_MODE_EXCLUSIVE
      }

    aci :: AllocationCreateInfo
aci = forall a. Zero a => a
zero
      { $sel:usage:AllocationCreateInfo :: MemoryUsage
VMA.usage         = MemoryUsage
VMA.MEMORY_USAGE_GPU_ONLY
      , $sel:requiredFlags:AllocationCreateInfo :: MemoryPropertyFlagBits
VMA.requiredFlags = MemoryPropertyFlagBits
Vk.MEMORY_PROPERTY_DEVICE_LOCAL_BIT
      }

    stageCI :: BufferCreateInfo '[]
stageCI = forall a. Zero a => a
zero
      { $sel:size:BufferCreateInfo :: DeviceSize
Vk.size        = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sizeBytes
      , $sel:usage:BufferCreateInfo :: BufferUsageFlagBits
Vk.usage       = BufferUsageFlagBits
Vk.BUFFER_USAGE_TRANSFER_SRC_BIT
      , $sel:sharingMode:BufferCreateInfo :: SharingMode
Vk.sharingMode = SharingMode
Vk.SHARING_MODE_EXCLUSIVE
      }

    stageAI :: AllocationCreateInfo
stageAI = forall a. Zero a => a
zero
      { $sel:flags:AllocationCreateInfo :: AllocationCreateFlags
VMA.flags         = AllocationCreateFlags
VMA.ALLOCATION_CREATE_MAPPED_BIT
      , $sel:usage:AllocationCreateInfo :: MemoryUsage
VMA.usage         = MemoryUsage
VMA.MEMORY_USAGE_CPU_TO_GPU
      , $sel:requiredFlags:AllocationCreateInfo :: MemoryPropertyFlagBits
VMA.requiredFlags = MemoryPropertyFlagBits
Vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT
      }

destroy
  :: (MonadUnliftIO io, HasVulkan context)
  => context
  -> Allocated s a
  -> io ()
destroy :: forall (io :: * -> *) context (s :: Store) a.
(MonadUnliftIO io, HasVulkan context) =>
context -> Allocated s a -> io ()
destroy context
context Allocated s a
a =
  forall (io :: * -> *).
MonadIO io =>
Allocator -> Buffer -> Allocation -> io ()
VMA.destroyBuffer (forall a. HasVulkan a => a -> Allocator
getAllocator context
context) (forall (s :: Store) a. Allocated s a -> Buffer
aBuffer Allocated s a
a) (forall (s :: Store) a. Allocated s a -> Allocation
aAllocation Allocated s a
a)

destroyAll
  :: (MonadUnliftIO io, HasVulkan context, Foldable t)
  => context
  -> t (Allocated s a)
  -> io ()
destroyAll :: forall (io :: * -> *) context (t :: * -> *) (s :: Store) a.
(MonadUnliftIO io, HasVulkan context, Foldable t) =>
context -> t (Allocated s a) -> io ()
destroyAll context
context =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ \Allocated s a
a ->
    forall (io :: * -> *).
MonadIO io =>
Allocator -> Buffer -> Allocation -> io ()
VMA.destroyBuffer (forall a. HasVulkan a => a -> Allocator
getAllocator context
context) (forall (s :: Store) a. Allocated s a -> Buffer
aBuffer Allocated s a
a) (forall (s :: Store) a. Allocated s a -> Allocation
aAllocation Allocated s a
a)

peekCoherent :: (MonadIO m, Storable a) => Word32 -> Allocated 'Coherent a -> m (Maybe a)
peekCoherent :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Word32 -> Allocated 'Coherent a -> m (Maybe a)
peekCoherent Word32
ix Allocated{Int
Word32
BufferUsageFlagBits
Buffer
Allocation
AllocationInfo
aUsage :: BufferUsageFlagBits
aUsed :: Word32
aCapacity :: Int
aAllocationInfo :: AllocationInfo
aAllocation :: Allocation
aBuffer :: Buffer
$sel:aUsage:Allocated :: forall (s :: Store) a. Allocated s a -> BufferUsageFlagBits
$sel:aUsed:Allocated :: forall (s :: Store) a. Allocated s a -> Word32
$sel:aCapacity:Allocated :: forall (s :: Store) a. Allocated s a -> Int
$sel:aAllocationInfo:Allocated :: forall (s :: Store) a. Allocated s a -> AllocationInfo
$sel:aAllocation:Allocated :: forall (s :: Store) a. Allocated s a -> Allocation
$sel:aBuffer:Allocated :: forall (s :: Store) a. Allocated s a -> Buffer
..} =
  case AllocationInfo -> Ptr ()
VMA.mappedData AllocationInfo
aAllocationInfo of
    Ptr ()
_ | Word32
ix forall a. Num a => a -> a -> a
+ Word32
1 forall a. Ord a => a -> a -> Bool
> Word32
aUsed ->
      forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
    Ptr ()
ptr | Ptr ()
ptr forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
Foreign.nullPtr ->
      forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
    Ptr ()
ptr ->
      forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
        forall a. Storable a => Ptr a -> Int -> IO a
Foreign.peekElemOff (forall a b. Ptr a -> Ptr b
Foreign.castPtr Ptr ()
ptr) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
ix)

updateCoherent
  :: (Foreign.Storable a, MonadUnliftIO io)
  => VectorS.Vector a
  -> Allocated 'Coherent a
  -> io (Allocated 'Coherent a)
updateCoherent :: forall a (io :: * -> *).
(Storable a, MonadUnliftIO io) =>
Vector a -> Allocated 'Coherent a -> io (Allocated 'Coherent a)
updateCoherent Vector a
xs Allocated 'Coherent a
old = do
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
VectorS.unsafeWith Vector a
xs \Ptr a
src ->
    forall a. Ptr a -> Ptr a -> Int -> IO ()
Foreign.copyBytes Ptr a
dst Ptr a
src Int
lenBytes
  pure Allocated 'Coherent a
old
    { $sel:aUsed:Allocated :: Word32
aUsed = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len
    }
  where
    dst :: Ptr a
dst = forall a b. Ptr a -> Ptr b
Foreign.castPtr forall a b. (a -> b) -> a -> b
$ AllocationInfo -> Ptr ()
VMA.mappedData (forall (s :: Store) a. Allocated s a -> AllocationInfo
aAllocationInfo Allocated 'Coherent a
old)
    len :: Int
len = forall a. Storable a => Vector a -> Int
VectorS.length Vector a
xs
    lenBytes :: Int
lenBytes = Int
len forall a. Num a => a -> a -> a
* forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. Storable a => Vector a -> a
VectorS.head Vector a
xs)

updateCoherentResize_
  :: (Storable a, HasVulkan context, MonadUnliftIO io)
  => context
  -> Allocated 'Coherent a
  -> VectorS.Vector a
  -> io (Allocated 'Coherent a)
updateCoherentResize_ :: forall a context (io :: * -> *).
(Storable a, HasVulkan context, MonadUnliftIO io) =>
context
-> Allocated 'Coherent a -> Vector a -> io (Allocated 'Coherent a)
updateCoherentResize_ context
ctx Allocated 'Coherent a
old Vector a
xs =
  if Int
newSize forall a. Ord a => a -> a -> Bool
<= Int
oldSize then
    forall a (io :: * -> *).
(Storable a, MonadUnliftIO io) =>
Vector a -> Allocated 'Coherent a -> io (Allocated 'Coherent a)
updateCoherent Vector a
xs Allocated 'Coherent a
old
  else do
    forall (io :: * -> *) context (t :: * -> *) (s :: Store) a.
(MonadUnliftIO io, HasVulkan context, Foldable t) =>
context -> t (Allocated s a) -> io ()
destroyAll context
ctx [Allocated 'Coherent a
old]
    forall a context (io :: * -> *).
(Storable a, HasVulkan context, MonadUnliftIO io) =>
context
-> BufferUsageFlagBits
-> Int
-> Vector a
-> io (Allocated 'Coherent a)
createCoherent context
ctx (forall (s :: Store) a. Allocated s a -> BufferUsageFlagBits
aUsage Allocated 'Coherent a
old) Int
newSize Vector a
xs
  where
    oldSize :: Int
oldSize = forall (s :: Store) a. Allocated s a -> Int
aCapacity Allocated 'Coherent a
old
    newSize :: Int
newSize = forall a. Storable a => Vector a -> Int
VectorS.length Vector a
xs

-- TODO: add a staged buffer to check out the transfer queue
copyBuffer_
  :: (MonadUnliftIO io, HasVulkan context)
  => context
  -> Queues Vk.CommandPool
  -> ("dstBuffer" ::: Vk.Buffer)
  -> ("srcBuffer" ::: Vk.Buffer)
  -> Vk.DeviceSize
  -> io ()
copyBuffer_ :: forall (io :: * -> *) context.
(MonadUnliftIO io, HasVulkan context) =>
context
-> Queues CommandPool -> Buffer -> Buffer -> DeviceSize -> io ()
copyBuffer_ context
context Queues CommandPool
commandQueues Buffer
dst Buffer
src DeviceSize
sizeBytes =
  forall context (m :: * -> *).
(HasVulkan context, MonadUnliftIO m) =>
context
-> Queues CommandPool
-> (forall a. Queues a -> a)
-> (CommandBuffer -> m ())
-> m ()
oneshot_ context
context Queues CommandPool
commandQueues forall a. Queues a -> a
qTransfer \CommandBuffer
cmd ->
    forall (io :: * -> *).
MonadIO io =>
CommandBuffer
-> Buffer -> Buffer -> ("regions" ::: Vector BufferCopy) -> io ()
Vk.cmdCopyBuffer CommandBuffer
cmd Buffer
src Buffer
dst
      (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ DeviceSize -> DeviceSize -> DeviceSize -> BufferCopy
Vk.BufferCopy DeviceSize
0 DeviceSize
0 DeviceSize
sizeBytes)

type ObserverCoherent a = Worker.ObserverIO (Allocated 'Coherent a)

newObserverCoherent
  :: Storable a
  => Vk.BufferUsageFlagBits
  -> Int
  -> Resource.ResourceT (StageRIO st) (ObserverCoherent a)
newObserverCoherent :: forall a st.
Storable a =>
BufferUsageFlagBits
-> Int -> ResourceT (StageRIO st) (ObserverCoherent a)
newObserverCoherent BufferUsageFlagBits
usage Int
initialSize = do
  App GlobalHandles st
context <- forall r (m :: * -> *). MonadReader r m => m r
ask

  Allocated 'Coherent a
initialBuffer <- forall a context (io :: * -> *).
(Storable a, HasVulkan context, MonadUnliftIO io) =>
context
-> BufferUsageFlagBits
-> Int
-> Vector a
-> io (Allocated 'Coherent a)
createCoherent App GlobalHandles st
context BufferUsageFlagBits
usage Int
initialSize forall a. Monoid a => a
mempty
  ObserverCoherent a
observer <- forall (m :: * -> *) a. MonadIO m => a -> m (ObserverIO a)
Worker.newObserverIO Allocated 'Coherent a
initialBuffer

  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$! forall (m :: * -> *). MonadResource m => IO () -> m ReleaseKey
Resource.register do
    Allocated 'Coherent a
currentBuffer <- forall (m :: * -> *) a.
MonadUnliftIO m =>
IORef (Versioned a) -> m a
Worker.readObservedIO ObserverCoherent a
observer
    forall (io :: * -> *) context (s :: Store) a.
(MonadUnliftIO io, HasVulkan context) =>
context -> Allocated s a -> io ()
destroy App GlobalHandles st
context Allocated 'Coherent a
currentBuffer

  pure ObserverCoherent a
observer

{-# INLINE observeCoherentResize_ #-}
observeCoherentResize_
  :: ( HasVulkan env
     , Worker.HasOutput source
     , Worker.GetOutput source ~ VectorS.Vector output
     , Storable output
     )
  => source
  -> ObserverCoherent output
  -> RIO env ()
observeCoherentResize_ :: forall env source output.
(HasVulkan env, HasOutput source, GetOutput source ~ Vector output,
 Storable output) =>
source -> ObserverCoherent output -> RIO env ()
observeCoherentResize_ source
source ObserverCoherent output
observer = do
  env
context <- forall r (m :: * -> *). MonadReader r m => m r
ask
  forall (m :: * -> *) output a.
(MonadUnliftIO m, HasOutput output) =>
output -> ObserverIO a -> (a -> GetOutput output -> m a) -> m ()
Worker.observeIO_ source
source ObserverCoherent output
observer forall a b. (a -> b) -> a -> b
$
    forall a context (io :: * -> *).
(Storable a, HasVulkan context, MonadUnliftIO io) =>
context
-> Allocated 'Coherent a -> Vector a -> io (Allocated 'Coherent a)
updateCoherentResize_ env
context