annotate semiconginev2/rendering/renderpasses.nim @ 1266:9e4dc93d4fa9

did: completely rewrite readme
author sam <sam@basx.dev>
date Sun, 28 Jul 2024 22:50:22 +0700
parents 5dcb503ef0c0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
1 proc CreateDirectPresentationRenderPass*(depthBuffer: bool, samples = VK_SAMPLE_COUNT_1_BIT): RenderPass =
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1199
diff changeset
2 assert vulkan.instance.Valid, "Vulkan not initialized"
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
3 result = RenderPass(depthBuffer: depthBuffer, samples: samples)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1199
diff changeset
4
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
5 var attachments = @[VkAttachmentDescription(
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
6 format: SURFACE_FORMAT,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
7 samples: samples,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
8 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
9 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
10 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
11 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
12 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
13 finalLayout: if samples == VK_SAMPLE_COUNT_1_BIT: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR else: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
14 )]
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
15 if depthBuffer:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
16 attachments.add VkAttachmentDescription(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
17 format: DEPTH_FORMAT,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
18 samples: samples,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
19 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
20 storeOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
21 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
22 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
23 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
24 finalLayout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
25 )
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
26 if samples != VK_SAMPLE_COUNT_1_BIT:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
27 attachments.add VkAttachmentDescription(
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
28 format: SURFACE_FORMAT,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
29 samples: VK_SAMPLE_COUNT_1_BIT,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
30 loadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
31 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
32 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
33 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
34 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
35 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
36 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
37 var
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
38 dependencies = @[
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
39 VkSubpassDependency(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
40 srcSubpass: VK_SUBPASS_EXTERNAL,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
41 dstSubpass: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
42 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
43 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT],
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
44 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
45 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
46 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
47 ]
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
48 colorAttachment = VkAttachmentReference(
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
49 attachment: 0,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
50 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
51 )
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
52 depthAttachment = VkAttachmentReference(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
53 attachment: 1,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
54 layout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
55 )
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
56 resolveAttachment = VkAttachmentReference(
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
57 attachment: (attachments.len - 1).uint32, # depending on whether depthBuffer is used or not
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
58 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
59 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
60
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
61 result.vk = svkCreateRenderPass(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
62 attachments = attachments,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
63 colorAttachments = [colorAttachment],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
64 depthAttachments = if depthBuffer: @[depthAttachment] else: @[],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
65 resolveAttachments = if samples > VK_SAMPLE_COUNT_1_BIT: @[resolveAttachment] else: @[],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
66 dependencies = dependencies,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
67 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
68
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
69 proc CreateIndirectPresentationRenderPass*(depthBuffer: bool, samples = VK_SAMPLE_COUNT_1_BIT): (RenderPass, RenderPass) =
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
70 assert vulkan.instance.Valid, "Vulkan not initialized"
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
71
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
72 result[0] = RenderPass(depthBuffer: depthBuffer, samples: samples)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
73 result[1] = RenderPass(depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
74
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
75 # first renderpass, drawing
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
76 var
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
77 attachments = @[VkAttachmentDescription(
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
78 format: SURFACE_FORMAT, # not strictly necessary
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
79 samples: samples,
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
80 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
81 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
82 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
83 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
84 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
85 # finalLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
86 finalLayout: if samples == VK_SAMPLE_COUNT_1_BIT: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL else: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
87 )]
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
88 if depthBuffer:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
89 attachments.add VkAttachmentDescription(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
90 format: DEPTH_FORMAT,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
91 samples: samples,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
92 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
93 storeOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
94 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
95 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
96 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
97 finalLayout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
98 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
99 if samples != VK_SAMPLE_COUNT_1_BIT:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
100 attachments.add VkAttachmentDescription(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
101 format: SURFACE_FORMAT,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
102 samples: VK_SAMPLE_COUNT_1_BIT,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
103 loadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
104 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
105 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
106 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
107 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
108 finalLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
109 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
110
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
111 var
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
112 dependencies = @[
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
113 VkSubpassDependency(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
114 srcSubpass: VK_SUBPASS_EXTERNAL,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
115 dstSubpass: 0,
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
116 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
117 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
118 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
119 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
120 ),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
121 VkSubpassDependency(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
122 srcSubpass: VK_SUBPASS_EXTERNAL,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
123 dstSubpass: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
124 srcStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
125 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
126 srcAccessMask: toBits [VK_ACCESS_SHADER_READ_BIT],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
127 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
128 ),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
129 VkSubpassDependency(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
130 srcSubpass: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
131 dstSubpass: VK_SUBPASS_EXTERNAL,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
132 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
133 dstStageMask: toBits [VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
134 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
135 dstAccessMask: toBits [VK_ACCESS_SHADER_READ_BIT],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
136 ),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
137 ]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
138 colorAttachment = VkAttachmentReference(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
139 attachment: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
140 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
141 )
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
142 depthAttachment = VkAttachmentReference(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
143 attachment: 1,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
144 layout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
145 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
146 resolveAttachment = VkAttachmentReference(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
147 attachment: (attachments.len - 1).uint32, # depending on whether depthBuffer is used or not
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
148 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
149 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
150 result[0].vk = svkCreateRenderPass(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
151 attachments = attachments,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
152 colorAttachments = [colorAttachment],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
153 depthAttachments = if depthBuffer: @[depthAttachment] else: @[],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
154 resolveAttachments = if samples > VK_SAMPLE_COUNT_1_BIT: @[resolveAttachment] else: @[],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
155 dependencies = dependencies
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
156 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
157
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
158 # second renderpass, presentation
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
159 var
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
160 presentAttachments = @[VkAttachmentDescription(
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
161 format: SURFACE_FORMAT,
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
162 samples: VK_SAMPLE_COUNT_1_BIT,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
163 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
164 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
165 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
166 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
167 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
168 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
169 )]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
170 presentDependencies = @[VkSubpassDependency(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
171 srcSubpass: VK_SUBPASS_EXTERNAL,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
172 dstSubpass: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
173 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
174 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
175 srcAccessMask: VkAccessFlags(0),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
176 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
177 )]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
178 presentColorAttachment = VkAttachmentReference(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
179 attachment: 0,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
180 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
181 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1204
diff changeset
182
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
183 result[1].vk = svkCreateRenderPass(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
184 attachments = presentAttachments,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
185 colorAttachments = [presentColorAttachment],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
186 depthAttachments = [],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
187 resolveAttachments = [],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
188 dependencies = presentDependencies
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
189 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
190
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
191 template WithRenderPass*(
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
192 theRenderpass: RenderPass,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
193 theFramebuffer: VkFramebuffer,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
194 commandbuffer: VkCommandBuffer,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
195 renderWidth: uint32,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
196 renderHeight: uint32,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
197 clearColor: Vec4f,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
198 body: untyped
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
199 ): untyped =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
200 var
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
201 clearColors = [
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
202 VkClearValue(color: VkClearColorValue(float32: clearColor)),
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
203 VkClearValue(depthStencil: VkClearDepthStencilValue(depth: 1'f32, stencil: 0))
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
204 ]
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
205 renderPassInfo = VkRenderPassBeginInfo(
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
206 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
207 renderPass: theRenderpass.vk,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
208 framebuffer: theFramebuffer,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
209 renderArea: VkRect2D(
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
210 offset: VkOffset2D(x: 0, y: 0),
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
211 extent: VkExtent2D(width: renderWidth, height: renderHeight),
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
212 ),
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1218
diff changeset
213 clearValueCount: clearColors.len.uint32,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
214 pClearValues: clearColors.ToCPointer(),
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
215 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
216 viewport = VkViewport(
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
217 x: 0.0,
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1199
diff changeset
218 y: renderHeight.float32,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
219 width: renderWidth.float32,
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1199
diff changeset
220 height: -renderHeight.float32,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
221 minDepth: 0.0,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
222 maxDepth: 1.0,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
223 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
224 scissor = VkRect2D(
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
225 offset: VkOffset2D(x: 0, y: 0),
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
226 extent: VkExtent2D(width: renderWidth, height: renderHeight)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
227 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
228
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
229 vkCmdBeginRenderPass(commandbuffer, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
230
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
231 # setup viewport
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
232 vkCmdSetViewport(commandbuffer, firstViewport = 0, viewportCount = 1, addr(viewport))
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
233 vkCmdSetScissor(commandbuffer, firstScissor = 0, scissorCount = 1, addr(scissor))
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
234
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
235 block:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
236 body
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
237
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
238 vkCmdEndRenderPass(commandbuffer)