vulkan-tutorial/src/Draw.hs

81 lines
2.2 KiB
Haskell
Raw Normal View History

2022-07-14 05:02:10 +00:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Draw where
import Control.Monad
import Control.Monad.Trans.Resource
import qualified Data.Vector as V
2022-07-14 09:02:12 +00:00
import Foreign.C.Types (CInt)
import Linear (V2(..))
2022-07-14 05:02:10 +00:00
import qualified Vulkan.Core10 as Vk
import qualified Vulkan.Extensions.VK_KHR_swapchain as Khr
import qualified Vulkan.CStruct.Extends as Vk
import qualified Vulkan.Zero as Vk
2022-07-14 09:02:12 +00:00
-- internal imports
import CommandBuffer
2022-07-14 05:02:10 +00:00
drawFrame
:: (MonadResource m)
=> Vk.Device
-> Khr.SwapchainKHR
-> Vk.Queue
-> Vk.CommandBuffer
2022-07-14 09:02:12 +00:00
-> Vk.RenderPass
-> Vk.Framebuffer
-> V2 CInt
-> Vk.Pipeline
2022-07-14 05:02:10 +00:00
-> Vk.Fence
-> Vk.Semaphore
-> Vk.Semaphore
-> m ()
drawFrame
logicalDevice
swapchain
graphicsQueue
commandBuffer
2022-07-14 09:02:12 +00:00
renderPass
framebuffer
dimensions
pipeline
2022-07-14 05:02:10 +00:00
inFlightFence
imageAvailableSemaphore
renderFinishedSemaphore
= do
let fences = V.singleton inFlightFence
void $ Vk.waitForFences logicalDevice fences True maxBound
Vk.resetFences logicalDevice fences
(imageResult, index) <-
2022-07-14 09:02:12 +00:00
Khr.acquireNextImageKHR logicalDevice swapchain maxBound imageAvailableSemaphore Vk.NULL_HANDLE
2022-07-14 05:02:10 +00:00
unless (imageResult == Vk.SUCCESS) $
error "drawFrame: Failed acquiring next image from swapchain"
Vk.resetCommandBuffer commandBuffer (Vk.CommandBufferResetFlagBits 0)
2022-07-14 09:02:12 +00:00
recordCommandBuffer commandBuffer renderPass framebuffer dimensions pipeline
2022-07-14 05:02:10 +00:00
let submitInfo = Vk.zero
{ Vk.waitSemaphores = V.singleton imageAvailableSemaphore
, Vk.waitDstStageMask = V.singleton Vk.PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
, Vk.commandBuffers = V.singleton (Vk.commandBufferHandle commandBuffer)
, Vk.signalSemaphores = V.singleton renderFinishedSemaphore
}
Vk.queueSubmit graphicsQueue (V.singleton (Vk.SomeStruct submitInfo)) inFlightFence
let presentInfo = Vk.zero
{ Khr.waitSemaphores = V.singleton renderFinishedSemaphore
, Khr.swapchains = V.singleton swapchain
2022-07-14 15:05:59 +00:00
, Khr.imageIndices = V.singleton 0
2022-07-14 05:02:10 +00:00
}
presentResult <- Khr.queuePresentKHR graphicsQueue presentInfo
unless (presentResult == Vk.SUCCESS) $
error "drawFrame: presentation failed"