split up and clean

This commit is contained in:
nek0 2023-04-29 18:41:59 +02:00
parent cde6d15245
commit b3a1545fd2
2 changed files with 43 additions and 46 deletions

View file

@ -5,7 +5,6 @@
module CommandBuffer where
import qualified Control.Concurrent.STM as STM
import Control.Exception (bracket)
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Reader
@ -173,7 +172,6 @@ recordCommandBuffer
-> Vk.Framebuffer
-> V2 CInt
-> Vk.Pipeline
-> VMA.Allocator
-> GPUSceneData
-> FrameData
-> Int
@ -184,12 +182,13 @@ recordCommandBuffer
frameBuffer
(V2 width height)
graphicsPipeline
allocator
sceneParameters
frame
frameNumber
= do
liftIO $ pokeData frame frameNumber sceneParameters (width, height)
let commandBufferBeginInfo = Vk.zero
{ Vk.flags = Vk.COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
, Vk.inheritanceInfo = Nothing
@ -221,62 +220,21 @@ recordCommandBuffer
liftIO $
prepareRecording width height commandBuffer renderPass frameBuffer graphicsPipeline
let framed = fromIntegral frameNumber / 120
ambient = normalize $ V4 (sin framed) 0 (cos framed) 1
-- ambient = V4 1 0 1 1
ambientParams = sceneParameters
{ ambientColor = ambient
}
liftIO $ do
let scenePointer = VMA.mappedData (bufferInfo (frameSceneBuffer frame))
liftIO $ poke
(castPtr scenePointer)
ambientParams
renderObjects <- (liftIO . STM.atomically . STM.readTMVar) =<< asks renderables
meshMap <- (liftIO . STM.atomically . STM.readTMVar) =<< asks meshLibrary
V.mapM_
(\(index, RenderObject meshID materialID modelMatrix) -> do
let camPosition = V3
(-10 * sin (fromIntegral frameNumber / 90))
(-2)
(-10 * cos (fromIntegral frameNumber / 90))
camCenter = V3 0 0 0
camUp = V3 0 1 0
camView = lookAt camPosition camCenter camUp
camProjection = perspective
(pi/4)
(fromIntegral width / fromIntegral height)
0.1
200
materialMap <- (liftIO . STM.atomically . STM.readTMVar) =<< asks materialLibrary
let cameraData = GPUCameraData
{ view = transpose camView
, projection = transpose camProjection
, viewProjection = transpose $ camProjection !*! camView
}
constants = MeshPushConstants
let constants = MeshPushConstants
{ meshPushData = V4 0 0 0 0
, meshRenderMatrix = transpose modelMatrix
}
mesh = meshMap M.! meshID
material = materialMap M.! materialID
liftIO $ VMA.withMappedMemory
allocator
(bufferAllocation $ frameCameraBuffer frame)
bracket
$ \memoryPointer ->
liftIO $ poke (castPtr memoryPointer) cameraData
Vk.cmdBindPipeline commandBuffer Vk.PIPELINE_BIND_POINT_GRAPHICS (materialPipeline material)
Vk.cmdBindDescriptorSets
@ -350,3 +308,43 @@ prepareRecording width height commandBuffer renderPass frameBuffer graphicsPipel
Vk.cmdSetViewport commandBuffer 0 (V.singleton viewport)
Vk.cmdSetScissor commandBuffer 0 (V.singleton scissor)
pokeData
:: FrameData
-> Int
-> GPUSceneData
-> (CInt, CInt)
-> IO ()
pokeData frame frameNumber sceneParameters (width, height) = do
let scenePointer = VMA.mappedData (bufferInfo $ frameSceneBuffer frame)
memoryPointer = VMA.mappedData (bufferInfo $ frameCameraBuffer frame)
framed = fromIntegral frameNumber / 120
ambient = normalize $ V4 (sin framed) 0 (cos framed) 1
-- ambient = V4 1 0 1 1
ambientParams = sceneParameters
{ ambientColor = ambient
}
camPosition = V3
(-10 * sin (fromIntegral frameNumber / 90))
(-2)
(-10 * cos (fromIntegral frameNumber / 90))
camCenter = V3 0 0 0
camUp = V3 0 1 0
camView = lookAt camPosition camCenter camUp
camProjection = perspective
(pi/4)
(fromIntegral width / fromIntegral height)
0.1
200
cameraData = GPUCameraData
{ view = transpose camView
, projection = transpose camProjection
, viewProjection = transpose $ camProjection !*! camView
}
poke
(castPtr scenePointer)
ambientParams
poke (castPtr memoryPointer) cameraData

View file

@ -61,7 +61,6 @@ drawFrame engineData frameNumber = do
(engineFramebuffers engineData V.! fromIntegral index)
(engineWindowDimensions engineData)
(materialPipeline $ matLibrary M.! "defaultMesh")
(engineAllocator engineData)
(engineSceneParameters engineData)
frame
frameNumber