keid-core-0.2.0.0: src/Render/Target.hs
{-# LANGUAGE OverloadedLists #-}
module Render.Target
( HasRendering(..)
, RenderingFormats(..)
, pipelineRendering
, RenderTarget(..)
, render
, setViewportScissor
, colorAttachment
, resolvedColor
, depthAttachment
, viewMaskFor
) where
import RIO
import Data.Bits (shiftL)
import UnliftIO.Resource (MonadResource)
import Vulkan.Core10 qualified as Vk
import Vulkan.Core12 qualified as Vk12
import Vulkan.Core13 qualified as Vk13
import Vulkan.CStruct.Extends (SomeStruct(..))
import Vulkan.NamedType ((:::))
import Engine.Vulkan.Types (HasSwapchain, HasVulkan)
import Render.Graph (Graph, HasSliceRegistry, Setup)
import Render.Graph qualified as Graph
class HasRendering a where
getRenderingFormats :: a -> RenderingFormats
getRenderingInfo :: a -> "image index" ::: Word32 -> Vk13.RenderingInfo '[]
getRenderArea :: a -> Vk.Rect2D
data RenderingFormats = RenderingFormats
{ rfColor :: Vector Vk.Format
, rfDepth :: Vk.Format
, rfStencil :: Vk.Format
, rfSamples :: Vk.SampleCountFlagBits
, rfViewMask :: Word32
}
deriving (Eq, Show)
pipelineRendering :: RenderingFormats -> Vk13.PipelineRenderingCreateInfo
pipelineRendering RenderingFormats{..} = Vk13.PipelineRenderingCreateInfo
{ viewMask = rfViewMask
, colorAttachmentFormats = rfColor
, depthAttachmentFormat = rfDepth
, stencilAttachmentFormat = rfStencil
}
class RenderTarget a where
updateTarget
:: ( HasLogFunc env
, HasSliceRegistry env
, HasSwapchain swapchain
, HasVulkan env
, MonadResource (RIO env)
)
=> swapchain
-> a
-> RIO env a
updateTarget = const pure
refcountTarget
:: MonadResource (RIO env)
=> a
-> RIO env ()
render
:: HasRendering target
=> Text
-> target
-> (i -> Setup o)
-> (Vk.CommandBuffer -> RIO env ())
-> Graph env i o
render name target setup draw =
Graph.passIndexed name setup \imageIndex cb ->
Vk13.cmdUseRendering cb (getRenderingInfo target imageIndex) do
setViewportScissor cb (getRenderArea target).extent target
draw cb
setViewportScissor
:: ( HasRendering target
, MonadIO io
)
=> Vk.CommandBuffer
-> Vk.Extent2D
-> target
-> io ()
setViewportScissor cb Vk.Extent2D{width, height} target = do
Vk.cmdSetViewport
cb
0
[ Vk.Viewport
{ x = 0
, y = 0
, width = realToFrac width
, height = realToFrac height
, minDepth = 0
, maxDepth = 1
}
]
Vk.cmdSetScissor
cb
0
[ getRenderArea target
]
colorAttachment
:: Vk.ImageView
-> Vk.AttachmentLoadOp
-> Vk.AttachmentStoreOp
-> Vk.ClearValue
-> SomeStruct Vk13.RenderingAttachmentInfo
colorAttachment attachmentView loadOp storeOp clear =
SomeStruct Vk13.RenderingAttachmentInfo
{ next = ()
, imageView = attachmentView
, imageLayout = Vk.IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
, resolveMode = Vk12.RESOLVE_MODE_NONE
, resolveImageView = Vk.NULL_HANDLE
, resolveImageLayout = Vk.IMAGE_LAYOUT_UNDEFINED
, loadOp = loadOp
, storeOp = storeOp
, clearValue = clear
}
resolvedColor
:: "multisampled" ::: Vk.ImageView
-> "resolved" ::: Vk.ImageView
-> Vk.ClearValue
-> SomeStruct Vk13.RenderingAttachmentInfo
resolvedColor multisampled resolved clear =
SomeStruct Vk13.RenderingAttachmentInfo
{ next = ()
, imageView = multisampled
, imageLayout = Vk.IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
, resolveMode = Vk12.RESOLVE_MODE_AVERAGE_BIT
, resolveImageView = resolved
, resolveImageLayout = Vk.IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
, loadOp = Vk.ATTACHMENT_LOAD_OP_CLEAR
, storeOp = Vk.ATTACHMENT_STORE_OP_DONT_CARE
, clearValue = clear
}
depthAttachment
:: Vk.ImageView
-> Vk.AttachmentStoreOp
-> "clear depth" ::: Float
-> SomeStruct Vk13.RenderingAttachmentInfo
depthAttachment attachmentView storeOp clearDepth =
SomeStruct Vk13.RenderingAttachmentInfo
{ next = ()
, imageView = attachmentView
, imageLayout = Vk.IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL
, resolveMode = Vk12.RESOLVE_MODE_NONE
, resolveImageView = Vk.NULL_HANDLE
, resolveImageLayout = Vk.IMAGE_LAYOUT_UNDEFINED
, loadOp = Vk.ATTACHMENT_LOAD_OP_CLEAR
, storeOp = storeOp
, clearValue = Vk.DepthStencil (Vk.ClearDepthStencilValue clearDepth 0)
}
viewMaskFor :: "views" ::: Word32 -> Word32
viewMaskFor views
| views == 0 = 0
| views > 32 = error $ "Too many views for a view mask: " <> show views
| views == 32 = maxBound
| otherwise = (1 `shiftL` fromIntegral views) - 1