Mercurial > games > semicongine
annotate semiconginev2/old/vulkan/renderpass.nim @ 1221:000befd9479f compiletime-tests
did: remove support for git, for now
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:05:47 +0700 |
parents | 56781cc0fc7c |
children |
rev | line source |
---|---|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import ../core |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import ../material |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import ./device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import ./physicaldevice |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import ./pipeline |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 import ./shader |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 import ./framebuffer |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 RenderPass* = object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 vk*: VkRenderPass |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 device*: Device |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
13 shaderPipelines*: seq[(MaterialType, ShaderPipeline)] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
14 clearColor*: Vec4f |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 |
1138 | 16 proc CreateRenderPass*( |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 device: Device, |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
18 shaders: openArray[(MaterialType, ShaderConfiguration)], |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
19 clearColor = Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32]), |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
20 backFaceCulling = true, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
21 inFlightFrames = 2, |
1167 | 22 samples = VK_SAMPLE_COUNT_1_BIT |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 ): RenderPass = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
24 assert device.vk.Valid |
998
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
944
diff
changeset
|
25 |
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
944
diff
changeset
|
26 # some asserts |
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
944
diff
changeset
|
27 for (materialtype, shaderconfig) in shaders: |
1138 | 28 shaderconfig.AssertCanRender(materialtype) |
998
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
944
diff
changeset
|
29 |
1167 | 30 var attachments = @[ |
31 VkAttachmentDescription( | |
1138 | 32 format: device.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format, |
1167 | 33 samples: samples, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 storeOp: VK_ATTACHMENT_STORE_OP_STORE, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, |
1167 | 39 finalLayout: if samples == VK_SAMPLE_COUNT_1_BIT: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR else: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
40 ), | |
41 ] | |
42 | |
43 if samples != VK_SAMPLE_COUNT_1_BIT: | |
44 attachments.add VkAttachmentDescription( | |
45 format: device.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format, | |
46 samples: VK_SAMPLE_COUNT_1_BIT, | |
47 loadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
48 storeOp: VK_ATTACHMENT_STORE_OP_STORE, | |
49 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, | |
50 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, | |
51 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED, | |
52 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | |
53 ) | |
54 | |
55 var | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 # dependencies seems to be optional, TODO: benchmark difference |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 dependencies = @[VkSubpassDependency( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 srcSubpass: VK_SUBPASS_EXTERNAL, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 dstSubpass: 0, |
1167 | 60 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT], |
61 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
62 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT], | |
63 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT], | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 )] |
1167 | 65 colorAttachment = VkAttachmentReference( |
66 attachment: 0, | |
67 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
68 ) | |
69 resolveAttachment = VkAttachmentReference( | |
70 attachment: 1, | |
71 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | |
72 ) | |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
73 |
1167 | 74 var subpass = VkSubpassDescription( |
75 flags: VkSubpassDescriptionFlags(0), | |
76 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS, | |
77 inputAttachmentCount: 0, | |
78 pInputAttachments: nil, | |
79 colorAttachmentCount: 1, | |
80 pColorAttachments: addr(colorAttachment), | |
81 pResolveAttachments: if samples == VK_SAMPLE_COUNT_1_BIT: nil else: addr(resolveAttachment), | |
82 pDepthStencilAttachment: nil, | |
83 preserveAttachmentCount: 0, | |
84 pPreserveAttachments: nil, | |
85 ) | |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
86 |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
87 var createInfo = VkRenderPassCreateInfo( |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
88 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
89 attachmentCount: uint32(attachments.len), |
1137 | 90 pAttachments: attachments.ToCPointer, |
1167 | 91 subpassCount: 1, |
92 pSubpasses: addr(subpass), | |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
93 dependencyCount: uint32(dependencies.len), |
1137 | 94 pDependencies: dependencies.ToCPointer, |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
95 ) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
96 result.device = device |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
97 result.clearColor = clearColor |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
98 checkVkResult device.vk.vkCreateRenderPass(addr(createInfo), nil, addr(result.vk)) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
99 |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
100 for (_, shaderconfig) in shaders: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
101 assert shaderconfig.outputs.len == 1 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 for (materialtype, shaderconfig) in shaders: |
1167 | 103 result.shaderPipelines.add (materialtype, device.CreatePipeline(result.vk, shaderconfig, inFlightFrames, 0, backFaceCulling = backFaceCulling, samples = samples)) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 |
1138 | 105 proc BeginRenderCommands*(commandBuffer: VkCommandBuffer, renderpass: RenderPass, framebuffer: Framebuffer, oneTimeSubmit: bool) = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
106 assert commandBuffer.Valid |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
107 assert renderpass.vk.Valid |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
108 assert framebuffer.vk.Valid |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 let |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 w = framebuffer.dimension.x |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 h = framebuffer.dimension.y |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
113 var clearColors = [VkClearValue(color: VkClearColorValue(float32: renderpass.clearColor))] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 var |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 beginInfo = VkCommandBufferBeginInfo( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 pInheritanceInfo: nil, |
937
d27e28f9d8ab
fix: a few small (vulkan) API optimizations
sam <sam@basx.dev>
parents:
920
diff
changeset
|
118 flags: if oneTimeSubmit: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) else: VkCommandBufferUsageFlags(0), |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
120 renderPassInfo = VkRenderPassBeginInfo( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
121 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
122 renderPass: renderPass.vk, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
123 framebuffer: framebuffer.vk, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
124 renderArea: VkRect2D( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
125 offset: VkOffset2D(x: 0, y: 0), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
126 extent: VkExtent2D(width: w, height: h), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
127 ), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
128 clearValueCount: uint32(clearColors.len), |
1137 | 129 pClearValues: clearColors.ToCPointer(), |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
130 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
131 viewport = VkViewport( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
132 x: 0.0, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
133 y: 0.0, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
134 width: (float)w, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
135 height: (float)h, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
136 minDepth: 0.0, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
137 maxDepth: 1.0, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
138 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 scissor = VkRect2D( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
140 offset: VkOffset2D(x: 0, y: 0), |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
141 extent: VkExtent2D(width: w, height: h) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
142 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
143 checkVkResult commandBuffer.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
144 checkVkResult commandBuffer.vkBeginCommandBuffer(addr(beginInfo)) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
145 commandBuffer.vkCmdBeginRenderPass(addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE) |
902 | 146 commandBuffer.vkCmdSetViewport(firstViewport = 0, viewportCount = 1, addr(viewport)) |
147 commandBuffer.vkCmdSetScissor(firstScissor = 0, scissorCount = 1, addr(scissor)) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
148 |
1138 | 149 proc EndRenderCommands*(commandBuffer: VkCommandBuffer) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
150 commandBuffer.vkCmdEndRenderPass() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
151 checkVkResult commandBuffer.vkEndCommandBuffer() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
152 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
153 |
1138 | 154 proc Destroy*(renderPass: var RenderPass) = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
155 assert renderPass.device.vk.Valid |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
156 assert renderPass.vk.Valid |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
157 renderPass.device.vk.vkDestroyRenderPass(renderPass.vk, nil) |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
158 renderPass.vk.Reset |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
159 for _, pipeline in renderPass.shaderPipelines.mitems: |
1138 | 160 pipeline.Destroy() |