Mercurial > games > semicongine
annotate semiconginev2/rendering/renderpasses.nim @ 1228:4e465583ea32
did: rename texture to image
author | sam <sam@basx.dev> |
---|---|
date | Thu, 18 Jul 2024 16:33:24 +0700 |
parents | 56781cc0fc7c |
children | 5dcb503ef0c0 |
rev | line source |
---|---|
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
1 proc CreateDirectPresentationRenderPass*(samples = VK_SAMPLE_COUNT_1_BIT): VkRenderPass = |
1204 | 2 assert vulkan.instance.Valid, "Vulkan not initialized" |
3 | |
1199 | 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 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
27 dependencies = @[ |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
28 VkSubpassDependency( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
29 srcSubpass: VK_SUBPASS_EXTERNAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
30 dstSubpass: 0, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
31 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
32 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
33 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
34 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
35 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
36 ] |
1199 | 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 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
46 if samples == VK_SAMPLE_COUNT_1_BIT: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
47 return svkCreateRenderPass(attachments, [colorAttachment], [], dependencies) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
48 else: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
49 return svkCreateRenderPass(attachments, [colorAttachment], [resolveAttachment], dependencies) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
50 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
51 proc CreateIndirectPresentationRenderPass*(): (VkRenderPass, VkRenderPass) = |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
52 assert vulkan.instance.Valid, "Vulkan not initialized" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
53 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
54 # first renderpass, drawing |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
55 let format = DefaultSurfaceFormat() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
56 var |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
57 attachments = @[VkAttachmentDescription( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
58 format: format, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
59 samples: VK_SAMPLE_COUNT_1_BIT, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
60 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
61 storeOp: VK_ATTACHMENT_STORE_OP_STORE, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
62 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
63 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
64 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
65 finalLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
66 )] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
67 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
68 dependencies = @[ |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
69 VkSubpassDependency( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
70 srcSubpass: VK_SUBPASS_EXTERNAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
71 dstSubpass: 0, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
72 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
73 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
74 srcAccessMask: VkAccessFlags(0), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
75 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
76 ), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
77 VkSubpassDependency( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
78 srcSubpass: VK_SUBPASS_EXTERNAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
79 dstSubpass: 0, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
80 srcStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
81 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
82 srcAccessMask: toBits [VK_ACCESS_SHADER_READ_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
83 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
84 ), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
85 VkSubpassDependency( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
86 srcSubpass: 0, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
87 dstSubpass: VK_SUBPASS_EXTERNAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
88 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
89 dstStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
90 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
91 dstAccessMask: toBits [VK_ACCESS_SHADER_READ_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
92 ), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
93 ] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
94 colorAttachment = VkAttachmentReference( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
95 attachment: 0, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
96 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
97 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
98 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
99 # second renderpass, presentation |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
100 var |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
101 presentAttachments = @[VkAttachmentDescription( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
102 format: format, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
103 samples: VK_SAMPLE_COUNT_1_BIT, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
104 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
105 storeOp: VK_ATTACHMENT_STORE_OP_STORE, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
106 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
107 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
108 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
109 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
110 )] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
111 presentDependencies = @[VkSubpassDependency( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
112 srcSubpass: VK_SUBPASS_EXTERNAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
113 dstSubpass: 0, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
114 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
115 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
116 srcAccessMask: VkAccessFlags(0), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
117 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
118 )] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
119 presentColorAttachment = VkAttachmentReference( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
120 attachment: 0, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
121 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
122 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
123 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
124 result = ( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
125 svkCreateRenderPass(attachments, [colorAttachment], [], dependencies), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
126 svkCreateRenderPass(presentAttachments, [presentColorAttachment], [], presentDependencies) |
1199 | 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, | |
1204 | 153 y: renderHeight.float32, |
1199 | 154 width: renderWidth.float32, |
1204 | 155 height: -renderHeight.float32, |
1199 | 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) |