comparison semiconginev2/rendering/renderpasses.nim @ 1218:56781cc0fc7c compiletime-tests

did: renamge main package
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:01:37 +0700
parents semicongine/rendering/renderpasses.nim@04e446a7eb2b
children 5dcb503ef0c0
comparison
equal deleted inserted replaced
1217:f819a874058f 1218:56781cc0fc7c
1 proc CreateDirectPresentationRenderPass*(samples = VK_SAMPLE_COUNT_1_BIT): VkRenderPass =
2 assert vulkan.instance.Valid, "Vulkan not initialized"
3
4 let format = DefaultSurfaceFormat()
5 var attachments = @[VkAttachmentDescription(
6 format: format,
7 samples: samples,
8 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
9 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
10 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
11 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
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,
14 )]
15 if samples != VK_SAMPLE_COUNT_1_BIT:
16 attachments.add VkAttachmentDescription(
17 format: format,
18 samples: VK_SAMPLE_COUNT_1_BIT,
19 loadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
20 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
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_PRESENT_SRC_KHR,
25 )
26 var
27 dependencies = @[
28 VkSubpassDependency(
29 srcSubpass: VK_SUBPASS_EXTERNAL,
30 dstSubpass: 0,
31 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],
33 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
34 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
35 )
36 ]
37 colorAttachment = VkAttachmentReference(
38 attachment: 0,
39 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
40 )
41 resolveAttachment = VkAttachmentReference(
42 attachment: 1,
43 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
44 )
45
46 if samples == VK_SAMPLE_COUNT_1_BIT:
47 return svkCreateRenderPass(attachments, [colorAttachment], [], dependencies)
48 else:
49 return svkCreateRenderPass(attachments, [colorAttachment], [resolveAttachment], dependencies)
50
51 proc CreateIndirectPresentationRenderPass*(): (VkRenderPass, VkRenderPass) =
52 assert vulkan.instance.Valid, "Vulkan not initialized"
53
54 # first renderpass, drawing
55 let format = DefaultSurfaceFormat()
56 var
57 attachments = @[VkAttachmentDescription(
58 format: format,
59 samples: VK_SAMPLE_COUNT_1_BIT,
60 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
61 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
62 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
63 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
64 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
65 finalLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
66 )]
67
68 dependencies = @[
69 VkSubpassDependency(
70 srcSubpass: VK_SUBPASS_EXTERNAL,
71 dstSubpass: 0,
72 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
73 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
74 srcAccessMask: VkAccessFlags(0),
75 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
76 ),
77 VkSubpassDependency(
78 srcSubpass: VK_SUBPASS_EXTERNAL,
79 dstSubpass: 0,
80 srcStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT],
81 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
82 srcAccessMask: toBits [VK_ACCESS_SHADER_READ_BIT],
83 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
84 ),
85 VkSubpassDependency(
86 srcSubpass: 0,
87 dstSubpass: VK_SUBPASS_EXTERNAL,
88 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
89 dstStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT],
90 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
91 dstAccessMask: toBits [VK_ACCESS_SHADER_READ_BIT],
92 ),
93 ]
94 colorAttachment = VkAttachmentReference(
95 attachment: 0,
96 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
97 )
98
99 # second renderpass, presentation
100 var
101 presentAttachments = @[VkAttachmentDescription(
102 format: format,
103 samples: VK_SAMPLE_COUNT_1_BIT,
104 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
105 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
106 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
107 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
108 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
109 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
110 )]
111 presentDependencies = @[VkSubpassDependency(
112 srcSubpass: VK_SUBPASS_EXTERNAL,
113 dstSubpass: 0,
114 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
115 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
116 srcAccessMask: VkAccessFlags(0),
117 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
118 )]
119 presentColorAttachment = VkAttachmentReference(
120 attachment: 0,
121 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
122 )
123
124 result = (
125 svkCreateRenderPass(attachments, [colorAttachment], [], dependencies),
126 svkCreateRenderPass(presentAttachments, [presentColorAttachment], [], presentDependencies)
127 )
128
129 template WithRenderPass*(
130 theRenderpass: VkRenderPass,
131 theFramebuffer: VkFramebuffer,
132 commandbuffer: VkCommandBuffer,
133 renderWidth: uint32,
134 renderHeight: uint32,
135 clearColor: Vec4f,
136 body: untyped
137 ): untyped =
138 var
139 clearColors = [VkClearValue(color: VkClearColorValue(float32: clearColor))]
140 renderPassInfo = VkRenderPassBeginInfo(
141 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
142 renderPass: theRenderpass,
143 framebuffer: theFramebuffer,
144 renderArea: VkRect2D(
145 offset: VkOffset2D(x: 0, y: 0),
146 extent: VkExtent2D(width: renderWidth, height: renderHeight),
147 ),
148 clearValueCount: uint32(clearColors.len),
149 pClearValues: clearColors.ToCPointer(),
150 )
151 viewport = VkViewport(
152 x: 0.0,
153 y: renderHeight.float32,
154 width: renderWidth.float32,
155 height: -renderHeight.float32,
156 minDepth: 0.0,
157 maxDepth: 1.0,
158 )
159 scissor = VkRect2D(
160 offset: VkOffset2D(x: 0, y: 0),
161 extent: VkExtent2D(width: renderWidth, height: renderHeight)
162 )
163
164 vkCmdBeginRenderPass(commandbuffer, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE)
165
166 # setup viewport
167 vkCmdSetViewport(commandbuffer, firstViewport = 0, viewportCount = 1, addr(viewport))
168 vkCmdSetScissor(commandbuffer, firstScissor = 0, scissorCount = 1, addr(scissor))
169
170 block:
171 body
172
173 vkCmdEndRenderPass(commandbuffer)