Mercurial > games > semicongine
comparison tests/test_rendering.nim @ 1240:42eeb59f3a43
add: more tests, line and point rendering
author | sam <sam@basx.dev> |
---|---|
date | Mon, 22 Jul 2024 12:42:35 +0700 |
parents | 69489a678141 |
children | e8b3dc80e48e |
comparison
equal
deleted
inserted
replaced
1239:69489a678141 | 1240:42eeb59f3a43 |
---|---|
431 | 431 |
432 let tEndLoop = getMonoTime() - tStart | 432 let tEndLoop = getMonoTime() - tStart |
433 let looptime = tEndLoop - tStartLoop | 433 let looptime = tEndLoop - tStartLoop |
434 let waitTime = 16_666 - looptime.inMicroseconds | 434 let waitTime = 16_666 - looptime.inMicroseconds |
435 if waitTime > 0: | 435 if waitTime > 0: |
436 echo "sleep ", waitTime / 1000 | |
437 sleep((waitTime / 1000).int) | 436 sleep((waitTime / 1000).int) |
438 | 437 |
439 # cleanup | 438 # cleanup |
440 checkVkResult vkDeviceWaitIdle(vulkan.device) | 439 checkVkResult vkDeviceWaitIdle(vulkan.device) |
441 DestroyPipeline(pipeline) | 440 DestroyPipeline(pipeline) |
442 DestroyRenderData(renderdata) | 441 DestroyRenderData(renderdata) |
443 | 442 |
444 proc test_06_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) = | 443 proc test_06_different_draw_modes(time: float32) = |
444 var renderdata = InitRenderData() | |
445 | |
446 type | |
447 Shader = object | |
448 position {.VertexAttribute.}: Vec3f | |
449 color {.VertexAttribute.}: Vec3f | |
450 fragmentColor {.Pass.}: Vec3f | |
451 outColor {.ShaderOutput.}: Vec4f | |
452 # code | |
453 vertexCode: string = """void main() { | |
454 gl_PointSize = 100; | |
455 fragmentColor = color; | |
456 gl_Position = vec4(position, 1);}""" | |
457 fragmentCode: string = """void main() { | |
458 outColor = vec4(fragmentColor, 1);}""" | |
459 TriangleMesh = object | |
460 position: GPUArray[Vec3f, VertexBuffer] | |
461 color: GPUArray[Vec3f, VertexBuffer] | |
462 var triangle = TriangleMesh( | |
463 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), | |
464 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), | |
465 ) | |
466 var lines = TriangleMesh( | |
467 position: asGPUArray([NewVec3f(-0.9, 0), NewVec3f(-0.05, -0.9), NewVec3f(0.05, -0.9), NewVec3f(0.9, 0)], VertexBuffer), | |
468 color: asGPUArray([NewVec3f(1, 1, 0), NewVec3f(1, 1, 0), NewVec3f(0, 1, 0), NewVec3f(0, 1, 0)], VertexBuffer), | |
469 ) | |
470 AssignBuffers(renderdata, triangle) | |
471 AssignBuffers(renderdata, lines) | |
472 renderdata.FlushAllMemory() | |
473 | |
474 var pipeline1 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_LINE, lineWidth = 20'f32) | |
475 var pipeline2 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT) | |
476 var pipeline3 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, lineWidth = 5) | |
477 var pipeline4 = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST) | |
478 | |
479 var start = getMonoTime() | |
480 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | |
481 WithNextFrame(framebuffer, commandbuffer): | |
482 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): | |
483 WithPipeline(commandbuffer, pipeline1): | |
484 Render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle) | |
485 WithPipeline(commandbuffer, pipeline2): | |
486 Render(commandbuffer = commandbuffer, pipeline = pipeline2, mesh = triangle) | |
487 WithPipeline(commandbuffer, pipeline3): | |
488 Render(commandbuffer = commandbuffer, pipeline = pipeline3, mesh = lines) | |
489 WithPipeline(commandbuffer, pipeline4): | |
490 Render(commandbuffer = commandbuffer, pipeline = pipeline4, mesh = lines) | |
491 | |
492 # cleanup | |
493 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
494 DestroyPipeline(pipeline1) | |
495 DestroyPipeline(pipeline2) | |
496 DestroyPipeline(pipeline3) | |
497 DestroyPipeline(pipeline4) | |
498 DestroyRenderData(renderdata) | |
499 | |
500 proc test_07_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) = | |
445 var (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 501 var (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
446 | 502 |
447 SetupSwapchain(renderpass = presentRP) | 503 SetupSwapchain(renderpass = presentRP) |
448 | 504 |
449 var renderdata = InitRenderData() | 505 var renderdata = InitRenderData() |
578 else: | 634 else: |
579 if offscreenRP.depthBuffer: | 635 if offscreenRP.depthBuffer: |
580 attachments = @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview] | 636 attachments = @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview] |
581 else: | 637 else: |
582 attachments = @[msaaImageView, uniforms1.data.frameTexture.imageview] | 638 attachments = @[msaaImageView, uniforms1.data.frameTexture.imageview] |
583 echo attachments | |
584 var offscreenFB = svkCreateFramebuffer( | 639 var offscreenFB = svkCreateFramebuffer( |
585 offscreenRP.vk, | 640 offscreenRP.vk, |
586 vulkan.swapchain.width, | 641 vulkan.swapchain.width, |
587 vulkan.swapchain.height, | 642 vulkan.swapchain.height, |
588 attachments | 643 attachments |
631 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), | 686 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), |
632 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), | 687 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), |
633 (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), | 688 (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), |
634 ] | 689 ] |
635 | 690 |
691 | |
636 # test normal | 692 # test normal |
637 for i, (depthBuffer, samples) in renderPasses: | 693 for i, (depthBuffer, samples) in renderPasses: |
638 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 694 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
639 SetupSwapchain(renderpass = renderpass) | 695 SetupSwapchain(renderpass = renderpass) |
640 | 696 |
651 test_04_multiple_descriptorsets(time) | 707 test_04_multiple_descriptorsets(time) |
652 | 708 |
653 # rotating cube | 709 # rotating cube |
654 test_05_cube(time) | 710 test_05_cube(time) |
655 | 711 |
712 # different draw modes (lines, points, and topologies) | |
713 test_06_different_draw_modes(time) | |
714 | |
656 checkVkResult vkDeviceWaitIdle(vulkan.device) | 715 checkVkResult vkDeviceWaitIdle(vulkan.device) |
657 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | 716 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
658 ClearSwapchain() | 717 ClearSwapchain() |
659 | 718 |
660 # test multiple render passes | 719 # test multiple render passes |
661 for i, (depthBuffer, samples) in renderPasses: | 720 for i, (depthBuffer, samples) in renderPasses: |
662 test_06_triangle_2pass(time, depthBuffer, samples) | 721 test_07_triangle_2pass(time, depthBuffer, samples) |
663 | 722 |
664 DestroyVulkan() | 723 DestroyVulkan() |