comparison semiconginev2/rendering/renderpasses.nim @ 1229:5dcb503ef0c0

did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
author sam <sam@basx.dev>
date Thu, 18 Jul 2024 21:32:41 +0700
parents 56781cc0fc7c
children
comparison
equal deleted inserted replaced
1228:4e465583ea32 1229:5dcb503ef0c0
1 proc CreateDirectPresentationRenderPass*(samples = VK_SAMPLE_COUNT_1_BIT): VkRenderPass = 1 proc CreateDirectPresentationRenderPass*(depthBuffer: bool, samples = VK_SAMPLE_COUNT_1_BIT): RenderPass =
2 assert vulkan.instance.Valid, "Vulkan not initialized" 2 assert vulkan.instance.Valid, "Vulkan not initialized"
3 3 result = RenderPass(depthBuffer: depthBuffer, samples: samples)
4 let format = DefaultSurfaceFormat() 4
5 var attachments = @[VkAttachmentDescription( 5 var attachments = @[VkAttachmentDescription(
6 format: format, 6 format: SURFACE_FORMAT,
7 samples: samples, 7 samples: samples,
8 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, 8 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
9 storeOp: VK_ATTACHMENT_STORE_OP_STORE, 9 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
10 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, 10 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
11 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, 11 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
12 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, 12 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
13 finalLayout: if samples == VK_SAMPLE_COUNT_1_BIT: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR else: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 13 finalLayout: if samples == VK_SAMPLE_COUNT_1_BIT: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR else: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
14 )] 14 )]
15 if depthBuffer:
16 attachments.add VkAttachmentDescription(
17 format: DEPTH_FORMAT,
18 samples: samples,
19 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
20 storeOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
21 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
22 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
23 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
24 finalLayout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
25 )
15 if samples != VK_SAMPLE_COUNT_1_BIT: 26 if samples != VK_SAMPLE_COUNT_1_BIT:
16 attachments.add VkAttachmentDescription( 27 attachments.add VkAttachmentDescription(
17 format: format, 28 format: SURFACE_FORMAT,
18 samples: VK_SAMPLE_COUNT_1_BIT, 29 samples: VK_SAMPLE_COUNT_1_BIT,
19 loadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, 30 loadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
20 storeOp: VK_ATTACHMENT_STORE_OP_STORE, 31 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
21 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, 32 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
22 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, 33 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
28 VkSubpassDependency( 39 VkSubpassDependency(
29 srcSubpass: VK_SUBPASS_EXTERNAL, 40 srcSubpass: VK_SUBPASS_EXTERNAL,
30 dstSubpass: 0, 41 dstSubpass: 0,
31 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], 42 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT],
32 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], 43 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT],
33 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], 44 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
34 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], 45 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
35 ) 46 )
36 ] 47 ]
37 colorAttachment = VkAttachmentReference( 48 colorAttachment = VkAttachmentReference(
38 attachment: 0, 49 attachment: 0,
39 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 50 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
40 ) 51 )
52 depthAttachment = VkAttachmentReference(
53 attachment: 1,
54 layout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
55 )
41 resolveAttachment = VkAttachmentReference( 56 resolveAttachment = VkAttachmentReference(
42 attachment: 1, 57 attachment: (attachments.len - 1).uint32, # depending on whether depthBuffer is used or not
43 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 58 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
44 ) 59 )
45 60
46 if samples == VK_SAMPLE_COUNT_1_BIT: 61 result.vk = svkCreateRenderPass(
47 return svkCreateRenderPass(attachments, [colorAttachment], [], dependencies) 62 attachments = attachments,
48 else: 63 colorAttachments = [colorAttachment],
49 return svkCreateRenderPass(attachments, [colorAttachment], [resolveAttachment], dependencies) 64 depthAttachments = if depthBuffer: @[depthAttachment] else: @[],
50 65 resolveAttachments = if samples > VK_SAMPLE_COUNT_1_BIT: @[resolveAttachment] else: @[],
51 proc CreateIndirectPresentationRenderPass*(): (VkRenderPass, VkRenderPass) = 66 dependencies = dependencies,
67 )
68
69 proc CreateIndirectPresentationRenderPass*(depthBuffer: bool, samples = VK_SAMPLE_COUNT_1_BIT): (RenderPass, RenderPass) =
52 assert vulkan.instance.Valid, "Vulkan not initialized" 70 assert vulkan.instance.Valid, "Vulkan not initialized"
53 71
72 result[0] = RenderPass(depthBuffer: depthBuffer, samples: samples)
73 result[1] = RenderPass(depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT)
74
54 # first renderpass, drawing 75 # first renderpass, drawing
55 let format = DefaultSurfaceFormat()
56 var 76 var
57 attachments = @[VkAttachmentDescription( 77 attachments = @[VkAttachmentDescription(
58 format: format, 78 format: SURFACE_FORMAT, # not strictly necessary
79 samples: samples,
80 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
81 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
82 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
83 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
84 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
85 # finalLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
86 finalLayout: if samples == VK_SAMPLE_COUNT_1_BIT: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL else: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
87 )]
88 if depthBuffer:
89 attachments.add VkAttachmentDescription(
90 format: DEPTH_FORMAT,
91 samples: samples,
92 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
93 storeOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
94 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
95 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
96 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
97 finalLayout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
98 )
99 if samples != VK_SAMPLE_COUNT_1_BIT:
100 attachments.add VkAttachmentDescription(
101 format: SURFACE_FORMAT,
59 samples: VK_SAMPLE_COUNT_1_BIT, 102 samples: VK_SAMPLE_COUNT_1_BIT,
60 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, 103 loadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
61 storeOp: VK_ATTACHMENT_STORE_OP_STORE, 104 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
62 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, 105 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
63 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, 106 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
64 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, 107 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
65 finalLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 108 finalLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
66 )] 109 )
67 110
111 var
68 dependencies = @[ 112 dependencies = @[
69 VkSubpassDependency( 113 VkSubpassDependency(
70 srcSubpass: VK_SUBPASS_EXTERNAL, 114 srcSubpass: VK_SUBPASS_EXTERNAL,
71 dstSubpass: 0, 115 dstSubpass: 0,
72 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], 116 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT],
73 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], 117 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT],
74 srcAccessMask: VkAccessFlags(0), 118 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
75 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], 119 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
76 ), 120 ),
77 VkSubpassDependency( 121 VkSubpassDependency(
78 srcSubpass: VK_SUBPASS_EXTERNAL, 122 srcSubpass: VK_SUBPASS_EXTERNAL,
79 dstSubpass: 0, 123 dstSubpass: 0,
80 srcStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT], 124 srcStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT],
93 ] 137 ]
94 colorAttachment = VkAttachmentReference( 138 colorAttachment = VkAttachmentReference(
95 attachment: 0, 139 attachment: 0,
96 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 140 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
97 ) 141 )
142 depthAttachment = VkAttachmentReference(
143 attachment: 1,
144 layout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
145 )
146 resolveAttachment = VkAttachmentReference(
147 attachment: (attachments.len - 1).uint32, # depending on whether depthBuffer is used or not
148 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
149 )
150 result[0].vk = svkCreateRenderPass(
151 attachments = attachments,
152 colorAttachments = [colorAttachment],
153 depthAttachments = if depthBuffer: @[depthAttachment] else: @[],
154 resolveAttachments = if samples > VK_SAMPLE_COUNT_1_BIT: @[resolveAttachment] else: @[],
155 dependencies = dependencies
156 )
98 157
99 # second renderpass, presentation 158 # second renderpass, presentation
100 var 159 var
101 presentAttachments = @[VkAttachmentDescription( 160 presentAttachments = @[VkAttachmentDescription(
102 format: format, 161 format: SURFACE_FORMAT,
103 samples: VK_SAMPLE_COUNT_1_BIT, 162 samples: VK_SAMPLE_COUNT_1_BIT,
104 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, 163 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
105 storeOp: VK_ATTACHMENT_STORE_OP_STORE, 164 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
106 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, 165 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
107 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, 166 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
119 presentColorAttachment = VkAttachmentReference( 178 presentColorAttachment = VkAttachmentReference(
120 attachment: 0, 179 attachment: 0,
121 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 180 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
122 ) 181 )
123 182
124 result = ( 183 result[1].vk = svkCreateRenderPass(
125 svkCreateRenderPass(attachments, [colorAttachment], [], dependencies), 184 attachments = presentAttachments,
126 svkCreateRenderPass(presentAttachments, [presentColorAttachment], [], presentDependencies) 185 colorAttachments = [presentColorAttachment],
186 depthAttachments = [],
187 resolveAttachments = [],
188 dependencies = presentDependencies
127 ) 189 )
128 190
129 template WithRenderPass*( 191 template WithRenderPass*(
130 theRenderpass: VkRenderPass, 192 theRenderpass: RenderPass,
131 theFramebuffer: VkFramebuffer, 193 theFramebuffer: VkFramebuffer,
132 commandbuffer: VkCommandBuffer, 194 commandbuffer: VkCommandBuffer,
133 renderWidth: uint32, 195 renderWidth: uint32,
134 renderHeight: uint32, 196 renderHeight: uint32,
135 clearColor: Vec4f, 197 clearColor: Vec4f,
136 body: untyped 198 body: untyped
137 ): untyped = 199 ): untyped =
138 var 200 var
139 clearColors = [VkClearValue(color: VkClearColorValue(float32: clearColor))] 201 clearColors = [
202 VkClearValue(color: VkClearColorValue(float32: clearColor)),
203 VkClearValue(depthStencil: VkClearDepthStencilValue(depth: 1'f32, stencil: 0))
204 ]
140 renderPassInfo = VkRenderPassBeginInfo( 205 renderPassInfo = VkRenderPassBeginInfo(
141 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, 206 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
142 renderPass: theRenderpass, 207 renderPass: theRenderpass.vk,
143 framebuffer: theFramebuffer, 208 framebuffer: theFramebuffer,
144 renderArea: VkRect2D( 209 renderArea: VkRect2D(
145 offset: VkOffset2D(x: 0, y: 0), 210 offset: VkOffset2D(x: 0, y: 0),
146 extent: VkExtent2D(width: renderWidth, height: renderHeight), 211 extent: VkExtent2D(width: renderWidth, height: renderHeight),
147 ), 212 ),
148 clearValueCount: uint32(clearColors.len), 213 clearValueCount: clearColors.len.uint32,
149 pClearValues: clearColors.ToCPointer(), 214 pClearValues: clearColors.ToCPointer(),
150 ) 215 )
151 viewport = VkViewport( 216 viewport = VkViewport(
152 x: 0.0, 217 x: 0.0,
153 y: renderHeight.float32, 218 y: renderHeight.float32,