packages feed

keid-render-basic-0.2.0.0: src/Render/ShadowMap/Pipeline.hs

module Render.ShadowMap.Pipeline
  ( Settings(..)
  , defaults

  , Pipeline
  , allocate

  , Config
  , config

  , stageCode
  , stageSpirv
  ) where

import RIO

import Control.Monad.Trans.Resource (ResourceT)
import Data.Tagged (Tagged(..))
import Geomancy (Transform)
import Vulkan.Core10 qualified as Vk

import Engine.Vulkan.Pipeline.Graphics qualified as Graphics
import Engine.Vulkan.Types (DsLayoutBindings, HasVulkan)
import Render.Code (compileStages)
import Render.DescSets.Sun (Sun)
import Render.ShadowMap.Code qualified as Code
import Render.Target (HasRendering)
import Resource.Model (Vertex3d)

type Pipeline = Graphics.Pipeline '[Sun] (Vertex3d ()) Transform
type Config = Graphics.Configure Pipeline
type instance Graphics.Specialization Pipeline = ()

data Settings = Settings
  { cull      :: Vk.CullModeFlagBits
  , depthBias :: Maybe (Float, Float)
  }

defaults :: Settings
defaults = Settings
  { cull      = Vk.CULL_MODE_FRONT_BIT
  , depthBias = Just (-2.0, -2.5)
  }

allocate
  :: ( HasVulkan env
     , HasRendering target
     )
  => Tagged Sun DsLayoutBindings
  -> target
  -> Settings
  -> ResourceT (RIO env) Pipeline
allocate tset0 target settings = do
  (_, p) <- Graphics.allocate
    Nothing
    (config tset0 settings)
    target
  pure p

config :: Tagged Sun DsLayoutBindings -> Settings -> Config
config (Tagged set0) Settings{..} = Graphics.baseConfig
  { Graphics.cDescLayouts  = Tagged @'[Sun] [set0]
  , Graphics.cStages       = stageSpirv
  , Graphics.cVertexInput  = Graphics.vertexInput @Pipeline
  , Graphics.cDepthBias    = depthBias
  , Graphics.cCull         = cull
  }

stageSpirv :: Graphics.StageSpirv
stageSpirv = $(compileStages $ Graphics.vertexOnly Code.vert)

stageCode :: Graphics.StageCode
stageCode = Graphics.vertexOnly Code.vert