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 (MonadResource)
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.Vulkan.Types (HasVulkan(getAllocator), Queues(qTransfer), MonadVulkan)
import Engine.Worker qualified as Worker
import Resource.CommandBuffer (oneshot_)
import Resource.Vulkan.Named qualified as Named

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
  , forall (s :: Store) a. Allocated s a -> Maybe Text
aLabel          :: Maybe Text
  } 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)

instance Vk.HasObjectType (Allocated s a) where
  objectTypeAndHandle :: Allocated s a -> (ObjectType, Word64)
objectTypeAndHandle =
    forall a. HasObjectType a => a -> (ObjectType, Word64)
Vk.objectTypeAndHandle forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: Store) a. Allocated s a -> Buffer
aBuffer

allocateCoherent
  :: ( MonadVulkan env m
     , Resource.MonadResource m
     , Storable a
     )
  => Maybe Text
  -> Vk.BufferUsageFlagBits
  -> "initial size" ::: Int
  -> VectorS.Vector a
  -> m (Resource.ReleaseKey, Allocated 'Coherent a)
allocateCoherent :: forall env (m :: * -> *) a.
(MonadVulkan env m, MonadResource m, Storable a) =>
Maybe Text
-> BufferUsageFlagBits
-> Int
-> Vector a
-> m (ReleaseKey, Allocated 'Coherent a)
allocateCoherent Maybe Text
label BufferUsageFlagBits
usage Int
initialSize Vector a
xs = do
  Allocated 'Coherent a
res <- forall a env (m :: * -> *).
(MonadVulkan env m, Storable a) =>
Maybe Text
-> BufferUsageFlagBits
-> Int
-> Vector a
-> m (Allocated 'Coherent a)
createCoherent Maybe Text
label BufferUsageFlagBits
usage Int
initialSize Vector a
xs
  IO ()
destroyIO <- forall (m :: * -> *) a. MonadUnliftIO m => m a -> m (IO a)
toIO do
    env
context <- forall r (m :: * -> *). MonadReader r m => m r
ask
    forall (io :: * -> *) context (s :: Store) a.
(MonadUnliftIO io, HasVulkan context) =>
context -> Allocated s a -> io ()
destroy env
context Allocated 'Coherent a
res
  ReleaseKey
key <- forall (m :: * -> *). MonadResource m => IO () -> m ReleaseKey
Resource.register IO ()
destroyIO
  pure (ReleaseKey
key, Allocated 'Coherent a
res)

createCoherent
  :: forall a env m
  .  ( MonadVulkan env m
     , Storable a
     )
  => Maybe Text
  -> Vk.BufferUsageFlagBits
  -> "initial size" ::: Int
  -> VectorS.Vector a
  -> m (Allocated 'Coherent a)
createCoherent :: forall a env (m :: * -> *).
(MonadVulkan env m, Storable a) =>
Maybe Text
-> BufferUsageFlagBits
-> Int
-> Vector a
-> m (Allocated 'Coherent a)
createCoherent Maybe Text
aLabel BufferUsageFlagBits
usage Int
initialSize Vector a
xs = do
  Allocator
allocator <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall a. HasVulkan a => a -> Allocator
getAllocator
  (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
allocator BufferCreateInfo '[]
bci AllocationCreateInfo
aci
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (forall env (m :: * -> *) a.
(MonadVulkan env m, HasObjectType a) =>
a -> Text -> m ()
Named.object Buffer
aBuffer) Maybe Text
aLabel

  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len forall a. Ord 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
    , Maybe Text
Allocation
AllocationInfo
Buffer
aAllocationInfo :: AllocationInfo
aAllocation :: Allocation
aBuffer :: Buffer
aLabel :: Maybe Text
$sel:aLabel:Allocated :: Maybe Text
$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 :: Word64
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 env m
  .  ( Storable a
     , MonadVulkan env m
     )
  => Maybe Text
  -> Queues Vk.CommandPool
  -> Vk.BufferUsageFlagBits
  -> Int
  -> VectorS.Vector a
  -> m (Allocated 'Staged a)
createStaged :: forall a env (m :: * -> *).
(Storable a, MonadVulkan env m) =>
Maybe Text
-> Queues CommandPool
-> BufferUsageFlagBits
-> Int
-> Vector a
-> m (Allocated 'Staged a)
createStaged Maybe Text
aLabel Queues CommandPool
commandQueues BufferUsageFlagBits
usage Int
initialSize Vector a
xs = do
  Allocator
allocator <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall a. HasVulkan a => a -> Allocator
getAllocator
  (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
allocator BufferCreateInfo '[]
bci AllocationCreateInfo
aci
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (forall env (m :: * -> *) a.
(MonadVulkan env m, HasObjectType a) =>
a -> Text -> m ()
Named.object Buffer
aBuffer) Maybe Text
aLabel

  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len forall a. Ord a => a -> a -> Bool
> Int
0) 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
allocator 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 (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
stageInfo) Ptr a
src Int
lenBytes
        env
context <- forall r (m :: * -> *). MonadReader r m => m r
ask
        forall (io :: * -> *) context.
(MonadUnliftIO io, HasVulkan context) =>
context
-> Queues CommandPool -> Buffer -> Buffer -> Word64 -> io ()
copyBuffer_ env
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
    , $sel:aLabel:Allocated :: Maybe Text
aLabel    = forall a. Maybe a
Nothing
    , Allocation
AllocationInfo
Buffer
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 :: Word64
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 :: Word64
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
      }

register
  :: ( MonadVulkan env m
     , MonadResource m
     )
  => Allocated stage a
  -> m Resource.ReleaseKey
register :: forall env (m :: * -> *) (stage :: Store) a.
(MonadVulkan env m, MonadResource m) =>
Allocated stage a -> m ReleaseKey
register Allocated{Buffer
aBuffer :: Buffer
$sel:aBuffer:Allocated :: forall (s :: Store) a. Allocated s a -> Buffer
aBuffer, Allocation
aAllocation :: Allocation
$sel:aAllocation:Allocated :: forall (s :: Store) a. Allocated s a -> Allocation
aAllocation} = do
  Allocator
allocator <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall a. HasVulkan a => a -> Allocator
getAllocator
  forall (m :: * -> *). MonadResource m => IO () -> m ReleaseKey
Resource.register forall a b. (a -> b) -> a -> b
$ forall (io :: * -> *).
MonadIO io =>
Allocator -> Buffer -> Allocation -> io ()
VMA.destroyBuffer Allocator
allocator Buffer
aBuffer Allocation
aAllocation

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)

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
Maybe Text
Word32
Allocation
AllocationInfo
BufferUsageFlagBits
Buffer
aLabel :: Maybe Text
aUsage :: BufferUsageFlagBits
aUsed :: Word32
aCapacity :: Int
aAllocationInfo :: AllocationInfo
aAllocation :: Allocation
aBuffer :: Buffer
$sel:aLabel:Allocated :: forall (s :: Store) a. Allocated s a -> Maybe Text
$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)

{-# INLINE pokeCoherent #-}
pokeCoherent
  :: ( MonadVulkan env m
     , Storable a
     )
  => Allocated 'Coherent a
  -> Word32
  -> a
  -> m ()
pokeCoherent :: forall env (m :: * -> *) a.
(MonadVulkan env m, Storable a) =>
Allocated 'Coherent a -> Word32 -> a -> m ()
pokeCoherent Allocated{Int
Maybe Text
Word32
Allocation
AllocationInfo
BufferUsageFlagBits
Buffer
aLabel :: Maybe Text
aUsage :: BufferUsageFlagBits
aUsed :: Word32
aCapacity :: Int
aAllocationInfo :: AllocationInfo
aAllocation :: Allocation
aBuffer :: Buffer
$sel:aLabel:Allocated :: forall (s :: Store) a. Allocated s a -> Maybe Text
$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
..} Word32
ix a
new =
  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 ()
  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 ()
  Ptr ()
ptr ->
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO  forall a b. (a -> b) -> a -> b
$
      forall a. Storable a => Ptr a -> Int -> a -> IO ()
Foreign.pokeElemOff
        (forall a b. Ptr a -> Ptr b
Foreign.castPtr Ptr ()
ptr)
        (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
ix)
        a
new

updateCoherent
  :: ( MonadUnliftIO io
     , Foreign.Storable a
     )
  => VectorS.Vector a
  -> Allocated 'Coherent a
  -> io (Allocated 'Coherent a)
updateCoherent :: forall (io :: * -> *) a.
(MonadUnliftIO io, Storable a) =>
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)

{-# INLINE updateCoherentResize_ #-}
updateCoherentResize_
  :: ( MonadVulkan env m
     , Storable a
     )
  => Allocated 'Coherent a
  -> VectorS.Vector a
  -> m (Allocated 'Coherent a)
updateCoherentResize_ :: forall env (m :: * -> *) a.
(MonadVulkan env m, Storable a) =>
Allocated 'Coherent a -> Vector a -> m (Allocated 'Coherent a)
updateCoherentResize_ Allocated 'Coherent a
old Vector a
xs =
  if Int
newSize forall a. Ord a => a -> a -> Bool
<= Int
oldSize then
    forall (io :: * -> *) a.
(MonadUnliftIO io, Storable a) =>
Vector a -> Allocated 'Coherent a -> io (Allocated 'Coherent a)
updateCoherent Vector a
xs Allocated 'Coherent a
old
  else do
    env
ctx <- forall r (m :: * -> *). MonadReader r m => m r
ask
    forall (io :: * -> *) context (s :: Store) a.
(MonadUnliftIO io, HasVulkan context) =>
context -> Allocated s a -> io ()
destroy env
ctx Allocated 'Coherent a
old
    forall a env (m :: * -> *).
(MonadVulkan env m, Storable a) =>
Maybe Text
-> BufferUsageFlagBits
-> Int
-> Vector a
-> m (Allocated 'Coherent a)
createCoherent (forall (s :: Store) a. Allocated s a -> Maybe Text
aLabel Allocated 'Coherent a
old) (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 -> Word64 -> io ()
copyBuffer_ context
context Queues CommandPool
commandQueues Buffer
dst Buffer
src Word64
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
$ Word64 -> Word64 -> Word64 -> BufferCopy
Vk.BufferCopy Word64
0 Word64
0 Word64
sizeBytes)

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

newObserverCoherent
  :: ( MonadVulkan env m
     , Storable a
     )
  => "label" ::: Text
  -> Vk.BufferUsageFlagBits
  -> Int
  -> VectorS.Vector a
  -> Resource.ResourceT m (ObserverCoherent a)
newObserverCoherent :: forall env (m :: * -> *) a.
(MonadVulkan env m, Storable a) =>
Text
-> BufferUsageFlagBits
-> Int
-> Vector a
-> ResourceT m (ObserverCoherent a)
newObserverCoherent Text
label BufferUsageFlagBits
usage Int
initialCapacity Vector a
initialData = do
  Allocated 'Coherent a
initialBuffer <- forall a env (m :: * -> *).
(MonadVulkan env m, Storable a) =>
Maybe Text
-> BufferUsageFlagBits
-> Int
-> Vector a
-> m (Allocated 'Coherent a)
createCoherent (forall a. a -> Maybe a
Just Text
label) BufferUsageFlagBits
usage Int
initialCapacity Vector a
initialData
  ObserverCoherent a
observer <- forall (m :: * -> *) a. MonadIO m => a -> m (ObserverIO a)
Worker.newObserverIO Allocated 'Coherent a
initialBuffer

  env
context <- forall r (m :: * -> *). MonadReader r m => m r
ask
  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 env
context Allocated 'Coherent a
currentBuffer

  pure ObserverCoherent a
observer

{-# INLINE observeCoherentResize_ #-}
observeCoherentResize_
  :: ( MonadVulkan env m
     , Worker.HasOutput source
     , Worker.GetOutput source ~ VectorS.Vector output
     , Storable output
     )
  => source
  -> ObserverCoherent output
  -> m ()
observeCoherentResize_ :: forall env (m :: * -> *) source output.
(MonadVulkan env m, HasOutput source,
 GetOutput source ~ Vector output, Storable output) =>
source -> ObserverCoherent output -> m ()
observeCoherentResize_ source
source ObserverCoherent output
observer = do
  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 env (m :: * -> *) a.
(MonadVulkan env m, Storable a) =>
Allocated 'Coherent a -> Vector a -> m (Allocated 'Coherent a)
updateCoherentResize_

{-# INLINE observeCoherentSingle #-}
observeCoherentSingle
  :: ( MonadVulkan env m
     , Worker.HasOutput source
     , Worker.GetOutput source ~ output
     , Storable output
     )
  => source
  -> ObserverCoherent output
  -> m ()
observeCoherentSingle :: forall env (m :: * -> *) source output.
(MonadVulkan env m, HasOutput source, GetOutput source ~ output,
 Storable output) =>
source -> ObserverCoherent output -> m ()
observeCoherentSingle source
source ObserverCoherent output
observer =
  forall (m :: * -> *) output a.
(MonadUnliftIO m, HasOutput output) =>
output -> ObserverIO a -> (a -> GetOutput output -> m a) -> m ()
Worker.observeIO_ source
source ObserverCoherent output
observer \Allocated 'Coherent output
a GetOutput source
b ->
    forall env (m :: * -> *) a.
(MonadVulkan env m, Storable a) =>
Allocated 'Coherent a -> Word32 -> a -> m ()
pokeCoherent Allocated 'Coherent output
a Word32
0 GetOutput source
b forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Allocated 'Coherent output
a