|
| 1 | +{-# LANGUAGE OverloadedLists #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE QuasiQuotes #-} |
| 4 | + |
| 5 | +{-| Dynamic-rendering version of "Triangle". Same colored triangle, same |
| 6 | +recycling-Frame loop, but with no 'Vk.RenderPass' and no 'Vk.Framebuffer': |
| 7 | +the swapchain image's layout transitions are handled by explicit pipeline |
| 8 | +barriers from "Vulkan.Utils.ImageBarrier", and the rendering region is |
| 9 | +opened with 'Vk.cmdUseRendering' against a 'Vk.RenderingInfo' that points |
| 10 | +straight at the swapchain image view. |
| 11 | +
|
| 12 | +The graphics pipeline is built with 'createColorPipelineDynamicFromShaders', |
| 13 | +which omits the render pass and instead carries a |
| 14 | +'Vk.PipelineRenderingCreateInfo' in its pNext chain. |
| 15 | +-} |
| 16 | +module TriangleDynamic |
| 17 | + ( runTriangle |
| 18 | + ) where |
| 19 | + |
| 20 | +import Control.Monad.Trans.Resource (ResourceT, register) |
| 21 | +import qualified Data.Vector as V |
| 22 | +import Data.Word (Word32) |
| 23 | +import qualified Triangle |
| 24 | +import Vulkan.CStruct.Extends (SomeStruct (..)) |
| 25 | +import qualified Vulkan.Core10 as Rect2D (Rect2D (..)) |
| 26 | +import qualified Vulkan.Core10 as Vk |
| 27 | +import qualified Vulkan.Core13 as Vk |
| 28 | +import qualified Vulkan.Extensions.VK_KHR_surface as KHR |
| 29 | +import Vulkan.Utils.Frame (Frame (..), acquireFrameImage, presentFrameImage, queueSubmitFrame, recordCommands) |
| 30 | +import Vulkan.Utils.ImageBarrier (cmdTransitionForColorAttachment, cmdTransitionForPresent) |
| 31 | +import Vulkan.Utils.Pipeline (createColorPipelineDynamicFromShaders) |
| 32 | +import Vulkan.Utils.Swapchain (Swapchain (..)) |
| 33 | +import Vulkan.Utils.VulkanContext (VulkanContext (..)) |
| 34 | +import Vulkan.Utils.WindowLoop (WindowLoop (..), noOnExit, noOnFrame, runWindowLoop) |
| 35 | +import Vulkan.Zero (zero) |
| 36 | + |
| 37 | +{- | Drive a recycling-Frame render loop drawing the colored triangle with |
| 38 | +'VK_KHR_dynamic_rendering'. |
| 39 | +-} |
| 40 | +runTriangle |
| 41 | + :: VulkanContext |
| 42 | + -> Swapchain |
| 43 | + -- ^ Initial swapchain |
| 44 | + -> IO Vk.Extent2D |
| 45 | + -- ^ Get current drawable size (for resize) |
| 46 | + -> IO Bool |
| 47 | + -- ^ Per-frame poller; 'True' means quit |
| 48 | + -> ResourceT IO () |
| 49 | +runTriangle vc initialSC getDrawableSize shouldQuit = do |
| 50 | + (_, pipeline) <- |
| 51 | + createColorPipelineDynamicFromShaders |
| 52 | + (vcDevice vc) |
| 53 | + [KHR.format (sFormat initialSC)] |
| 54 | + [ (Vk.SHADER_STAGE_VERTEX_BIT, Triangle.vertCode) |
| 55 | + , (Vk.SHADER_STAGE_FRAGMENT_BIT, Triangle.fragCode) |
| 56 | + ] |
| 57 | + |
| 58 | + runWindowLoop |
| 59 | + vc |
| 60 | + initialSC |
| 61 | + getDrawableSize |
| 62 | + shouldQuit |
| 63 | + WindowLoop |
| 64 | + { wlMkState = \_sc -> do |
| 65 | + k <- register (pure ()) |
| 66 | + pure ((), k) |
| 67 | + , wlRender = \() -> drawTriangle vc pipeline |
| 68 | + , wlOnFrame = noOnFrame |
| 69 | + , wlOnExit = noOnExit |
| 70 | + } |
| 71 | + |
| 72 | +---------------------------------------------------------------- |
| 73 | +-- Per-frame draw |
| 74 | +---------------------------------------------------------------- |
| 75 | + |
| 76 | +drawTriangle :: VulkanContext -> Vk.Pipeline -> Frame -> ResourceT IO () |
| 77 | +drawTriangle vc pipeline f = do |
| 78 | + (acquireResult, imageIndex) <- acquireFrameImage vc f |
| 79 | + commands <- recordCommands vc f \cb -> do |
| 80 | + let image = sImages V.! fromIntegral imageIndex |
| 81 | + cmdTransitionForColorAttachment cb image |
| 82 | + Vk.cmdUseRendering cb (renderingInfo imageIndex) do |
| 83 | + Vk.cmdSetViewport cb 0 [vp] |
| 84 | + Vk.cmdSetScissor cb 0 [area] |
| 85 | + Vk.cmdBindPipeline cb Vk.PIPELINE_BIND_POINT_GRAPHICS pipeline |
| 86 | + Vk.cmdDraw cb 3 1 0 0 |
| 87 | + cmdTransitionForPresent cb image |
| 88 | + queueSubmitFrame vc f [commands] |
| 89 | + presentFrameImage vc f acquireResult imageIndex |
| 90 | + where |
| 91 | + Swapchain{sExtent, sImages, sImageViews} = fSwapchain f |
| 92 | + area = zero{Rect2D.extent = sExtent} |
| 93 | + renderingInfo :: Word32 -> Vk.RenderingInfo '[] |
| 94 | + renderingInfo imageIndex = |
| 95 | + zero |
| 96 | + { Vk.renderArea = area |
| 97 | + , Vk.layerCount = 1 |
| 98 | + , Vk.colorAttachments = [SomeStruct colorAttachment] |
| 99 | + } |
| 100 | + where |
| 101 | + colorAttachment :: Vk.RenderingAttachmentInfo '[] |
| 102 | + colorAttachment = |
| 103 | + zero |
| 104 | + { Vk.imageView = sImageViews V.! fromIntegral imageIndex |
| 105 | + , Vk.imageLayout = Vk.IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL |
| 106 | + , Vk.loadOp = Vk.ATTACHMENT_LOAD_OP_CLEAR |
| 107 | + , Vk.storeOp = Vk.ATTACHMENT_STORE_OP_STORE |
| 108 | + , Vk.clearValue = Vk.Color (Vk.Float32 0.1 0.1 0.1 0) |
| 109 | + } |
| 110 | + vp :: Vk.Viewport |
| 111 | + vp = zero{Vk.width = realToFrac w, Vk.height = realToFrac h, Vk.maxDepth = 1} |
| 112 | + where |
| 113 | + Vk.Extent2D w h = sExtent |
0 commit comments