{-# OPTIONS_GHC -Wwarn=orphans #-}

{-# LANGUAGE OverloadedLists #-}

module Engine.Vulkan.Shader
  ( Shader(..)
  , create
  , destroy

  , withSpecialization
  , Specialization(..)

  , SpecializationConst(..)
  ) where

import RIO

import Data.Vector qualified as Vector
import Data.Vector.Storable qualified as Storable
import Foreign qualified
import GHC.Stack (withFrozenCallStack)
import Vulkan.Core10 qualified as Vk
import Vulkan.CStruct.Extends (SomeStruct(..))
import Vulkan.Zero (Zero(..))
import Unsafe.Coerce (unsafeCoerce)

import Engine.Vulkan.Pipeline.Stages (StageInfo(..))
import Engine.Vulkan.Types (MonadVulkan, getDevice)
import Resource.Vulkan.Named qualified as Named

data Shader = Shader
  { Shader -> Vector ShaderModule
sModules        :: Vector Vk.ShaderModule
  , Shader -> Vector (SomeStruct PipelineShaderStageCreateInfo)
sPipelineStages :: Vector (SomeStruct Vk.PipelineShaderStageCreateInfo)
  }

create
  :: ( MonadVulkan env io
     , StageInfo t
     , HasCallStack
     )
  => t (Maybe ByteString)
  -> Maybe Vk.SpecializationInfo
  -> io Shader
create :: forall env (io :: * -> *) (t :: * -> *).
(MonadVulkan env io, StageInfo t, HasCallStack) =>
t (Maybe ByteString) -> Maybe SpecializationInfo -> io Shader
create t (Maybe ByteString)
stages Maybe SpecializationInfo
spec = (HasCallStack => io Shader) -> io Shader
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack do
  Device
device <- (env -> Device) -> io Device
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks env -> Device
forall a. HasVulkan a => a -> Device
getDevice
  Vector (ShaderModule, SomeStruct PipelineShaderStageCreateInfo)
staged <- Vector (ShaderStageFlagBits, ByteString)
-> ((ShaderStageFlagBits, ByteString)
    -> io (ShaderModule, SomeStruct PipelineShaderStageCreateInfo))
-> io
     (Vector (ShaderModule, SomeStruct PipelineShaderStageCreateInfo))
forall (m :: * -> *) a b.
Monad m =>
Vector a -> (a -> m b) -> m (Vector b)
Vector.forM Vector (ShaderStageFlagBits, ByteString)
collected \(ShaderStageFlagBits
stage, ByteString
code) -> do
    ShaderModule
module_ <- Device
-> ShaderModuleCreateInfo '[]
-> ("allocator" ::: Maybe AllocationCallbacks)
-> io ShaderModule
forall (a :: [*]) (io :: * -> *).
(Extendss ShaderModuleCreateInfo a, PokeChain a, MonadIO io) =>
Device
-> ShaderModuleCreateInfo a
-> ("allocator" ::: Maybe AllocationCallbacks)
-> io ShaderModule
Vk.createShaderModule
      Device
device
      ShaderModuleCreateInfo '[]
forall a. Zero a => a
zero
        { $sel:code:ShaderModuleCreateInfo :: ByteString
Vk.code = ByteString
code
        }
      "allocator" ::: Maybe AllocationCallbacks
forall a. Maybe a
Nothing

    ShaderModule -> io ()
forall env (m :: * -> *) a.
(MonadVulkan env m, HasObjectType a, HasCallStack) =>
a -> m ()
Named.objectOrigin ShaderModule
module_

    pure
      ( ShaderModule
module_
      , PipelineShaderStageCreateInfo '[]
-> SomeStruct PipelineShaderStageCreateInfo
forall (a :: [*] -> *) (es :: [*]).
(Extendss a es, PokeChain es, Show (Chain es)) =>
a es -> SomeStruct a
SomeStruct PipelineShaderStageCreateInfo '[]
forall a. Zero a => a
zero
          { $sel:stage:PipelineShaderStageCreateInfo :: ShaderStageFlagBits
Vk.stage              = ShaderStageFlagBits
stage
          , $sel:module':PipelineShaderStageCreateInfo :: ShaderModule
Vk.module'            = ShaderModule
module_
          , $sel:name:PipelineShaderStageCreateInfo :: ByteString
Vk.name               = ByteString
"main"
          , $sel:specializationInfo:PipelineShaderStageCreateInfo :: Maybe SpecializationInfo
Vk.specializationInfo = Maybe SpecializationInfo
spec
          }
      )
  let (Vector ShaderModule
modules, Vector (SomeStruct PipelineShaderStageCreateInfo)
pStages) = Vector (ShaderModule, SomeStruct PipelineShaderStageCreateInfo)
-> (Vector ShaderModule,
    Vector (SomeStruct PipelineShaderStageCreateInfo))
forall a b. Vector (a, b) -> (Vector a, Vector b)
Vector.unzip Vector (ShaderModule, SomeStruct PipelineShaderStageCreateInfo)
staged
  Shader -> io Shader
forall a. a -> io a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Shader
    { $sel:sModules:Shader :: Vector ShaderModule
sModules        = Vector ShaderModule
modules
    , $sel:sPipelineStages:Shader :: Vector (SomeStruct PipelineShaderStageCreateInfo)
sPipelineStages = Vector (SomeStruct PipelineShaderStageCreateInfo)
pStages
    }
  where
    collected :: Vector (ShaderStageFlagBits, ByteString)
collected = [(ShaderStageFlagBits, ByteString)]
-> Vector (ShaderStageFlagBits, ByteString)
forall a. [a] -> Vector a
Vector.fromList do
      (ShaderStageFlagBits
stage, Just ByteString
code) <- t (ShaderStageFlagBits, Maybe ByteString)
-> [(ShaderStageFlagBits, Maybe ByteString)]
forall a. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (t (ShaderStageFlagBits, Maybe ByteString)
 -> [(ShaderStageFlagBits, Maybe ByteString)])
-> t (ShaderStageFlagBits, Maybe ByteString)
-> [(ShaderStageFlagBits, Maybe ByteString)]
forall a b. (a -> b) -> a -> b
$ (,) (ShaderStageFlagBits
 -> Maybe ByteString -> (ShaderStageFlagBits, Maybe ByteString))
-> t ShaderStageFlagBits
-> t (Maybe ByteString -> (ShaderStageFlagBits, Maybe ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t ShaderStageFlagBits
forall (t :: * -> *). StageInfo t => t ShaderStageFlagBits
stageFlagBits t (Maybe ByteString -> (ShaderStageFlagBits, Maybe ByteString))
-> t (Maybe ByteString)
-> t (ShaderStageFlagBits, Maybe ByteString)
forall a b. t (a -> b) -> t a -> t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (Maybe ByteString)
stages
      (ShaderStageFlagBits, ByteString)
-> [(ShaderStageFlagBits, ByteString)]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ShaderStageFlagBits
stage, ByteString
code)

destroy :: MonadVulkan env io => Shader -> io ()
destroy :: forall env (io :: * -> *). MonadVulkan env io => Shader -> io ()
destroy Shader{Vector ShaderModule
$sel:sModules:Shader :: Shader -> Vector ShaderModule
sModules :: Vector ShaderModule
sModules} = do
  Device
device <- (env -> Device) -> io Device
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks env -> Device
forall a. HasVulkan a => a -> Device
getDevice
  Vector ShaderModule -> (ShaderModule -> io ()) -> io ()
forall (m :: * -> *) a b. Monad m => Vector a -> (a -> m b) -> m ()
Vector.forM_ Vector ShaderModule
sModules \ShaderModule
module_ ->
    Device
-> ShaderModule
-> ("allocator" ::: Maybe AllocationCallbacks)
-> io ()
forall (io :: * -> *).
MonadIO io =>
Device
-> ShaderModule
-> ("allocator" ::: Maybe AllocationCallbacks)
-> io ()
Vk.destroyShaderModule Device
device ShaderModule
module_ "allocator" ::: Maybe AllocationCallbacks
forall a. Maybe a
Nothing

-- * Specialization constants

withSpecialization
  :: ( Specialization spec
     , MonadUnliftIO m
     )
  => spec
  -> (Maybe Vk.SpecializationInfo -> m a)
  -> m a
withSpecialization :: forall spec (m :: * -> *) a.
(Specialization spec, MonadUnliftIO m) =>
spec -> (Maybe SpecializationInfo -> m a) -> m a
withSpecialization spec
spec Maybe SpecializationInfo -> m a
action =
  case Vector SpecializationMapEntry
mapEntries of
    [] ->
      Maybe SpecializationInfo -> m a
action Maybe SpecializationInfo
forall a. Maybe a
Nothing
    Vector SpecializationMapEntry
_some ->
      ((forall a. m a -> IO a) -> IO a) -> m a
forall b. ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO \forall a. m a -> IO a
run ->
        Vector Word32 -> (Ptr Word32 -> IO a) -> IO a
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
Storable.unsafeWith Vector Word32
specData \Ptr Word32
specPtr ->
          m a -> IO a
forall a. m a -> IO a
run (m a -> IO a)
-> (Maybe SpecializationInfo -> m a)
-> Maybe SpecializationInfo
-> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe SpecializationInfo -> m a
action (Maybe SpecializationInfo -> IO a)
-> Maybe SpecializationInfo -> IO a
forall a b. (a -> b) -> a -> b
$ SpecializationInfo -> Maybe SpecializationInfo
forall a. a -> Maybe a
Just Vk.SpecializationInfo
            { $sel:mapEntries:SpecializationInfo :: Vector SpecializationMapEntry
mapEntries = Vector SpecializationMapEntry
mapEntries
            , $sel:dataSize:SpecializationInfo :: Word64
dataSize   = Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word64) -> Int -> Word64
forall a b. (a -> b) -> a -> b
$ Vector Word32 -> Int
forall a. Storable a => Vector a -> Int
Storable.length Vector Word32
specData Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
4
            , $sel:data':SpecializationInfo :: Ptr ()
data'      = forall a b. Ptr a -> Ptr b
Foreign.castPtr @_ @() Ptr Word32
specPtr
            }
  where
    specData :: Vector Word32
specData = [Word32] -> Vector Word32
forall a. Storable a => [a] -> Vector a
Storable.fromList ([Word32] -> Vector Word32) -> [Word32] -> Vector Word32
forall a b. (a -> b) -> a -> b
$ spec -> [Word32]
forall a. Specialization a => a -> [Word32]
specializationData spec
spec

    mapEntries :: Vector SpecializationMapEntry
mapEntries = (Int -> Word32 -> SpecializationMapEntry)
-> Vector Word32 -> Vector SpecializationMapEntry
forall a b. (Int -> a -> b) -> Vector a -> Vector b
Vector.imap
      (\Int
ix Word32
_data -> Vk.SpecializationMapEntry
          { $sel:constantID:SpecializationMapEntry :: Word32
constantID = Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ix
          , $sel:offset:SpecializationMapEntry :: Word32
offset     = Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word32) -> Int -> Word32
forall a b. (a -> b) -> a -> b
$ Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
4
          , $sel:size:SpecializationMapEntry :: Word64
size       = Word64
4
          }
      )
      (Vector Word32 -> Vector Word32
forall (v :: * -> *) a (w :: * -> *).
(Vector v a, Vector w a) =>
v a -> w a
Vector.convert Vector Word32
specData)

class Specialization a where
  -- XXX: abusing the fact that most scalars (sans double) are 4-wide
  specializationData :: a -> [Word32]

instance Specialization () where
  specializationData :: () -> [Word32]
specializationData = [Word32] -> () -> [Word32]
forall a b. a -> b -> a
const []

instance Specialization [Word32] where
  specializationData :: [Word32] -> [Word32]
specializationData = [Word32] -> [Word32]
forall a. a -> a
id

{- |
  The constant_id can only be applied to a scalar *int*, a scalar *float* or a scalar *bool*.
  (https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt)

  XXX: Apparently it is possible to pass uints and doubles too.
-}

instance Specialization Word32 where
  specializationData :: Word32 -> [Word32]
specializationData Word32
x = [Word32 -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData Word32
x]

instance Specialization Int32 where
  specializationData :: Int32 -> [Word32]
specializationData Int32
x = [Int32 -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData Int32
x]

instance Specialization Float where
  specializationData :: Float -> [Word32]
specializationData Float
x = [Float -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData Float
x]

instance Specialization Bool where
  specializationData :: Bool -> [Word32]
specializationData Bool
x = [Bool -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData Bool
x]

class SpecializationConst a where
  packConstData :: a -> Word32

instance SpecializationConst Word32 where
  packConstData :: Word32 -> Word32
packConstData = Word32 -> Word32
forall a. a -> a
id

instance SpecializationConst Int32 where
  packConstData :: Int32 -> Word32
packConstData = Int32 -> Word32
forall a b. a -> b
unsafeCoerce

instance SpecializationConst Float where
  packConstData :: Float -> Word32
packConstData = Float -> Word32
forall a b. a -> b
unsafeCoerce

instance SpecializationConst Bool where
  packConstData :: Bool -> Word32
packConstData = Word32 -> Word32 -> Bool -> Word32
forall a. a -> a -> Bool -> a
bool Word32
0 Word32
1

instance
  ( SpecializationConst a
  , SpecializationConst b
  ) => Specialization (a, b) where
  specializationData :: (a, b) -> [Word32]
specializationData (a
a, b
b) =
    [ a -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData a
a
    , b -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData b
b
    ]

instance
  ( SpecializationConst a
  , SpecializationConst b
  , SpecializationConst c
  ) => Specialization (a, b, c) where
  specializationData :: (a, b, c) -> [Word32]
specializationData (a
a, b
b, c
c) =
    [ a -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData a
a
    , b -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData b
b
    , c -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData c
c
    ]

instance
  ( SpecializationConst a
  , SpecializationConst b
  , SpecializationConst c
  , SpecializationConst d
  ) => Specialization (a, b, c, d) where
  specializationData :: (a, b, c, d) -> [Word32]
specializationData (a
a, b
b, c
c, d
d) =
    [ a -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData a
a
    , b -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData b
b
    , c -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData c
c
    , d -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData d
d
    ]

instance
  ( SpecializationConst a
  , SpecializationConst b
  , SpecializationConst c
  , SpecializationConst d
  , SpecializationConst e
  ) => Specialization (a, b, c, d, e) where
  specializationData :: (a, b, c, d, e) -> [Word32]
specializationData (a
a, b
b, c
c, d
d, e
e) =
    [ a -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData a
a
    , b -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData b
b
    , c -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData c
c
    , d -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData d
d
    , e -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData e
e
    ]

instance
  ( SpecializationConst a
  , SpecializationConst b
  , SpecializationConst c
  , SpecializationConst d
  , SpecializationConst e
  , SpecializationConst f
  ) => Specialization (a, b, c, d, e, f) where
  specializationData :: (a, b, c, d, e, f) -> [Word32]
specializationData (a
a, b
b, c
c, d
d, e
e, f
f) =
    [ a -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData a
a
    , b -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData b
b
    , c -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData c
c
    , d -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData d
d
    , e -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData e
e
    , f -> Word32
forall a. SpecializationConst a => a -> Word32
packConstData f
f
    ]