Mercurial > games > semicongine
annotate tests/test_rendering.nim @ 1205:f7530247a21f compiletime-tests
did: improve descriptor-set handling, add simple descriptor set test
author | sam <sam@basx.dev> |
---|---|
date | Tue, 16 Jul 2024 11:53:43 +0700 |
parents | e2901100a596 |
children | 8cf9a05b03eb |
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 | |
127 gamma: float32 | |
128 FirstDS = object | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
129 settings: GPUValue[RenderSettings, UniformBufferMapped] |
1204 | 130 QuadShader = object |
131 position {.VertexAttribute.}: Vec3f | |
132 color {.VertexAttribute.}: Vec3f | |
133 fragmentColor {.Pass.}: Vec3f | |
134 outColor {.ShaderOutput.}: Vec4f | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
135 descriptorSets {.DescriptorSets.}: (FirstDS, ) |
1204 | 136 # code |
137 vertexCode: string = """void main() { | |
138 fragmentColor = vec3(pow(color.r, settings.gamma), pow(color.g, settings.gamma), pow(color.b, settings.gamma)); | |
139 gl_Position = vec4(position, 1);}""" | |
140 fragmentCode: string = """void main() { | |
141 outColor = vec4(fragmentColor, 1);}""" | |
142 QuadMesh = object | |
143 position: GPUArray[Vec3f, VertexBuffer] | |
144 color: GPUArray[Vec3f, VertexBuffer] | |
145 indices: GPUArray[uint16, IndexBuffer] | |
1203 | 146 |
1204 | 147 var quad = QuadMesh( |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
148 position: asGPUArray([NewVec3f(-0.9, -0.5), NewVec3f(-0.9, 0.5), NewVec3f(0.9, 0.5), NewVec3f(0.9, -0.5)], VertexBuffer), |
1204 | 149 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
150 color: asGPUArray([NewVec3f(0, 0, 0), NewVec3f(0, 0, 0), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1)], VertexBuffer), |
1204 | 151 ) |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
152 var settings = DescriptorSet[FirstDS]( |
1204 | 153 data: FirstDS( |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
154 settings: asGPUValue(RenderSettings( |
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
155 gamma: 0.01'f32 |
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
156 ), UniformBufferMapped) |
1204 | 157 ) |
158 ) | |
159 AssignBuffers(renderdata, quad) | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
160 AssignBuffers(renderdata, settings) |
1204 | 161 renderdata.FlushAllMemory() |
162 | |
163 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples) | |
164 | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
165 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], settings) |
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
166 |
1204 | 167 var c = 0 |
168 while UpdateInputs() and c < nFrames: | |
169 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
170 WithBind(commandbuffer, (settings, ), pipeline, swapchain.currentFiF): |
1204 | 171 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
172 WithPipeline(commandbuffer, pipeline): | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
173 Render(commandbuffer = commandbuffer, pipeline = pipeline, firstSet = settings, mesh = quad) |
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
174 settings.data.settings.data.gamma = 0.01'f32 + (c.float32 / nFrames.float32) * 5'f32 |
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
175 UpdateGPUBuffer(settings.data.settings) |
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
176 renderdata.FlushAllMemory() |
1204 | 177 inc c |
178 | |
179 # cleanup | |
1203 | 180 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1204 | 181 DestroyPipeline(pipeline) |
182 DestroyRenderData(renderdata) | |
183 | |
184 when isMainModule: | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
185 var nFrames = 5000 |
1204 | 186 InitVulkan() |
187 | |
188 # test normal | |
189 block: | |
190 mainRenderpass = CreatePresentationRenderPass() | |
191 swapchain = InitSwapchain(renderpass = mainRenderpass).get() | |
192 | |
193 # tests a simple triangle with minimalistic shader and vertex format | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
194 # test_01_triangle(nFrames) |
1204 | 195 |
196 # tests instanced triangles and quads, mixing meshes and instances | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
197 # test_02_triangle_quad_instanced(nFrames) |
1204 | 198 |
199 # tests | |
200 test_03_global_descriptorset(nFrames) | |
201 | |
202 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
203 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) | |
204 DestroySwapchain(swapchain) | |
205 | |
206 # test MSAA | |
207 block: | |
208 mainRenderpass = CreatePresentationRenderPass(samples = VK_SAMPLE_COUNT_4_BIT) | |
209 swapchain = InitSwapchain(renderpass = mainRenderpass, samples = VK_SAMPLE_COUNT_4_BIT).get() | |
210 | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
211 # test_01_triangle(nFrames) |
1204 | 212 |
213 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
214 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) | |
215 DestroySwapchain(swapchain) | |
216 | |
1203 | 217 DestroyVulkan() |