Mercurial > games > semicongine
comparison tests/test_rendering.nim @ 1235:c70fee6568f6
did: improv render tests to run without user input
author | sam <sam@basx.dev> |
---|---|
date | Sat, 20 Jul 2024 15:45:02 +0700 |
parents | 1cf698973dca |
children | 69489a678141 |
comparison
equal
deleted
inserted
replaced
1234:841e12f33c47 | 1235:c70fee6568f6 |
---|---|
5 import std/options | 5 import std/options |
6 import std/random | 6 import std/random |
7 | 7 |
8 import ../semiconginev2 | 8 import ../semiconginev2 |
9 | 9 |
10 proc test_01_triangle(nFrames: int, swapchain: var Swapchain) = | 10 proc test_01_triangle(time: float32, swapchain: var Swapchain) = |
11 var renderdata = InitRenderData() | 11 var renderdata = InitRenderData() |
12 | 12 |
13 type | 13 type |
14 TrianglShader = object | 14 TrianglShader = object |
15 position {.VertexAttribute.}: Vec3f | 15 position {.VertexAttribute.}: Vec3f |
33 renderdata.FlushAllMemory() | 33 renderdata.FlushAllMemory() |
34 | 34 |
35 var | 35 var |
36 pipeline = CreatePipeline[TrianglShader](renderPass = swapchain.renderPass) | 36 pipeline = CreatePipeline[TrianglShader](renderPass = swapchain.renderPass) |
37 | 37 |
38 var c = 0 | 38 var start = getMonoTime() |
39 while UpdateInputs() and c < nFrames: | 39 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
40 WithNextFrame(swapchain, framebuffer, commandbuffer): | 40 WithNextFrame(swapchain, framebuffer, commandbuffer): |
41 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 41 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
42 WithPipeline(commandbuffer, pipeline): | 42 WithPipeline(commandbuffer, pipeline): |
43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) | 43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
44 inc c | |
45 | 44 |
46 # cleanup | 45 # cleanup |
47 checkVkResult vkDeviceWaitIdle(vulkan.device) | 46 checkVkResult vkDeviceWaitIdle(vulkan.device) |
48 DestroyPipeline(pipeline) | 47 DestroyPipeline(pipeline) |
49 DestroyRenderData(renderdata) | 48 DestroyRenderData(renderdata) |
50 | 49 |
51 | 50 |
52 proc test_02_triangle_quad_instanced(nFrames: int, swapchain: var Swapchain) = | 51 proc test_02_triangle_quad_instanced(time: float32, swapchain: var Swapchain) = |
53 var renderdata = InitRenderData() | 52 var renderdata = InitRenderData() |
54 | 53 |
55 type | 54 type |
56 SomeShader = object | 55 SomeShader = object |
57 position {.VertexAttribute.}: Vec3f | 56 position {.VertexAttribute.}: Vec3f |
101 AssignBuffers(renderdata, instancesB) | 100 AssignBuffers(renderdata, instancesB) |
102 renderdata.FlushAllMemory() | 101 renderdata.FlushAllMemory() |
103 | 102 |
104 var pipeline = CreatePipeline[SomeShader](renderPass = swapchain.renderPass) | 103 var pipeline = CreatePipeline[SomeShader](renderPass = swapchain.renderPass) |
105 | 104 |
106 var c = 0 | 105 var start = getMonoTime() |
107 while UpdateInputs() and c < nFrames: | 106 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
108 WithNextFrame(swapchain, framebuffer, commandbuffer): | 107 WithNextFrame(swapchain, framebuffer, commandbuffer): |
109 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 108 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
110 WithPipeline(commandbuffer, pipeline): | 109 WithPipeline(commandbuffer, pipeline): |
111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) | 110 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) |
112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) | 111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) |
113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) | 112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) |
114 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) | 113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) |
115 inc c | |
116 | 114 |
117 # cleanup | 115 # cleanup |
118 checkVkResult vkDeviceWaitIdle(vulkan.device) | 116 checkVkResult vkDeviceWaitIdle(vulkan.device) |
119 DestroyPipeline(pipeline) | 117 DestroyPipeline(pipeline) |
120 DestroyRenderData(renderdata) | 118 DestroyRenderData(renderdata) |
121 | 119 |
122 proc test_03_simple_descriptorset(nFrames: int, swapchain: var Swapchain) = | 120 proc test_03_simple_descriptorset(time: float32, swapchain: var Swapchain) = |
123 var renderdata = InitRenderData() | 121 var renderdata = InitRenderData() |
124 | 122 |
125 type | 123 type |
126 Material = object | 124 Material = object |
127 baseColor: Vec3f | 125 baseColor: Vec3f |
181 var pipeline = CreatePipeline[QuadShader](renderPass = swapchain.renderPass) | 179 var pipeline = CreatePipeline[QuadShader](renderPass = swapchain.renderPass) |
182 | 180 |
183 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | 181 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) |
184 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) | 182 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) |
185 | 183 |
186 var c = 0 | 184 var start = getMonoTime() |
187 while UpdateInputs() and c < nFrames: | 185 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
188 WithNextFrame(swapchain, framebuffer, commandbuffer): | 186 WithNextFrame(swapchain, framebuffer, commandbuffer): |
189 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 187 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
190 WithPipeline(commandbuffer, pipeline): | 188 WithPipeline(commandbuffer, pipeline): |
191 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): | 189 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): |
192 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 190 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
193 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF): | 191 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF): |
194 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 192 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
195 inc c | |
196 | 193 |
197 # cleanup | 194 # cleanup |
198 checkVkResult vkDeviceWaitIdle(vulkan.device) | 195 checkVkResult vkDeviceWaitIdle(vulkan.device) |
199 DestroyPipeline(pipeline) | 196 DestroyPipeline(pipeline) |
200 DestroyRenderData(renderdata) | 197 DestroyRenderData(renderdata) |
201 | 198 |
202 proc test_04_multiple_descriptorsets(nFrames: int, swapchain: var Swapchain) = | 199 proc test_04_multiple_descriptorsets(time: float32, swapchain: var Swapchain) = |
203 var renderdata = InitRenderData() | 200 var renderdata = InitRenderData() |
204 | 201 |
205 type | 202 type |
206 RenderSettings = object | 203 RenderSettings = object |
207 brigthness: float32 | 204 brigthness: float32 |
291 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset) | 288 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset) |
292 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset) | 289 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset) |
293 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) | 290 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) |
294 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) | 291 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) |
295 | 292 |
296 var c = 0 | 293 var start = getMonoTime() |
297 while UpdateInputs() and c < nFrames: | 294 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
298 TimeAndLog: | 295 TimeAndLog: |
299 WithNextFrame(swapchain, framebuffer, commandbuffer): | 296 WithNextFrame(swapchain, framebuffer, commandbuffer): |
300 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 297 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
301 WithPipeline(commandbuffer, pipeline): | 298 WithPipeline(commandbuffer, pipeline): |
302 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF): | 299 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF): |
303 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 300 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
304 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF): | 301 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF): |
305 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 302 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
306 mainset.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32) | 303 mainset.data.renderSettings.data.brigthness = ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
307 otherset1.data.objectSettings.data.scale = 0.5 + (c.float32 / nFrames.float32) | 304 otherset1.data.objectSettings.data.scale = 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
308 UpdateGPUBuffer(mainset.data.renderSettings) | 305 UpdateGPUBuffer(mainset.data.renderSettings) |
309 UpdateGPUBuffer(otherset1.data.objectSettings) | 306 UpdateGPUBuffer(otherset1.data.objectSettings) |
310 renderdata.FlushAllMemory() | 307 renderdata.FlushAllMemory() |
311 inc c | |
312 | 308 |
313 # cleanup | 309 # cleanup |
314 checkVkResult vkDeviceWaitIdle(vulkan.device) | 310 checkVkResult vkDeviceWaitIdle(vulkan.device) |
315 DestroyPipeline(pipeline) | 311 DestroyPipeline(pipeline) |
316 DestroyRenderData(renderdata) | 312 DestroyRenderData(renderdata) |
317 | 313 |
318 proc test_05_cube(nFrames: int, swapchain: var Swapchain) = | 314 proc test_05_cube(time: float32, swapchain: var Swapchain) = |
319 type | 315 type |
320 | 316 |
321 UniformData = object | 317 UniformData = object |
322 mvp: Mat4 | 318 mvp: Mat4 |
323 Uniforms = object | 319 Uniforms = object |
410 renderdata.FlushAllMemory() | 406 renderdata.FlushAllMemory() |
411 | 407 |
412 var pipeline = CreatePipeline[CubeShader](renderPass = swapchain.renderPass) | 408 var pipeline = CreatePipeline[CubeShader](renderPass = swapchain.renderPass) |
413 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | 409 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) |
414 | 410 |
415 var c = 0 | |
416 var tStart = getMonoTime() | 411 var tStart = getMonoTime() |
417 var t = tStart | 412 var t = tStart |
418 while UpdateInputs() and c < nFrames: | 413 |
419 | 414 var start = getMonoTime() |
415 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | |
420 let tStartLoop = getMonoTime() - tStart | 416 let tStartLoop = getMonoTime() - tStart |
421 | 417 |
422 uniforms1.data.data.data.mvp = ( | 418 uniforms1.data.data.data.mvp = ( |
423 Perspective(-PI / 2, GetAspectRatio(swapchain), 0.01, 100) * | 419 Perspective(-PI / 2, GetAspectRatio(swapchain), 0.01, 100) * |
424 Translate(0, 0, 2) * | 420 Translate(0, 0, 2) * |
437 let looptime = tEndLoop - tStartLoop | 433 let looptime = tEndLoop - tStartLoop |
438 let waitTime = 16_666 - looptime.inMicroseconds | 434 let waitTime = 16_666 - looptime.inMicroseconds |
439 if waitTime > 0: | 435 if waitTime > 0: |
440 echo "sleep ", waitTime / 1000 | 436 echo "sleep ", waitTime / 1000 |
441 sleep((waitTime / 1000).int) | 437 sleep((waitTime / 1000).int) |
442 inc c | |
443 | 438 |
444 # cleanup | 439 # cleanup |
445 checkVkResult vkDeviceWaitIdle(vulkan.device) | 440 checkVkResult vkDeviceWaitIdle(vulkan.device) |
446 DestroyPipeline(pipeline) | 441 DestroyPipeline(pipeline) |
447 DestroyRenderData(renderdata) | 442 DestroyRenderData(renderdata) |
448 | 443 |
449 proc test_06_triangle_2pass(nFrames: int, depthBuffer: bool, samples: VkSampleCountFlagBits) = | 444 proc test_06_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) = |
450 var | 445 var |
451 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 446 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
452 swapchain = InitSwapchain(renderpass = presentRP).get() | 447 swapchain = InitSwapchain(renderpass = presentRP).get() |
453 | 448 |
454 var renderdata = InitRenderData() | 449 var renderdata = InitRenderData() |
591 swapchain.width, | 586 swapchain.width, |
592 swapchain.height, | 587 swapchain.height, |
593 attachments | 588 attachments |
594 ) | 589 ) |
595 | 590 |
596 var c = 0 | 591 var start = getMonoTime() |
597 while UpdateInputs() and c < nFrames: | 592 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
598 | 593 |
599 TimeAndLog: | 594 TimeAndLog: |
600 WithNextFrame(swapchain, framebuffer, commandbuffer): | 595 WithNextFrame(swapchain, framebuffer, commandbuffer): |
601 | 596 |
602 WithRenderPass(offscreenRP, offscreenFB, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 597 WithRenderPass(offscreenRP, offscreenFB, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
605 | 600 |
606 WithRenderPass(presentRP, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 601 WithRenderPass(presentRP, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
607 WithPipeline(commandbuffer, presentPipeline): | 602 WithPipeline(commandbuffer, presentPipeline): |
608 WithBind(commandbuffer, (uniforms1, ), presentPipeline, swapchain.currentFiF): | 603 WithBind(commandbuffer, (uniforms1, ), presentPipeline, swapchain.currentFiF): |
609 Render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) | 604 Render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) |
610 inc c | |
611 | 605 |
612 # cleanup | 606 # cleanup |
613 checkVkResult vkDeviceWaitIdle(vulkan.device) | 607 checkVkResult vkDeviceWaitIdle(vulkan.device) |
614 DestroyPipeline(presentPipeline) | 608 DestroyPipeline(presentPipeline) |
615 DestroyPipeline(drawPipeline) | 609 DestroyPipeline(drawPipeline) |
626 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil) | 620 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil) |
627 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) | 621 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) |
628 DestroySwapchain(swapchain) | 622 DestroySwapchain(swapchain) |
629 | 623 |
630 when isMainModule: | 624 when isMainModule: |
631 var nFrames = 3000 | 625 var time = 1'f32 |
632 InitVulkan() | 626 InitVulkan() |
633 | 627 |
634 var mainRenderpass: RenderPass | 628 var mainRenderpass: RenderPass |
635 var renderPasses = [ | 629 var renderPasses = [ |
636 # (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), | 630 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), |
637 # (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), | 631 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), |
638 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), | 632 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), |
639 # (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), | 633 (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), |
640 ] | 634 ] |
641 | 635 |
642 # test normal | 636 # test normal |
643 for i, (depthBuffer, samples) in renderPasses: | 637 for i, (depthBuffer, samples) in renderPasses: |
644 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 638 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
645 var swapchain = InitSwapchain(renderpass = renderpass).get() | 639 var swapchain = InitSwapchain(renderpass = renderpass).get() |
646 | 640 |
647 #[ | |
648 # tests a simple triangle with minimalistic shader and vertex format | 641 # tests a simple triangle with minimalistic shader and vertex format |
649 test_01_triangle(nFrames, swapchain) | 642 test_01_triangle(time, swapchain) |
650 | 643 |
651 # tests instanced triangles and quads, mixing meshes and instances | 644 # tests instanced triangles and quads, mixing meshes and instances |
652 test_02_triangle_quad_instanced(nFrames, swapchain) | 645 test_02_triangle_quad_instanced(time, swapchain) |
653 | 646 |
654 # teste descriptor sets | 647 # teste descriptor sets |
655 test_03_simple_descriptorset(nFrames, swapchain) | 648 test_03_simple_descriptorset(time, swapchain) |
656 | 649 |
657 # tests multiple descriptor sets and arrays | 650 # tests multiple descriptor sets and arrays |
658 test_04_multiple_descriptorsets(nFrames, swapchain) | 651 test_04_multiple_descriptorsets(time, swapchain) |
659 ]# | |
660 | 652 |
661 # rotating cube | 653 # rotating cube |
662 test_05_cube(999999999, swapchain) | 654 test_05_cube(time, swapchain) |
663 | 655 |
664 checkVkResult vkDeviceWaitIdle(vulkan.device) | 656 checkVkResult vkDeviceWaitIdle(vulkan.device) |
665 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | 657 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
666 DestroySwapchain(swapchain) | 658 DestroySwapchain(swapchain) |
667 | 659 |
668 # test multiple render passes | 660 # test multiple render passes |
669 # for i, (depthBuffer, samples) in renderPasses: | 661 for i, (depthBuffer, samples) in renderPasses: |
670 # test_06_triangle_2pass(nFrames, depthBuffer, samples) | 662 test_06_triangle_2pass(time, depthBuffer, samples) |
671 | 663 |
672 DestroyVulkan() | 664 DestroyVulkan() |