{-# LANGUAGE DeriveAnyClass #-}

module Render.Samplers
  ( Collection(..)
  , allocate
  , indices

  , Params
  , params
  , create
  , destroy
  ) where

import RIO

import Control.Monad.Trans.Resource qualified as Resource
import GHC.Generics (Generic1)
import Vulkan.Core10 qualified as Vk
import Vulkan.NamedType ((:::))
import Vulkan.Zero (zero)

import Engine.Vulkan.Types (HasVulkan(..), MonadVulkan)
import Resource.Collection qualified as Collection
import Resource.Collection (Generically1(..))

data Collection a = Collection
  { forall a. Collection a -> a
linearMipRepeat  :: a -- 0
  , forall a. Collection a -> a
linearMip        :: a -- 1
  , forall a. Collection a -> a
linearRepeat     :: a -- 2
  , forall a. Collection a -> a
linear           :: a -- 3
  , forall a. Collection a -> a
nearestMipRepeat :: a -- 4
  , forall a. Collection a -> a
nearestMip       :: a -- 5
  , forall a. Collection a -> a
nearestRepeat    :: a -- 6
  , forall a. Collection a -> a
nearest          :: a -- 7
  }
  deriving stock (Int -> Collection a -> ShowS
[Collection a] -> ShowS
Collection a -> String
(Int -> Collection a -> ShowS)
-> (Collection a -> String)
-> ([Collection a] -> ShowS)
-> Show (Collection a)
forall a. Show a => Int -> Collection a -> ShowS
forall a. Show a => [Collection a] -> ShowS
forall a. Show a => Collection a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Collection a] -> ShowS
$cshowList :: forall a. Show a => [Collection a] -> ShowS
show :: Collection a -> String
$cshow :: forall a. Show a => Collection a -> String
showsPrec :: Int -> Collection a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Collection a -> ShowS
Show, (forall a b. (a -> b) -> Collection a -> Collection b)
-> (forall a b. a -> Collection b -> Collection a)
-> Functor Collection
forall a b. a -> Collection b -> Collection a
forall a b. (a -> b) -> Collection a -> Collection b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Collection b -> Collection a
$c<$ :: forall a b. a -> Collection b -> Collection a
fmap :: forall a b. (a -> b) -> Collection a -> Collection b
$cfmap :: forall a b. (a -> b) -> Collection a -> Collection b
Functor, (forall m. Monoid m => Collection m -> m)
-> (forall m a. Monoid m => (a -> m) -> Collection a -> m)
-> (forall m a. Monoid m => (a -> m) -> Collection a -> m)
-> (forall a b. (a -> b -> b) -> b -> Collection a -> b)
-> (forall a b. (a -> b -> b) -> b -> Collection a -> b)
-> (forall b a. (b -> a -> b) -> b -> Collection a -> b)
-> (forall b a. (b -> a -> b) -> b -> Collection a -> b)
-> (forall a. (a -> a -> a) -> Collection a -> a)
-> (forall a. (a -> a -> a) -> Collection a -> a)
-> (forall a. Collection a -> [a])
-> (forall a. Collection a -> Bool)
-> (forall a. Collection a -> Int)
-> (forall a. Eq a => a -> Collection a -> Bool)
-> (forall a. Ord a => Collection a -> a)
-> (forall a. Ord a => Collection a -> a)
-> (forall a. Num a => Collection a -> a)
-> (forall a. Num a => Collection a -> a)
-> Foldable Collection
forall a. Eq a => a -> Collection a -> Bool
forall a. Num a => Collection a -> a
forall a. Ord a => Collection a -> a
forall m. Monoid m => Collection m -> m
forall a. Collection a -> Bool
forall a. Collection a -> Int
forall a. Collection a -> [a]
forall a. (a -> a -> a) -> Collection a -> a
forall m a. Monoid m => (a -> m) -> Collection a -> m
forall b a. (b -> a -> b) -> b -> Collection a -> b
forall a b. (a -> b -> b) -> b -> Collection a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Collection a -> a
$cproduct :: forall a. Num a => Collection a -> a
sum :: forall a. Num a => Collection a -> a
$csum :: forall a. Num a => Collection a -> a
minimum :: forall a. Ord a => Collection a -> a
$cminimum :: forall a. Ord a => Collection a -> a
maximum :: forall a. Ord a => Collection a -> a
$cmaximum :: forall a. Ord a => Collection a -> a
elem :: forall a. Eq a => a -> Collection a -> Bool
$celem :: forall a. Eq a => a -> Collection a -> Bool
length :: forall a. Collection a -> Int
$clength :: forall a. Collection a -> Int
null :: forall a. Collection a -> Bool
$cnull :: forall a. Collection a -> Bool
toList :: forall a. Collection a -> [a]
$ctoList :: forall a. Collection a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Collection a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Collection a -> a
foldr1 :: forall a. (a -> a -> a) -> Collection a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Collection a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Collection a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Collection a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Collection a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Collection a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Collection a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Collection a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Collection a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Collection a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Collection a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Collection a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Collection a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Collection a -> m
fold :: forall m. Monoid m => Collection m -> m
$cfold :: forall m. Monoid m => Collection m -> m
Foldable, Functor Collection
Foldable Collection
Functor Collection
-> Foldable Collection
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Collection a -> f (Collection b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Collection (f a) -> f (Collection a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Collection a -> m (Collection b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Collection (m a) -> m (Collection a))
-> Traversable Collection
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
Collection (m a) -> m (Collection a)
forall (f :: * -> *) a.
Applicative f =>
Collection (f a) -> f (Collection a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Collection a -> m (Collection b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Collection a -> f (Collection b)
sequence :: forall (m :: * -> *) a.
Monad m =>
Collection (m a) -> m (Collection a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
Collection (m a) -> m (Collection a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Collection a -> m (Collection b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Collection a -> m (Collection b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Collection (f a) -> f (Collection a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Collection (f a) -> f (Collection a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Collection a -> f (Collection b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Collection a -> f (Collection b)
Traversable, (forall a. Collection a -> Rep1 Collection a)
-> (forall a. Rep1 Collection a -> Collection a)
-> Generic1 Collection
forall a. Rep1 Collection a -> Collection a
forall a. Collection a -> Rep1 Collection a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall a. Rep1 Collection a -> Collection a
$cfrom1 :: forall a. Collection a -> Rep1 Collection a
Generic1)
  deriving Functor Collection
Functor Collection
-> (forall a. a -> Collection a)
-> (forall a b.
    Collection (a -> b) -> Collection a -> Collection b)
-> (forall a b c.
    (a -> b -> c) -> Collection a -> Collection b -> Collection c)
-> (forall a b. Collection a -> Collection b -> Collection b)
-> (forall a b. Collection a -> Collection b -> Collection a)
-> Applicative Collection
forall a. a -> Collection a
forall a b. Collection a -> Collection b -> Collection a
forall a b. Collection a -> Collection b -> Collection b
forall a b. Collection (a -> b) -> Collection a -> Collection b
forall a b c.
(a -> b -> c) -> Collection a -> Collection b -> Collection c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Collection a -> Collection b -> Collection a
$c<* :: forall a b. Collection a -> Collection b -> Collection a
*> :: forall a b. Collection a -> Collection b -> Collection b
$c*> :: forall a b. Collection a -> Collection b -> Collection b
liftA2 :: forall a b c.
(a -> b -> c) -> Collection a -> Collection b -> Collection c
$cliftA2 :: forall a b c.
(a -> b -> c) -> Collection a -> Collection b -> Collection c
<*> :: forall a b. Collection (a -> b) -> Collection a -> Collection b
$c<*> :: forall a b. Collection (a -> b) -> Collection a -> Collection b
pure :: forall a. a -> Collection a
$cpure :: forall a. a -> Collection a
Applicative via Generically1 Collection

type Params = (Vk.Filter, "LOD clamp" ::: Float, Vk.SamplerAddressMode)

params :: Collection Params
params :: Collection Params
params = Collection :: forall a. a -> a -> a -> a -> a -> a -> a -> a -> Collection a
Collection
  { $sel:linearMipRepeat:Collection :: Params
linearMipRepeat  = (Filter
Vk.FILTER_LINEAR,  Float
Vk.LOD_CLAMP_NONE, SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_REPEAT)          -- 0
  , $sel:linearMip:Collection :: Params
linearMip        = (Filter
Vk.FILTER_LINEAR,  Float
Vk.LOD_CLAMP_NONE, SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) -- 1
  , $sel:linearRepeat:Collection :: Params
linearRepeat     = (Filter
Vk.FILTER_LINEAR,  Float
0,                 SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_REPEAT)          -- 2
  , $sel:linear:Collection :: Params
linear           = (Filter
Vk.FILTER_LINEAR,  Float
0,                 SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) -- 3
  , $sel:nearestMipRepeat:Collection :: Params
nearestMipRepeat = (Filter
Vk.FILTER_NEAREST, Float
Vk.LOD_CLAMP_NONE, SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_REPEAT)          -- 4
  , $sel:nearestMip:Collection :: Params
nearestMip       = (Filter
Vk.FILTER_NEAREST, Float
Vk.LOD_CLAMP_NONE, SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) -- 5
  , $sel:nearestRepeat:Collection :: Params
nearestRepeat    = (Filter
Vk.FILTER_NEAREST, Float
0,                 SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_REPEAT)          -- 6
  , $sel:nearest:Collection :: Params
nearest          = (Filter
Vk.FILTER_NEAREST, Float
0,                 SamplerAddressMode
Vk.SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) -- 7
  }

indices :: Collection Int32
indices :: Collection Int32
indices = ((Int32, Params) -> Int32)
-> Collection (Int32, Params) -> Collection Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int32, Params) -> Int32
forall a b. (a, b) -> a
fst (Collection (Int32, Params) -> Collection Int32)
-> Collection (Int32, Params) -> Collection Int32
forall a b. (a -> b) -> a -> b
$ Collection Params -> Collection (Int32, Params)
forall (t :: * -> *) ix a.
(Traversable t, Num ix) =>
t a -> t (ix, a)
Collection.enumerate Collection Params
params

allocate
  :: ( MonadVulkan env m
     , Resource.MonadResource m
     )
  => "max anisotropy" ::: Float
  -> m (Resource.ReleaseKey, Collection Vk.Sampler)
allocate :: forall env (m :: * -> *).
(MonadVulkan env m, MonadResource m) =>
Float -> m (ReleaseKey, Collection Sampler)
allocate Float
maxAnisotropy = do
  env
context <- m env
forall r (m :: * -> *). MonadReader r m => m r
ask
  IO (Collection Sampler)
-> (Collection Sampler -> IO ())
-> m (ReleaseKey, Collection Sampler)
forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
Resource.allocate
    (Collection Params
-> (Params -> IO Sampler) -> IO (Collection Sampler)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for Collection Params
params ((Params -> IO Sampler) -> IO (Collection Sampler))
-> (Params -> IO Sampler) -> IO (Collection Sampler)
forall a b. (a -> b) -> a -> b
$ env -> Float -> Params -> IO Sampler
forall (io :: * -> *) context.
(MonadIO io, HasVulkan context) =>
context -> Float -> Params -> io Sampler
create env
context Float
maxAnisotropy)
    ((Sampler -> IO ()) -> Collection Sampler -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((Sampler -> IO ()) -> Collection Sampler -> IO ())
-> (Sampler -> IO ()) -> Collection Sampler -> IO ()
forall a b. (a -> b) -> a -> b
$ env -> Sampler -> IO ()
forall (io :: * -> *) context.
(MonadIO io, HasVulkan context) =>
context -> Sampler -> io ()
destroy env
context)

create
  :: (MonadIO io, HasVulkan context)
  => context
  -> "max anisotropy" ::: Float
  -> Params
  -> io Vk.Sampler
create :: forall (io :: * -> *) context.
(MonadIO io, HasVulkan context) =>
context -> Float -> Params -> io Sampler
create context
context Float
maxAnisotropy (Filter
filt, Float
mips, SamplerAddressMode
reps) =
  Device
-> SamplerCreateInfo '[]
-> ("allocator" ::: Maybe AllocationCallbacks)
-> io Sampler
forall (a :: [*]) (io :: * -> *).
(Extendss SamplerCreateInfo a, PokeChain a, MonadIO io) =>
Device
-> SamplerCreateInfo a
-> ("allocator" ::: Maybe AllocationCallbacks)
-> io Sampler
Vk.createSampler (context -> Device
forall a. HasVulkan a => a -> Device
getDevice context
context) SamplerCreateInfo '[]
samplerCI "allocator" ::: Maybe AllocationCallbacks
forall a. Maybe a
Nothing
  where
    samplerCI :: SamplerCreateInfo '[]
samplerCI = SamplerCreateInfo '[]
forall a. Zero a => a
zero
      { $sel:magFilter:SamplerCreateInfo :: Filter
Vk.magFilter               = Filter
filt
      , $sel:minFilter:SamplerCreateInfo :: Filter
Vk.minFilter               = Filter
filt
      , $sel:addressModeU:SamplerCreateInfo :: SamplerAddressMode
Vk.addressModeU            = SamplerAddressMode
reps
      , $sel:addressModeV:SamplerCreateInfo :: SamplerAddressMode
Vk.addressModeV            = SamplerAddressMode
reps
      , $sel:addressModeW:SamplerCreateInfo :: SamplerAddressMode
Vk.addressModeW            = SamplerAddressMode
reps
      , $sel:anisotropyEnable:SamplerCreateInfo :: Bool
Vk.anisotropyEnable        = Float
maxAnisotropy Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
> Float
1
      , $sel:maxAnisotropy:SamplerCreateInfo :: Float
Vk.maxAnisotropy           = Float
maxAnisotropy
      , $sel:borderColor:SamplerCreateInfo :: BorderColor
Vk.borderColor             = BorderColor
Vk.BORDER_COLOR_INT_OPAQUE_BLACK
      , $sel:unnormalizedCoordinates:SamplerCreateInfo :: Bool
Vk.unnormalizedCoordinates = Bool
False
      , $sel:compareEnable:SamplerCreateInfo :: Bool
Vk.compareEnable           = Bool
False
      , $sel:compareOp:SamplerCreateInfo :: CompareOp
Vk.compareOp               = CompareOp
Vk.COMPARE_OP_ALWAYS
      , $sel:mipmapMode:SamplerCreateInfo :: SamplerMipmapMode
Vk.mipmapMode              = SamplerMipmapMode
Vk.SAMPLER_MIPMAP_MODE_LINEAR
      , $sel:mipLodBias:SamplerCreateInfo :: Float
Vk.mipLodBias              = Float
0
      , $sel:minLod:SamplerCreateInfo :: Float
Vk.minLod                  = Float
0
      , $sel:maxLod:SamplerCreateInfo :: Float
Vk.maxLod                  = Float
mips
      }

destroy :: (MonadIO io, HasVulkan context) => context -> Vk.Sampler -> io ()
destroy :: forall (io :: * -> *) context.
(MonadIO io, HasVulkan context) =>
context -> Sampler -> io ()
destroy context
context Sampler
sampler =
  Device
-> Sampler -> ("allocator" ::: Maybe AllocationCallbacks) -> io ()
forall (io :: * -> *).
MonadIO io =>
Device
-> Sampler -> ("allocator" ::: Maybe AllocationCallbacks) -> io ()
Vk.destroySampler (context -> Device
forall a. HasVulkan a => a -> Device
getDevice context
context) Sampler
sampler "allocator" ::: Maybe AllocationCallbacks
forall a. Maybe a
Nothing