keid-core-0.2.0.0: src/Resource/Image/Downsample.hs
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedRecordDot #-}
module Resource.Image.Downsample
( Pyramid(..)
, allocate
, tracked
, texture
, generate
) where
import RIO
import Control.Arrow (arr)
import Control.Monad.Trans.Resource qualified as Resource
import Data.Bits (shiftR)
import RIO.Vector qualified as Vector
import RIO.Vector.Partial ((!), (//))
import RIO.Vector.Partial qualified as Partial
import Vulkan.Core10 qualified as Vk
import Vulkan.NamedType ((:::))
import Vulkan.Utils.RefCounted (RefCounted)
import Vulkan.Utils.RefCounted qualified as RefCounted
import Vulkan.Zero (zero)
import Engine.Types.RefCounted qualified as RefCounted
import Engine.Vulkan.Types (MonadVulkan)
import Render.Graph (Graph, HasSliceRegistry, Image, Tracked)
import Render.Graph qualified as Graph
import Render.Target (RenderTarget(..))
import Resource.Image (AllocatedImage(..), DstImage(..))
import Resource.Image qualified as Image
import Resource.Region qualified as Region
import Resource.Texture (Texture(..))
data Pyramid (tag :: k) = Pyramid
{ pyramidLabel :: Text
, pyramidSource :: AllocatedImage
, pyramidImage :: AllocatedImage
, pyramidMips :: Tracked tag
, pyramidRelease :: RefCounted
}
instance RenderTarget (Pyramid tag) where
refcountTarget = RefCounted.resourceTRefCount . pyramidRelease
allocate
:: ( Resource.MonadResource m
, MonadVulkan env m
, HasLogFunc env
, HasSliceRegistry env
)
=> Text
-> "max mips" ::: Word32
-> "source" ::: AllocatedImage
-> m (Pyramid tag)
allocate pyramidLabel maxMips pyramidSource = do
(pyramidRelease, (pyramidImage, pyramidMips)) <- RefCounted.wrapped $ Region.run do
Region.logDebug
("Allocating mip pyramid " <> display pyramidLabel)
("Releasing mip pyramid " <> display pyramidLabel)
DstImage image <- Image.allocateDstMipFor (Just pyramidLabel) maxMips pyramidSource
mips <- Graph.trackMips pyramidLabel image
pure (image, mips)
pure Pyramid{..}
tracked :: Pyramid tag -> Tracked tag
tracked = pyramidMips
texture :: Pyramid tag -> Texture layers
texture Pyramid{pyramidImage} = Texture
{ tFormat = pyramidImage.aiFormat
, tMipLevels = pyramidImage.aiImageRange.levelCount
, tLayers = pyramidImage.aiImageRange.layerCount
, tAllocatedImage = pyramidImage
}
generate :: Pyramid tag -> Graph env (Image source, Image tag) (Image tag)
generate Pyramid{pyramidLabel, pyramidSource, pyramidImage} =
arr (second $ Vector.fromList . toList . Graph.splitSlices) >>>
copy >>>
foldr (\mip next -> blit mip >>> next) (arr id) ([1 .. levels - 1] :: [Word32]) >>>
arr (Graph.joinSlices . fromVector)
where
levels = pyramidImage.aiImageRange.levelCount
layers = pyramidImage.aiImageRange.layerCount
aspect = pyramidImage.aiImageRange.aspectMask
fromVector :: Vector (Image t) -> NonEmpty (Image t)
fromVector mips = Partial.head mips :| toList (Partial.tail mips)
copy =
Graph.pass (pyramidLabel <> ".copy")
( \(source, mips) -> do
Graph.reading (Graph.transferSrc source)
base <- Graph.transferDst (mips ! 0)
pure $ mips // [(0, base)]
)
\cb ->
Vk.cmdCopyImage
cb
pyramidSource.aiImage
Vk.IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
pyramidImage.aiImage
Vk.IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
[ Vk.ImageCopy
{ srcSubresource = mipLayers 0
, srcOffset = zero
, dstSubresource = mipLayers 0
, dstOffset = zero
, extent = pyramidSource.aiExtent
}
]
blit mip =
Graph.pass (pyramidLabel <> ".mip" <> textDisplay mip)
( \mips -> do
Graph.reading (Graph.transferSrc $ mips ! fromIntegral (mip - 1))
next <- Graph.transferDst (mips ! fromIntegral mip)
pure $ mips // [(fromIntegral mip, next)]
)
\cb ->
Vk.cmdBlitImage
cb
pyramidImage.aiImage
Vk.IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
pyramidImage.aiImage
Vk.IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
[ Vk.ImageBlit
{ srcSubresource = mipLayers (mip - 1)
, srcOffsets = (zero, mipCorner (mip - 1))
, dstSubresource = mipLayers mip
, dstOffsets = (zero, mipCorner mip)
}
]
Vk.FILTER_LINEAR
mipLayers mip = Vk.ImageSubresourceLayers
{ aspectMask = aspect
, mipLevel = mip
, baseArrayLayer = 0
, layerCount = layers
}
mipCorner mip = Vk.Offset3D
(max 1 $ fromIntegral pyramidImage.aiExtent.width `shiftR` fromIntegral mip)
(max 1 $ fromIntegral pyramidImage.aiExtent.height `shiftR` fromIntegral mip)
1