Mercurial > games > semicongine
comparison tests/test_rendering.nim @ 1283:0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
author | sam <sam@basx.dev> |
---|---|
date | Mon, 29 Jul 2024 16:50:50 +0700 |
parents | a89a70ea3da2 |
children | 6d16003406fb |
comparison
equal
deleted
inserted
replaced
1282:3308b88e53a6 | 1283:0369fa1ffbd9 |
---|---|
6 import std/random | 6 import std/random |
7 | 7 |
8 import ../semicongine | 8 import ../semicongine |
9 | 9 |
10 proc test_01_triangle(time: float32) = | 10 proc test_01_triangle(time: float32) = |
11 var renderdata = InitRenderData() | 11 var renderdata = initRenderData() |
12 | 12 |
13 type | 13 type |
14 PushConstant = object | 14 PushConstant = object |
15 scale: float32 | 15 scale: float32 |
16 Shader = object | 16 Shader = object |
30 color: GPUArray[Vec3f, VertexBuffer] | 30 color: GPUArray[Vec3f, VertexBuffer] |
31 var mesh = TriangleMesh( | 31 var mesh = TriangleMesh( |
32 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), | 32 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), |
33 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), | 33 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), |
34 ) | 34 ) |
35 AssignBuffers(renderdata, mesh) | 35 assignBuffers(renderdata, mesh) |
36 renderdata.FlushAllMemory() | 36 renderdata.flushAllMemory() |
37 | 37 |
38 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) | 38 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass) |
39 | 39 |
40 var start = getMonoTime() | 40 var start = getMonoTime() |
41 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 41 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
42 | 42 |
43 WithNextFrame(framebuffer, commandbuffer): | 43 withNextFrame(framebuffer, commandbuffer): |
44 | 44 |
45 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 45 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
46 | 46 |
47 WithPipeline(commandbuffer, pipeline): | 47 withPipeline(commandbuffer, pipeline): |
48 | 48 |
49 RenderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh, pushConstant = PushConstant(scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000))) | 49 renderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh, pushConstant = PushConstant(scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000))) |
50 | 50 |
51 # cleanup | 51 # cleanup |
52 checkVkResult vkDeviceWaitIdle(vulkan.device) | 52 checkVkResult vkDeviceWaitIdle(vulkan.device) |
53 DestroyPipeline(pipeline) | 53 destroyPipeline(pipeline) |
54 DestroyRenderData(renderdata) | 54 destroyRenderData(renderdata) |
55 | 55 |
56 | 56 |
57 proc test_02_triangle_quad_instanced(time: float32) = | 57 proc test_02_triangle_quad_instanced(time: float32) = |
58 var renderdata = InitRenderData() | 58 var renderdata = initRenderData() |
59 | 59 |
60 type | 60 type |
61 SomeShader = object | 61 SomeShader = object |
62 position {.VertexAttribute.}: Vec3f | 62 position {.VertexAttribute.}: Vec3f |
63 color {.VertexAttribute.}: Vec3f | 63 color {.VertexAttribute.}: Vec3f |
98 var instancesB: Instances | 98 var instancesB: Instances |
99 for n in 1..100: | 99 for n in 1..100: |
100 instancesB.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32), 0) | 100 instancesB.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32), 0) |
101 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) | 101 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) |
102 | 102 |
103 AssignBuffers(renderdata, tri) | 103 assignBuffers(renderdata, tri) |
104 AssignBuffers(renderdata, quad) | 104 assignBuffers(renderdata, quad) |
105 AssignBuffers(renderdata, instancesA) | 105 assignBuffers(renderdata, instancesA) |
106 AssignBuffers(renderdata, instancesB) | 106 assignBuffers(renderdata, instancesB) |
107 renderdata.FlushAllMemory() | 107 renderdata.flushAllMemory() |
108 | 108 |
109 var pipeline = CreatePipeline[SomeShader](renderPass = vulkan.swapchain.renderPass) | 109 var pipeline = createPipeline[SomeShader](renderPass = vulkan.swapchain.renderPass) |
110 | 110 |
111 var start = getMonoTime() | 111 var start = getMonoTime() |
112 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 112 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
113 | 113 |
114 WithNextFrame(framebuffer, commandbuffer): | 114 withNextFrame(framebuffer, commandbuffer): |
115 | 115 |
116 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 116 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
117 | 117 |
118 WithPipeline(commandbuffer, pipeline): | 118 withPipeline(commandbuffer, pipeline): |
119 | 119 |
120 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) | 120 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) |
121 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) | 121 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) |
122 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) | 122 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) |
123 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) | 123 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) |
124 | 124 |
125 # cleanup | 125 # cleanup |
126 checkVkResult vkDeviceWaitIdle(vulkan.device) | 126 checkVkResult vkDeviceWaitIdle(vulkan.device) |
127 DestroyPipeline(pipeline) | 127 destroyPipeline(pipeline) |
128 DestroyRenderData(renderdata) | 128 destroyRenderData(renderdata) |
129 | 129 |
130 proc test_03_simple_descriptorset(time: float32) = | 130 proc test_03_simple_descriptorset(time: float32) = |
131 var renderdata = InitRenderData() | 131 var renderdata = initRenderData() |
132 | 132 |
133 type | 133 type |
134 Material = object | 134 Material = object |
135 baseColor: Vec3f | 135 baseColor: Vec3f |
136 | 136 |
177 material: asGPUValue(Material(baseColor: vec3(0.5, 0.5, 0.5)), UniformBuffer), | 177 material: asGPUValue(Material(baseColor: vec3(0.5, 0.5, 0.5)), UniformBuffer), |
178 texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]), | 178 texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]), |
179 ) | 179 ) |
180 ) | 180 ) |
181 | 181 |
182 AssignBuffers(renderdata, quad) | 182 assignBuffers(renderdata, quad) |
183 AssignBuffers(renderdata, uniforms1) | 183 assignBuffers(renderdata, uniforms1) |
184 AssignBuffers(renderdata, uniforms2) | 184 assignBuffers(renderdata, uniforms2) |
185 UploadImages(renderdata, uniforms1) | 185 uploadImages(renderdata, uniforms1) |
186 UploadImages(renderdata, uniforms2) | 186 uploadImages(renderdata, uniforms2) |
187 renderdata.FlushAllMemory() | 187 renderdata.flushAllMemory() |
188 | 188 |
189 var pipeline = CreatePipeline[QuadShader](renderPass = vulkan.swapchain.renderPass) | 189 var pipeline = createPipeline[QuadShader](renderPass = vulkan.swapchain.renderPass) |
190 | 190 |
191 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | 191 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) |
192 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) | 192 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) |
193 | 193 |
194 var start = getMonoTime() | 194 var start = getMonoTime() |
195 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 195 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
196 | 196 |
197 WithNextFrame(framebuffer, commandbuffer): | 197 withNextFrame(framebuffer, commandbuffer): |
198 | 198 |
199 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 199 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
200 | 200 |
201 WithPipeline(commandbuffer, pipeline): | 201 withPipeline(commandbuffer, pipeline): |
202 | 202 |
203 WithBind(commandbuffer, (uniforms1, ), pipeline): | 203 withBind(commandbuffer, (uniforms1, ), pipeline): |
204 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 204 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
205 | 205 |
206 WithBind(commandbuffer, (uniforms2, ), pipeline): | 206 withBind(commandbuffer, (uniforms2, ), pipeline): |
207 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 207 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
208 | 208 |
209 # cleanup | 209 # cleanup |
210 checkVkResult vkDeviceWaitIdle(vulkan.device) | 210 checkVkResult vkDeviceWaitIdle(vulkan.device) |
211 DestroyPipeline(pipeline) | 211 destroyPipeline(pipeline) |
212 DestroyRenderData(renderdata) | 212 destroyRenderData(renderdata) |
213 | 213 |
214 proc test_04_multiple_descriptorsets(time: float32) = | 214 proc test_04_multiple_descriptorsets(time: float32) = |
215 var renderdata = InitRenderData() | 215 var renderdata = initRenderData() |
216 | 216 |
217 type | 217 type |
218 RenderSettings = object | 218 RenderSettings = object |
219 brigthness: float32 | 219 brigthness: float32 |
220 Material = object | 220 Material = object |
288 OtherSet( | 288 OtherSet( |
289 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped), | 289 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped), |
290 ) | 290 ) |
291 ) | 291 ) |
292 | 292 |
293 AssignBuffers(renderdata, quad) | 293 assignBuffers(renderdata, quad) |
294 AssignBuffers(renderdata, constset) | 294 assignBuffers(renderdata, constset) |
295 AssignBuffers(renderdata, mainset) | 295 assignBuffers(renderdata, mainset) |
296 AssignBuffers(renderdata, otherset1) | 296 assignBuffers(renderdata, otherset1) |
297 AssignBuffers(renderdata, otherset2) | 297 assignBuffers(renderdata, otherset2) |
298 UploadImages(renderdata, mainset) | 298 uploadImages(renderdata, mainset) |
299 renderdata.FlushAllMemory() | 299 renderdata.flushAllMemory() |
300 | 300 |
301 var pipeline = CreatePipeline[QuadShader](renderPass = vulkan.swapchain.renderPass) | 301 var pipeline = createPipeline[QuadShader](renderPass = vulkan.swapchain.renderPass) |
302 | 302 |
303 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset) | 303 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset) |
304 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset) | 304 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset) |
305 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) | 305 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) |
306 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) | 306 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) |
307 | 307 |
308 var start = getMonoTime() | 308 var start = getMonoTime() |
309 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 309 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
310 | 310 |
311 WithNextFrame(framebuffer, commandbuffer): | 311 withNextFrame(framebuffer, commandbuffer): |
312 | 312 |
313 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 313 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
314 | 314 |
315 WithPipeline(commandbuffer, pipeline): | 315 withPipeline(commandbuffer, pipeline): |
316 | 316 |
317 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline): | 317 withBind(commandbuffer, (constset, mainset, otherset1), pipeline): |
318 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 318 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
319 | 319 |
320 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline): | 320 withBind(commandbuffer, (constset, mainset, otherset2), pipeline): |
321 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 321 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
322 | 322 |
323 mainset.data.renderSettings.data.brigthness = ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 323 mainset.data.renderSettings.data.brigthness = ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
324 otherset1.data.objectSettings.data.scale = 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 324 otherset1.data.objectSettings.data.scale = 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
325 UpdateGPUBuffer(mainset.data.renderSettings) | 325 updateGPUBuffer(mainset.data.renderSettings) |
326 UpdateGPUBuffer(otherset1.data.objectSettings) | 326 updateGPUBuffer(otherset1.data.objectSettings) |
327 renderdata.FlushAllMemory() | 327 renderdata.flushAllMemory() |
328 | 328 |
329 # cleanup | 329 # cleanup |
330 checkVkResult vkDeviceWaitIdle(vulkan.device) | 330 checkVkResult vkDeviceWaitIdle(vulkan.device) |
331 DestroyPipeline(pipeline) | 331 destroyPipeline(pipeline) |
332 DestroyRenderData(renderdata) | 332 destroyRenderData(renderdata) |
333 | 333 |
334 proc test_05_cube(time: float32) = | 334 proc test_05_cube(time: float32) = |
335 type | 335 type |
336 | 336 |
337 UniformData = object | 337 UniformData = object |
398 # top, yellow | 398 # top, yellow |
399 vertices.add quad.transf(rotate(-PI / 2, X) * translate(0, 0, -0.5)) | 399 vertices.add quad.transf(rotate(-PI / 2, X) * translate(0, 0, -0.5)) |
400 colors.add newSeqWith(6, vec4(1, 1, 0, 1)) | 400 colors.add newSeqWith(6, vec4(1, 1, 0, 1)) |
401 normals.add newSeqWith(6, vec3(0, 1, 0)) | 401 normals.add newSeqWith(6, vec3(0, 1, 0)) |
402 | 402 |
403 var renderdata = InitRenderData() | 403 var renderdata = initRenderData() |
404 | 404 |
405 var mesh = Mesh( | 405 var mesh = Mesh( |
406 position: asGPUArray(vertices, VertexBuffer), | 406 position: asGPUArray(vertices, VertexBuffer), |
407 color: asGPUArray(colors, VertexBuffer), | 407 color: asGPUArray(colors, VertexBuffer), |
408 normals: asGPUArray(normals, VertexBuffer), | 408 normals: asGPUArray(normals, VertexBuffer), |
409 ) | 409 ) |
410 AssignBuffers(renderdata, mesh) | 410 assignBuffers(renderdata, mesh) |
411 | 411 |
412 var floor = Mesh( | 412 var floor = Mesh( |
413 position: asGPUArray(quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), VertexBuffer), | 413 position: asGPUArray(quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), VertexBuffer), |
414 color: asGPUArray(newSeqWith(6, vec4(0.1, 0.1, 0.1, 1)), VertexBuffer), | 414 color: asGPUArray(newSeqWith(6, vec4(0.1, 0.1, 0.1, 1)), VertexBuffer), |
415 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), | 415 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), |
416 ) | 416 ) |
417 AssignBuffers(renderdata, floor) | 417 assignBuffers(renderdata, floor) |
418 | 418 |
419 var uniforms1 = asDescriptorSet( | 419 var uniforms1 = asDescriptorSet( |
420 Uniforms( | 420 Uniforms( |
421 data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped) | 421 data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped) |
422 ) | 422 ) |
423 ) | 423 ) |
424 AssignBuffers(renderdata, uniforms1) | 424 assignBuffers(renderdata, uniforms1) |
425 | 425 |
426 renderdata.FlushAllMemory() | 426 renderdata.flushAllMemory() |
427 | 427 |
428 var pipeline = CreatePipeline[CubeShader](renderPass = vulkan.swapchain.renderPass) | 428 var pipeline = createPipeline[CubeShader](renderPass = vulkan.swapchain.renderPass) |
429 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | 429 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) |
430 | 430 |
431 var tStart = getMonoTime() | 431 var tStart = getMonoTime() |
432 var t = tStart | 432 var t = tStart |
433 | 433 |
434 var start = getMonoTime() | 434 var start = getMonoTime() |
435 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 435 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
436 let tStartLoop = getMonoTime() - tStart | 436 let tStartLoop = getMonoTime() - tStart |
437 | 437 |
438 uniforms1.data.data.data.mvp = ( | 438 uniforms1.data.data.data.mvp = ( |
439 projection(-PI / 2, GetAspectRatio(), 0.01, 100) * | 439 projection(-PI / 2, getAspectRatio(), 0.01, 100) * |
440 translate(0, 0, 2) * | 440 translate(0, 0, 2) * |
441 rotate(PI / 4, X) * | 441 rotate(PI / 4, X) * |
442 rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y) | 442 rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y) |
443 ) | 443 ) |
444 UpdateGPUBuffer(uniforms1.data.data, flush = true) | 444 updateGPUBuffer(uniforms1.data.data, flush = true) |
445 | 445 |
446 WithNextFrame(framebuffer, commandbuffer): | 446 withNextFrame(framebuffer, commandbuffer): |
447 | 447 |
448 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 448 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
449 WithPipeline(commandbuffer, pipeline): | 449 withPipeline(commandbuffer, pipeline): |
450 | 450 |
451 WithBind(commandbuffer, (uniforms1, ), pipeline): | 451 withBind(commandbuffer, (uniforms1, ), pipeline): |
452 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) | 452 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
453 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor) | 453 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor) |
454 | 454 |
455 let tEndLoop = getMonoTime() - tStart | 455 let tEndLoop = getMonoTime() - tStart |
456 let looptime = tEndLoop - tStartLoop | 456 let looptime = tEndLoop - tStartLoop |
457 let waitTime = 16_666 - looptime.inMicroseconds | 457 let waitTime = 16_666 - looptime.inMicroseconds |
458 if waitTime > 0: | 458 if waitTime > 0: |
459 sleep((waitTime / 1000).int) | 459 sleep((waitTime / 1000).int) |
460 | 460 |
461 # cleanup | 461 # cleanup |
462 checkVkResult vkDeviceWaitIdle(vulkan.device) | 462 checkVkResult vkDeviceWaitIdle(vulkan.device) |
463 DestroyPipeline(pipeline) | 463 destroyPipeline(pipeline) |
464 DestroyRenderData(renderdata) | 464 destroyRenderData(renderdata) |
465 | 465 |
466 proc test_06_different_draw_modes(time: float32) = | 466 proc test_06_different_draw_modes(time: float32) = |
467 var renderdata = InitRenderData() | 467 var renderdata = initRenderData() |
468 | 468 |
469 type | 469 type |
470 Shader = object | 470 Shader = object |
471 position {.VertexAttribute.}: Vec3f | 471 position {.VertexAttribute.}: Vec3f |
472 color {.VertexAttribute.}: Vec3f | 472 color {.VertexAttribute.}: Vec3f |
488 ) | 488 ) |
489 var lines = TriangleMesh( | 489 var lines = TriangleMesh( |
490 position: asGPUArray([vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], VertexBuffer), | 490 position: asGPUArray([vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], VertexBuffer), |
491 color: asGPUArray([vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer), | 491 color: asGPUArray([vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer), |
492 ) | 492 ) |
493 AssignBuffers(renderdata, triangle) | 493 assignBuffers(renderdata, triangle) |
494 AssignBuffers(renderdata, lines) | 494 assignBuffers(renderdata, lines) |
495 renderdata.FlushAllMemory() | 495 renderdata.flushAllMemory() |
496 | 496 |
497 var pipeline1 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_LINE, lineWidth = 20'f32) | 497 var pipeline1 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_LINE, lineWidth = 20'f32) |
498 var pipeline2 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT) | 498 var pipeline2 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT) |
499 var pipeline3 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, lineWidth = 5) | 499 var pipeline3 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, lineWidth = 5) |
500 var pipeline4 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST) | 500 var pipeline4 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST) |
501 | 501 |
502 var start = getMonoTime() | 502 var start = getMonoTime() |
503 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 503 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
504 WithNextFrame(framebuffer, commandbuffer): | 504 withNextFrame(framebuffer, commandbuffer): |
505 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 505 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
506 WithPipeline(commandbuffer, pipeline1): | 506 withPipeline(commandbuffer, pipeline1): |
507 Render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle) | 507 render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle) |
508 WithPipeline(commandbuffer, pipeline2): | 508 withPipeline(commandbuffer, pipeline2): |
509 Render(commandbuffer = commandbuffer, pipeline = pipeline2, mesh = triangle) | 509 render(commandbuffer = commandbuffer, pipeline = pipeline2, mesh = triangle) |
510 WithPipeline(commandbuffer, pipeline3): | 510 withPipeline(commandbuffer, pipeline3): |
511 Render(commandbuffer = commandbuffer, pipeline = pipeline3, mesh = lines) | 511 render(commandbuffer = commandbuffer, pipeline = pipeline3, mesh = lines) |
512 WithPipeline(commandbuffer, pipeline4): | 512 withPipeline(commandbuffer, pipeline4): |
513 Render(commandbuffer = commandbuffer, pipeline = pipeline4, mesh = lines) | 513 render(commandbuffer = commandbuffer, pipeline = pipeline4, mesh = lines) |
514 | 514 |
515 # cleanup | 515 # cleanup |
516 checkVkResult vkDeviceWaitIdle(vulkan.device) | 516 checkVkResult vkDeviceWaitIdle(vulkan.device) |
517 DestroyPipeline(pipeline1) | 517 destroyPipeline(pipeline1) |
518 DestroyPipeline(pipeline2) | 518 destroyPipeline(pipeline2) |
519 DestroyPipeline(pipeline3) | 519 destroyPipeline(pipeline3) |
520 DestroyPipeline(pipeline4) | 520 destroyPipeline(pipeline4) |
521 DestroyRenderData(renderdata) | 521 destroyRenderData(renderdata) |
522 | 522 |
523 proc test_07_png_texture(time: float32) = | 523 proc test_07_png_texture(time: float32) = |
524 var renderdata = InitRenderData() | 524 var renderdata = initRenderData() |
525 | 525 |
526 type | 526 type |
527 Uniforms = object | 527 Uniforms = object |
528 texture1: Image[BGRA] | 528 texture1: Image[BGRA] |
529 Shader = object | 529 Shader = object |
553 uv: asGPUArray([ | 553 uv: asGPUArray([ |
554 vec2(0, 1), vec2(0, 0), vec2(1, 0), | 554 vec2(0, 1), vec2(0, 0), vec2(1, 0), |
555 vec2(1, 0), vec2(1, 1), vec2(0, 1), | 555 vec2(1, 0), vec2(1, 1), vec2(0, 1), |
556 ], VertexBuffer), | 556 ], VertexBuffer), |
557 ) | 557 ) |
558 AssignBuffers(renderdata, mesh) | 558 assignBuffers(renderdata, mesh) |
559 renderdata.FlushAllMemory() | 559 renderdata.flushAllMemory() |
560 | 560 |
561 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) | 561 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass) |
562 var uniforms1 = asDescriptorSet( | 562 var uniforms1 = asDescriptorSet( |
563 Uniforms( | 563 Uniforms( |
564 texture1: LoadImage[BGRA]("art.png"), | 564 texture1: loadImage[BGRA]("art.png"), |
565 ) | 565 ) |
566 ) | 566 ) |
567 UploadImages(renderdata, uniforms1) | 567 uploadImages(renderdata, uniforms1) |
568 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | 568 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) |
569 | 569 |
570 var start = getMonoTime() | 570 var start = getMonoTime() |
571 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 571 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
572 | 572 |
573 WithNextFrame(framebuffer, commandbuffer): | 573 withNextFrame(framebuffer, commandbuffer): |
574 | 574 |
575 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 575 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
576 | 576 |
577 WithPipeline(commandbuffer, pipeline): | 577 withPipeline(commandbuffer, pipeline): |
578 | 578 |
579 WithBind(commandbuffer, (uniforms1, ), pipeline): | 579 withBind(commandbuffer, (uniforms1, ), pipeline): |
580 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) | 580 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
581 | 581 |
582 # cleanup | 582 # cleanup |
583 checkVkResult vkDeviceWaitIdle(vulkan.device) | 583 checkVkResult vkDeviceWaitIdle(vulkan.device) |
584 DestroyPipeline(pipeline) | 584 destroyPipeline(pipeline) |
585 DestroyRenderData(renderdata) | 585 destroyRenderData(renderdata) |
586 | 586 |
587 proc test_08_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) = | 587 proc test_08_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) = |
588 var (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 588 var (offscreenRP, presentRP) = createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
589 | 589 |
590 SetupSwapchain(renderpass = presentRP) | 590 setupSwapchain(renderpass = presentRP) |
591 | 591 |
592 var renderdata = InitRenderData() | 592 var renderdata = initRenderData() |
593 | 593 |
594 type | 594 type |
595 Uniforms = object | 595 Uniforms = object |
596 frameTexture: Image[BGRA] | 596 frameTexture: Image[BGRA] |
597 TriangleShader = object | 597 TriangleShader = object |
643 var uniforms1 = asDescriptorSet( | 643 var uniforms1 = asDescriptorSet( |
644 Uniforms( | 644 Uniforms( |
645 frameTexture: Image[BGRA](width: vulkan.swapchain.width, height: vulkan.swapchain.height, isRenderTarget: true), | 645 frameTexture: Image[BGRA](width: vulkan.swapchain.width, height: vulkan.swapchain.height, isRenderTarget: true), |
646 ) | 646 ) |
647 ) | 647 ) |
648 AssignBuffers(renderdata, mesh) | 648 assignBuffers(renderdata, mesh) |
649 AssignBuffers(renderdata, quad) | 649 assignBuffers(renderdata, quad) |
650 UploadImages(renderdata, uniforms1) | 650 uploadImages(renderdata, uniforms1) |
651 renderdata.FlushAllMemory() | 651 renderdata.flushAllMemory() |
652 | 652 |
653 var | 653 var |
654 drawPipeline = CreatePipeline[TriangleShader](renderPass = offscreenRP) | 654 drawPipeline = createPipeline[TriangleShader](renderPass = offscreenRP) |
655 presentPipeline = CreatePipeline[PresentShader](renderPass = presentRP) | 655 presentPipeline = createPipeline[PresentShader](renderPass = presentRP) |
656 | 656 |
657 InitDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms1) | 657 initDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms1) |
658 | 658 |
659 # create depth buffer images (will not use the one in the swapchain | 659 # create depth buffer images (will not use the one in the swapchain |
660 var | 660 var |
661 depthImage: VkImage | 661 depthImage: VkImage |
662 depthImageView: VkImageView | 662 depthImageView: VkImageView |
670 samples = offscreenRP.samples, | 670 samples = offscreenRP.samples, |
671 ) | 671 ) |
672 let requirements = svkGetImageMemoryRequirements(depthImage) | 672 let requirements = svkGetImageMemoryRequirements(depthImage) |
673 depthMemory = svkAllocateMemory( | 673 depthMemory = svkAllocateMemory( |
674 requirements.size, | 674 requirements.size, |
675 BestMemory(mappable = false, filter = requirements.memoryTypes) | 675 bestMemory(mappable = false, filter = requirements.memoryTypes) |
676 ) | 676 ) |
677 checkVkResult vkBindImageMemory( | 677 checkVkResult vkBindImageMemory( |
678 vulkan.device, | 678 vulkan.device, |
679 depthImage, | 679 depthImage, |
680 depthMemory, | 680 depthMemory, |
700 samples = offscreenRP.samples, | 700 samples = offscreenRP.samples, |
701 ) | 701 ) |
702 let requirements = svkGetImageMemoryRequirements(msaaImage) | 702 let requirements = svkGetImageMemoryRequirements(msaaImage) |
703 msaaMemory = svkAllocateMemory( | 703 msaaMemory = svkAllocateMemory( |
704 requirements.size, | 704 requirements.size, |
705 BestMemory(mappable = false, filter = requirements.memoryTypes) | 705 bestMemory(mappable = false, filter = requirements.memoryTypes) |
706 ) | 706 ) |
707 checkVkResult vkBindImageMemory( | 707 checkVkResult vkBindImageMemory( |
708 vulkan.device, | 708 vulkan.device, |
709 msaaImage, | 709 msaaImage, |
710 msaaMemory, | 710 msaaMemory, |
731 ) | 731 ) |
732 | 732 |
733 var start = getMonoTime() | 733 var start = getMonoTime() |
734 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 734 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
735 | 735 |
736 WithNextFrame(framebuffer, commandbuffer): | 736 withNextFrame(framebuffer, commandbuffer): |
737 | 737 |
738 WithRenderPass(offscreenRP, offscreenFB, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 738 withRenderPass(offscreenRP, offscreenFB, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
739 WithPipeline(commandbuffer, drawPipeline): | 739 withPipeline(commandbuffer, drawPipeline): |
740 Render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) | 740 render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) |
741 | 741 |
742 WithRenderPass(presentRP, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 742 withRenderPass(presentRP, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
743 | 743 |
744 WithPipeline(commandbuffer, presentPipeline): | 744 withPipeline(commandbuffer, presentPipeline): |
745 | 745 |
746 WithBind(commandbuffer, (uniforms1, ), presentPipeline): | 746 withBind(commandbuffer, (uniforms1, ), presentPipeline): |
747 Render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) | 747 render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) |
748 | 748 |
749 # cleanup | 749 # cleanup |
750 checkVkResult vkDeviceWaitIdle(vulkan.device) | 750 checkVkResult vkDeviceWaitIdle(vulkan.device) |
751 DestroyPipeline(presentPipeline) | 751 destroyPipeline(presentPipeline) |
752 DestroyPipeline(drawPipeline) | 752 destroyPipeline(drawPipeline) |
753 DestroyRenderData(renderdata) | 753 destroyRenderData(renderdata) |
754 if depthImage.Valid: | 754 if depthImage.Valid: |
755 vkDestroyImageView(vulkan.device, depthImageView, nil) | 755 vkDestroyImageView(vulkan.device, depthImageView, nil) |
756 vkDestroyImage(vulkan.device, depthImage, nil) | 756 vkDestroyImage(vulkan.device, depthImage, nil) |
757 vkFreeMemory(vulkan.device, depthMemory, nil) | 757 vkFreeMemory(vulkan.device, depthMemory, nil) |
758 if msaaImage.Valid: | 758 if msaaImage.Valid: |
760 vkDestroyImage(vulkan.device, msaaImage, nil) | 760 vkDestroyImage(vulkan.device, msaaImage, nil) |
761 vkFreeMemory(vulkan.device, msaaMemory, nil) | 761 vkFreeMemory(vulkan.device, msaaMemory, nil) |
762 vkDestroyRenderPass(vulkan.device, offscreenRP.vk, nil) | 762 vkDestroyRenderPass(vulkan.device, offscreenRP.vk, nil) |
763 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil) | 763 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil) |
764 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) | 764 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) |
765 ClearSwapchain() | 765 clearSwapchain() |
766 | 766 |
767 when isMainModule: | 767 when isMainModule: |
768 var time = 1'f32 | 768 var time = 1'f32 |
769 InitVulkan() | 769 initVulkan() |
770 | 770 |
771 var mainRenderpass: RenderPass | 771 var mainRenderpass: RenderPass |
772 var renderPasses = [ | 772 var renderPasses = [ |
773 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), | 773 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), |
774 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), | 774 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), |
777 ] | 777 ] |
778 | 778 |
779 | 779 |
780 # test normal | 780 # test normal |
781 for i, (depthBuffer, samples) in renderPasses: | 781 for i, (depthBuffer, samples) in renderPasses: |
782 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 782 var renderpass = createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
783 SetupSwapchain(renderpass = renderpass) | 783 setupSwapchain(renderpass = renderpass) |
784 | 784 |
785 # tests a simple triangle with minimalistic shader and vertex format | 785 # tests a simple triangle with minimalistic shader and vertex format |
786 test_01_triangle(time) | 786 test_01_triangle(time) |
787 | 787 |
788 # tests instanced triangles and quads, mixing meshes and instances | 788 # tests instanced triangles and quads, mixing meshes and instances |
803 # load PNG texture | 803 # load PNG texture |
804 test_07_png_texture(time) | 804 test_07_png_texture(time) |
805 | 805 |
806 checkVkResult vkDeviceWaitIdle(vulkan.device) | 806 checkVkResult vkDeviceWaitIdle(vulkan.device) |
807 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | 807 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
808 ClearSwapchain() | 808 clearSwapchain() |
809 | 809 |
810 # test multiple render passes | 810 # test multiple render passes |
811 for i, (depthBuffer, samples) in renderPasses: | 811 for i, (depthBuffer, samples) in renderPasses: |
812 test_08_triangle_2pass(time, depthBuffer, samples) | 812 test_08_triangle_2pass(time, depthBuffer, samples) |
813 | 813 |
814 DestroyVulkan() | 814 destroyVulkan() |