From 1107ea91daead7910d1428c46a076f7e5f18d786 Mon Sep 17 00:00:00 2001 From: nek0 Date: Sat, 3 Dec 2022 01:44:06 +0100 Subject: [PATCH] slowly working forwards --- src/CommandBuffer.hs | 105 ++++++++++++++++++++++++---------------- src/Draw.hs | 8 ++- src/GraphicsPipeline.hs | 11 +++-- src/Init.hs | 3 ++ src/Main.hs | 2 +- src/Types.hs | 3 ++ 6 files changed, 83 insertions(+), 49 deletions(-) diff --git a/src/CommandBuffer.hs b/src/CommandBuffer.hs index 106ab8a..ad123d7 100644 --- a/src/CommandBuffer.hs +++ b/src/CommandBuffer.hs @@ -14,6 +14,8 @@ import qualified Vulkan.Zero as Vk -- internal imports import Devices +import Types +import Mesh createCommandPool :: (MonadResource m) @@ -64,63 +66,82 @@ recordCommandBuffer -> Vk.Framebuffer -> V2 CInt -> Vk.Pipeline + -> Maybe Mesh -> m () -recordCommandBuffer commandBuffer renderPass frameBuffer (V2 width height) graphicsPipeline = do +recordCommandBuffer + commandBuffer + renderPass + frameBuffer + (V2 width height) + graphicsPipeline + mesh + = do - let commandBufferBeginInfo = Vk.zero - { Vk.flags = Vk.COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT - , Vk.inheritanceInfo = Nothing - } + let commandBufferBeginInfo = Vk.zero + { Vk.flags = Vk.COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT + , Vk.inheritanceInfo = Nothing + } - Vk.beginCommandBuffer commandBuffer commandBufferBeginInfo + Vk.beginCommandBuffer commandBuffer commandBufferBeginInfo - let renderPassInfo = Vk.zero - { Vk.renderPass = renderPass - , Vk.framebuffer = frameBuffer - , Vk.renderArea = Vk.Rect2D - { offset = Vk.Offset2D + let renderPassInfo = Vk.zero + { Vk.renderPass = renderPass + , Vk.framebuffer = frameBuffer + , Vk.renderArea = Vk.Rect2D + { offset = Vk.Offset2D + { Vk.x = 0 + , Vk.y = 0 + } + , extent = Vk.Extent2D + { Vk.width = fromIntegral width + , Vk.height = fromIntegral height + } + } + , Vk.clearValues = V.replicate 4 (Vk.Color $ Vk.Float32 0 0 0 1) + } + + Vk.cmdBeginRenderPass commandBuffer renderPassInfo Vk.SUBPASS_CONTENTS_INLINE + + Vk.cmdBindPipeline commandBuffer Vk.PIPELINE_BIND_POINT_GRAPHICS graphicsPipeline + + let viewport = Vk.zero + { Vk.x = 0 + , Vk.y = 0 + , Vk.width = fromIntegral width + , Vk.height = fromIntegral height + , Vk.minDepth = 0 + , Vk.maxDepth = 1 + } + scissor = Vk.Rect2D + { Vk.offset = Vk.Offset2D { Vk.x = 0 , Vk.y = 0 } - , extent = Vk.Extent2D - { Vk.width = fromIntegral width + , Vk.extent = Vk.Extent2D + { Vk.width = fromIntegral width , Vk.height = fromIntegral height } } - , Vk.clearValues = V.replicate 4 (Vk.Color $ Vk.Float32 0 0 0 1) - } - Vk.cmdBeginRenderPass commandBuffer renderPassInfo Vk.SUBPASS_CONTENTS_INLINE + Vk.cmdSetViewport commandBuffer 0 (V.singleton viewport) + Vk.cmdSetScissor commandBuffer 0 (V.singleton scissor) - Vk.cmdBindPipeline commandBuffer Vk.PIPELINE_BIND_POINT_GRAPHICS graphicsPipeline + maybe + (Vk.cmdDraw commandBuffer 3 1 0 0) + (\jmesh -> do + Vk.cmdBindVertexBuffers + commandBuffer + 0 + (V.fromList [ allocatedBuffer $ meshBuffer jmesh ]) + (V.fromList [ 0 ]) - let viewport = Vk.zero - { Vk.x = 0 - , Vk.y = 0 - , Vk.width = fromIntegral width - , Vk.height = fromIntegral height - , Vk.minDepth = 0 - , Vk.maxDepth = 1 - } - scissor = Vk.Rect2D - { Vk.offset = Vk.Offset2D - { Vk.x = 0 - , Vk.y = 0 - } - , Vk.extent = Vk.Extent2D - { Vk.width = fromIntegral width - , Vk.height = fromIntegral height - } - } + Vk.cmdDraw commandBuffer (fromIntegral $ V.length $ meshVertices jmesh) 1 0 0 + ) + mesh - Vk.cmdSetViewport commandBuffer 0 (V.singleton viewport) - Vk.cmdSetScissor commandBuffer 0 (V.singleton scissor) + Vk.cmdEndRenderPass commandBuffer - Vk.cmdDraw commandBuffer 3 1 0 0 - - Vk.cmdEndRenderPass commandBuffer - - Vk.endCommandBuffer commandBuffer + Vk.endCommandBuffer commandBuffer createSyncObjects :: (MonadResource m, MonadFail m) diff --git a/src/Draw.hs b/src/Draw.hs index 1428747..c34e1a7 100644 --- a/src/Draw.hs +++ b/src/Draw.hs @@ -18,7 +18,7 @@ import Types import Control.Monad.IO.Class (MonadIO(liftIO)) drawFrame - :: (MonadResource m) + :: (MonadResource m, MonadFail m) => EngineData -> STM.TMVar Pipelines -> m () @@ -69,6 +69,12 @@ drawFrame engineData switch = do Green -> meshPipeline engineData V.! fromIntegral index ) + (case switchState of + Green -> + Just $ engineMesh engineData + _ -> + Nothing + ) let submitInfo = Vk.zero { Vk.waitSemaphores = diff --git a/src/GraphicsPipeline.hs b/src/GraphicsPipeline.hs index dad0ae9..b97ea6a 100644 --- a/src/GraphicsPipeline.hs +++ b/src/GraphicsPipeline.hs @@ -12,6 +12,7 @@ import Data.Bits import qualified Data.ByteString.Char8 as BS import qualified Data.Vector as V import Foreign.C.Types (CInt) +import qualified Vulkan as VK import qualified Vulkan.Core10 as Vk import qualified Vulkan.Zero as Vk import qualified Vulkan.CStruct.Extends as Vk @@ -19,8 +20,7 @@ import qualified Vulkan.Utils.ShaderQQ.GLSL.Shaderc as Vk -- internal imports import Types -import qualified Vulkan as VK -import qualified Vulkan.CStruct.Extends as VK +import Mesh loadShader :: (MonadResource m) @@ -128,7 +128,7 @@ createVertexInputAssemblyStateCreateInfo topology = createRasterizationStateCreateInfo :: Vk.PolygonMode - -> VK.SomeStruct Vk.PipelineRasterizationStateCreateInfo + -> Vk.SomeStruct Vk.PipelineRasterizationStateCreateInfo createRasterizationStateCreateInfo mode = Vk.SomeStruct $ Vk.zero { Vk.depthClampEnable = False @@ -198,9 +198,10 @@ createGraphicsPipelines pipelineDynamicStateCreateInfo = Vk.zero { Vk.dynamicStates = dynamicStates } + vertexDescription = getVertexDescription (V.head loadMeshes) pipelineVertexInputCreateInfo = Vk.zero - { Vk.vertexBindingDescriptions = V.empty -- Fill me? - , Vk.vertexAttributeDescriptions = V.empty -- Fill me? + { Vk.vertexBindingDescriptions = vidBindings vertexDescription + , Vk.vertexAttributeDescriptions = vidAttributes vertexDescription } viewport = Vk.Viewport { Vk.x = 0 diff --git a/src/Init.hs b/src/Init.hs index 026e739..3e15d2e 100644 --- a/src/Init.hs +++ b/src/Init.hs @@ -130,7 +130,9 @@ initVulkan window = do return $ EngineData window dimensions + vulkanPhysicalDevice vulkanLogicalDevice + vulkanInstance swapchain graphicsQueue commandBuffer @@ -143,3 +145,4 @@ initVulkan window = do imageAvailableSemaphore renderFinishedSemaphore mesh + allocator diff --git a/src/Main.hs b/src/Main.hs index 0a0feec..3163b23 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -28,7 +28,7 @@ main = runResourceT $ do -- create abort condition for upcoming lop quit <- liftIO $ STM.newTMVarIO True - showPipeline <- liftIO $ STM.newTMVarIO Rainbow + showPipeline <- liftIO $ STM.newTMVarIO Green -- main loop whileM_ diff --git a/src/Types.hs b/src/Types.hs index c0dcfea..d63beac 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -27,7 +27,9 @@ data ShaderContainer = ShaderContainer data EngineData = EngineData { engineWindow :: SDL.Window , engineWindowDimensions :: V2 CInt + , enginePhysicalDevice :: Vk.PhysicalDevice , engineLogicalDevice :: Vk.Device + , engineInstance :: Vk.Instance , engineSwapchain :: Vk.SwapchainKHR , engineQueue :: Vk.Queue , engineCommandBuffers :: V.Vector Vk.CommandBuffer @@ -40,6 +42,7 @@ data EngineData = EngineData , engineImageAvailableSemaphore :: Vk.Semaphore , engineRenderFinishedSemaphore :: Vk.Semaphore , engineMesh :: Mesh + , engineAllocator :: VMA.Allocator } data AllocatedBuffer = AllocatedBuffer