{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PatternSynonyms #-}
{-# OPTIONS_GHC -Wall -fno-warn-tabs #-}

module Gpu.Vulkan.Sparse.Internal where

import Data.TypeLevel.Tuple.Uncurry
import Data.HeteroParList (pattern (:**))
import Data.HeteroParList qualified as HPList

import Gpu.Vulkan.Device qualified as Device
import Gpu.Vulkan.Memory.Type qualified as Memory
import Gpu.Vulkan.Sparse.Enum

import Gpu.Vulkan.Sparse.Middle qualified as M

data MemoryBind sm ibargs i = MemoryBind {
	forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> Size
memoryBindResourceOffset :: Device.Size,
	forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> Size
memoryBindSize :: Device.Size,
	forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> M sm ibargs
memoryBindMemory :: Memory.M sm ibargs,
	forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> RawOffset i
memoryBindMemoryOffset :: Memory.RawOffset i,
	forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> MemoryBindFlags
memoryBindFlags :: MemoryBindFlags }

memoryBindToMiddle :: Memory.RawOffsetToOffset ibargs i =>
	Device.D sd -> MemoryBind sm ibargs i -> IO M.MemoryBind
memoryBindToMiddle :: forall (ibargs :: [(*, ImageBufferArg)]) (i :: Nat) sd sm.
RawOffsetToOffset ibargs i =>
D sd -> MemoryBind sm ibargs i -> IO MemoryBind
memoryBindToMiddle D sd
dv MemoryBind {
	memoryBindResourceOffset :: forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> Size
memoryBindResourceOffset = Size
rsco,
	memoryBindSize :: forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> Size
memoryBindSize = Size
sz,
	memoryBindMemory :: forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> M sm ibargs
memoryBindMemory = m :: M sm ibargs
m@(Memory.M IORef (PL (U2 ImageBuffer) ibargs)
_ M
mm),
	memoryBindMemoryOffset :: forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> RawOffset i
memoryBindMemoryOffset = RawOffset i
rwo,
	memoryBindFlags :: forall sm (ibargs :: [(*, ImageBufferArg)]) (i :: Nat).
MemoryBind sm ibargs i -> MemoryBindFlags
memoryBindFlags = MemoryBindFlags
fs } = do
	mo <- D sd -> M sm ibargs -> RawOffset i -> IO Size
forall (ibargs :: [(*, ImageBufferArg)]) (n :: Nat) sd sm.
RawOffsetToOffset ibargs n =>
D sd -> M sm ibargs -> RawOffset n -> IO Size
Memory.rawOffset D sd
dv M sm ibargs
m RawOffset i
rwo
	pure M.MemoryBind {
		M.memoryBindResourceOffset = rsco,
		M.memoryBindSize = sz,
		M.memoryBindMemory = mm,
		M.memoryBindMemoryOffset = mo,
		M.memoryBindFlags = fs }

class MemoryBindsToMiddle sais where
	memoryBindsToMiddle ::
		Device.D sd -> HPList.PL (U3 MemoryBind) sais -> IO [M.MemoryBind]

instance MemoryBindsToMiddle '[] where
	memoryBindsToMiddle :: forall sd. D sd -> PL (U3 MemoryBind) '[] -> IO [MemoryBind]
memoryBindsToMiddle D sd
_ PL (U3 MemoryBind) '[]
HPList.Nil = [MemoryBind] -> IO [MemoryBind]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []

instance (Memory.RawOffsetToOffset ibargs i, MemoryBindsToMiddle sais) =>
	MemoryBindsToMiddle ('(sm, ibargs, i) ': sais) where
	memoryBindsToMiddle :: forall sd.
D sd
-> PL (U3 MemoryBind) ('(sm, ibargs, i) : sais) -> IO [MemoryBind]
memoryBindsToMiddle D sd
dv (U3 MemoryBind s1 s2 s3
mb :** PL (U3 MemoryBind) ss1
mbs) = (:)
		(MemoryBind -> [MemoryBind] -> [MemoryBind])
-> IO MemoryBind -> IO ([MemoryBind] -> [MemoryBind])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> D sd -> MemoryBind s1 s2 s3 -> IO MemoryBind
forall (ibargs :: [(*, ImageBufferArg)]) (i :: Nat) sd sm.
RawOffsetToOffset ibargs i =>
D sd -> MemoryBind sm ibargs i -> IO MemoryBind
memoryBindToMiddle D sd
dv MemoryBind s1 s2 s3
mb
		IO ([MemoryBind] -> [MemoryBind])
-> IO [MemoryBind] -> IO [MemoryBind]
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> D sd -> PL (U3 MemoryBind) ss1 -> IO [MemoryBind]
forall (sais :: [(*, [(*, ImageBufferArg)], Nat)]) sd.
MemoryBindsToMiddle sais =>
D sd -> PL (U3 MemoryBind) sais -> IO [MemoryBind]
forall sd. D sd -> PL (U3 MemoryBind) ss1 -> IO [MemoryBind]
memoryBindsToMiddle D sd
dv PL (U3 MemoryBind) ss1
mbs