Mercurial > games > semicongine
annotate tests/test_rendering.nim @ 1209:e177336cac3f compiletime-tests
sync to notebook in bedroom
author | sam <sam@basx.dev> |
---|---|
date | Tue, 16 Jul 2024 20:39:35 +0700 |
parents | 8cf9a05b03eb |
children | 570ac1620fb6 |
rev | line source |
---|---|
1203 | 1 import std/options |
1204 | 2 import std/random |
3 | |
1203 | 4 import ../semicongine |
5 | |
6 var | |
7 mainRenderpass: VkRenderPass | |
8 swapchain: Swapchain | |
9 | |
1204 | 10 proc test_01_triangle(nFrames: int) = |
1203 | 11 var renderdata = InitRenderData() |
12 | |
13 type | |
14 TrianglShader = object | |
15 position {.VertexAttribute.}: Vec3f | |
16 color {.VertexAttribute.}: Vec3f | |
17 fragmentColor {.Pass.}: Vec3f | |
18 outColor {.ShaderOutput.}: Vec4f | |
19 # code | |
20 vertexCode: string = """void main() { | |
21 fragmentColor = color; | |
22 gl_Position = vec4(position, 1);}""" | |
23 fragmentCode: string = """void main() { | |
24 outColor = vec4(fragmentColor, 1);}""" | |
25 TriangleMesh = object | |
26 position: GPUArray[Vec3f, VertexBuffer] | |
27 color: GPUArray[Vec3f, VertexBuffer] | |
28 var mesh = TriangleMesh( | |
1204 | 29 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), |
1203 | 30 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), |
31 ) | |
1204 | 32 AssignBuffers(renderdata, mesh) |
33 renderdata.FlushAllMemory() | |
1203 | 34 |
35 var | |
1204 | 36 pipeline = CreatePipeline[TrianglShader](renderPass = mainRenderpass, samples = swapchain.samples) |
1203 | 37 |
1204 | 38 var c = 0 |
39 while UpdateInputs() and c < nFrames: | |
1203 | 40 WithNextFrame(swapchain, framebuffer, commandbuffer): |
41 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | |
42 WithPipeline(commandbuffer, pipeline): | |
1204 | 43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
44 inc c | |
1203 | 45 |
46 # cleanup | |
1204 | 47 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1203 | 48 DestroyPipeline(pipeline) |
49 DestroyRenderData(renderdata) | |
50 | |
51 | |
1204 | 52 proc test_02_triangle_quad_instanced(nFrames: int) = |
53 var renderdata = InitRenderData() | |
54 | |
55 type | |
56 SomeShader = object | |
57 position {.VertexAttribute.}: Vec3f | |
58 color {.VertexAttribute.}: Vec3f | |
59 pos {.InstanceAttribute.}: Vec3f | |
60 scale {.InstanceAttribute.}: float32 | |
61 fragmentColor {.Pass.}: Vec3f | |
62 outColor {.ShaderOutput.}: Vec4f | |
63 # code | |
64 vertexCode: string = """void main() { | |
65 fragmentColor = color; | |
66 gl_Position = vec4((position * scale) + pos, 1);}""" | |
67 fragmentCode: string = """void main() { | |
68 outColor = vec4(fragmentColor, 1);}""" | |
69 TriangleMesh = object | |
70 position: GPUArray[Vec3f, VertexBuffer] | |
71 color: GPUArray[Vec3f, VertexBuffer] | |
72 QuadMesh = object | |
73 position: GPUArray[Vec3f, VertexBuffer] | |
74 color: GPUArray[Vec3f, VertexBuffer] | |
75 indices: GPUArray[uint16, IndexBuffer] | |
76 Instances = object | |
77 pos: GPUArray[Vec3f, VertexBuffer] | |
78 scale: GPUArray[float32, VertexBuffer] | |
79 var tri = TriangleMesh( | |
80 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), | |
81 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), | |
82 ) | |
83 var quad = QuadMesh( | |
84 position: asGPUArray([NewVec3f(-0.3, -0.3), NewVec3f(-0.3, 0.3), NewVec3f(0.3, 0.3), NewVec3f(0.3, -0.3)], VertexBuffer), | |
85 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | |
86 color: asGPUArray([NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1)], VertexBuffer), | |
87 ) | |
88 | |
89 var instancesA: Instances | |
90 for n in 1..100: | |
91 instancesA.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32)) | |
92 instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32) | |
93 var instancesB: Instances | |
94 for n in 1..100: | |
95 instancesB.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32)) | |
96 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) | |
97 | |
98 AssignBuffers(renderdata, tri) | |
99 AssignBuffers(renderdata, quad) | |
100 AssignBuffers(renderdata, instancesA) | |
101 AssignBuffers(renderdata, instancesB) | |
102 renderdata.FlushAllMemory() | |
103 | |
104 var pipeline = CreatePipeline[SomeShader](renderPass = mainRenderpass, samples = swapchain.samples) | |
105 | |
106 var c = 0 | |
107 while UpdateInputs() and c < nFrames: | |
108 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
109 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | |
110 WithPipeline(commandbuffer, pipeline): | |
111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) | |
112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) | |
113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) | |
114 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) | |
115 inc c | |
116 | |
117 # cleanup | |
118 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
119 DestroyPipeline(pipeline) | |
120 DestroyRenderData(renderdata) | |
121 | |
122 proc test_03_global_descriptorset(nFrames: int) = | |
123 var renderdata = InitRenderData() | |
1203 | 124 |
1204 | 125 type |
126 RenderSettings = object | |
1209 | 127 brigthness: float32 |
128 ObjectSettings = object | |
129 baseColor: Vec3f | |
130 MainSet = object | |
131 renderSettings: GPUValue[RenderSettings, UniformBufferMapped] | |
132 materialSettings: GPUValue[ObjectSettings, UniformBuffer] | |
133 OtherSet = object | |
134 # TODO | |
135 | |
1204 | 136 QuadShader = object |
137 position {.VertexAttribute.}: Vec3f | |
138 fragmentColor {.Pass.}: Vec3f | |
139 outColor {.ShaderOutput.}: Vec4f | |
1209 | 140 descriptorSets {.DescriptorSets.}: (MainSet, OtherSet) |
1204 | 141 # code |
142 vertexCode: string = """void main() { | |
1209 | 143 fragmentColor = materialSettings.baseColor * renderSettings.brigthness; |
1204 | 144 gl_Position = vec4(position, 1);}""" |
145 fragmentCode: string = """void main() { | |
146 outColor = vec4(fragmentColor, 1);}""" | |
147 QuadMesh = object | |
148 position: GPUArray[Vec3f, VertexBuffer] | |
149 indices: GPUArray[uint16, IndexBuffer] | |
1203 | 150 |
1204 | 151 var quad = QuadMesh( |
1209 | 152 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), |
1204 | 153 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
1209 | 154 ) |
155 var mainSet = asDescriptorSet( | |
156 MainSet( | |
157 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped), | |
158 materialSettings: asGPUValue(ObjectSettings(baseColor: NewVec3f(1, 1, 0)), UniformBuffer), | |
159 ) | |
1204 | 160 ) |
1209 | 161 var settings = asDescriptorSet( |
162 OtherSet( | |
163 # TODO | |
164 ) | |
1204 | 165 ) |
1209 | 166 |
1204 | 167 AssignBuffers(renderdata, quad) |
1209 | 168 AssignBuffers(renderdata, mainSet) |
1204 | 169 renderdata.FlushAllMemory() |
170 | |
171 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples) | |
172 | |
1209 | 173 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], mainSet) |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
174 |
1204 | 175 var c = 0 |
176 while UpdateInputs() and c < nFrames: | |
177 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
1209 | 178 WithBind(commandbuffer, (mainSet, ), pipeline, swapchain.currentFiF): |
1204 | 179 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
180 WithPipeline(commandbuffer, pipeline): | |
1209 | 181 Render(commandbuffer = commandbuffer, pipeline = pipeline, firstSet = mainSet, mesh = quad) |
182 mainSet.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32) | |
183 UpdateGPUBuffer(mainSet.data.renderSettings) | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
184 renderdata.FlushAllMemory() |
1204 | 185 inc c |
186 | |
187 # cleanup | |
1203 | 188 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1204 | 189 DestroyPipeline(pipeline) |
190 DestroyRenderData(renderdata) | |
191 | |
192 when isMainModule: | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
193 var nFrames = 5000 |
1204 | 194 InitVulkan() |
195 | |
196 # test normal | |
197 block: | |
198 mainRenderpass = CreatePresentationRenderPass() | |
199 swapchain = InitSwapchain(renderpass = mainRenderpass).get() | |
200 | |
201 # tests a simple triangle with minimalistic shader and vertex format | |
1209 | 202 # test_01_triangle(nFrames) |
1204 | 203 |
204 # tests instanced triangles and quads, mixing meshes and instances | |
1209 | 205 # test_02_triangle_quad_instanced(nFrames) |
1204 | 206 |
207 # tests | |
208 test_03_global_descriptorset(nFrames) | |
209 | |
210 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
211 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) | |
212 DestroySwapchain(swapchain) | |
213 | |
214 # test MSAA | |
215 block: | |
216 mainRenderpass = CreatePresentationRenderPass(samples = VK_SAMPLE_COUNT_4_BIT) | |
217 swapchain = InitSwapchain(renderpass = mainRenderpass, samples = VK_SAMPLE_COUNT_4_BIT).get() | |
218 | |
1209 | 219 # test_01_triangle(99999999) |
1204 | 220 |
221 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
222 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) | |
223 DestroySwapchain(swapchain) | |
224 | |
1203 | 225 DestroyVulkan() |