comparison tests/test_rendering.nim @ 1334:d5a6f69dc855

add: support for multi-layer images
author sam <sam@basx.dev>
date Thu, 22 Aug 2024 22:18:33 +0700
parents df3c075e5dea
children
comparison
equal deleted inserted replaced
1333:966032c7a3aa 1334:d5a6f69dc855
664 destroyPipeline(pipeline2) 664 destroyPipeline(pipeline2)
665 destroyPipeline(pipeline3) 665 destroyPipeline(pipeline3)
666 destroyPipeline(pipeline4) 666 destroyPipeline(pipeline4)
667 destroyRenderData(renderdata) 667 destroyRenderData(renderdata)
668 668
669 proc test_08_texture_array(time: float32) =
670 var renderdata = initRenderData()
671
672 type
673 Uniforms = object
674 textures: ImageArray[BGRA]
675
676 Shader = object
677 position {.VertexAttribute.}: Vec3f
678 uv {.VertexAttribute.}: Vec2f
679 fragmentUv {.Pass.}: Vec2f
680 outColor {.ShaderOutput.}: Vec4f
681 descriptorSets {.DescriptorSet: 0.}: Uniforms
682 # code
683 vertexCode: string =
684 """
685 void main() {
686 fragmentUv = uv;
687 gl_Position = vec4(position, 1);
688 }"""
689 fragmentCode: string =
690 """
691 void main() {
692 vec4 col1 = texture(textures, vec3(fragmentUv, 0));
693 vec4 col2 = texture(textures, vec3(fragmentUv, 1));
694 float w = length(fragmentUv * 2 - 1) / 1.41421;
695 outColor = (1 - w) * col1 + w * col2;
696 }"""
697
698 Quad = object
699 position: GPUArray[Vec3f, VertexBuffer]
700 uv: GPUArray[Vec2f, VertexBuffer]
701
702 var mesh = Quad(
703 position: asGPUArray(
704 [
705 vec3(-0.8, -0.5),
706 vec3(-0.8, 0.5),
707 vec3(0.8, 0.5),
708 vec3(0.8, 0.5),
709 vec3(0.8, -0.5),
710 vec3(-0.8, -0.5),
711 ],
712 VertexBuffer,
713 ),
714 uv: asGPUArray(
715 [vec2(0, 1), vec2(0, 0), vec2(1, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1)],
716 VertexBuffer,
717 ),
718 )
719 assignBuffers(renderdata, mesh)
720 renderdata.flushAllMemory()
721
722 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass)
723 var uniforms1 = asDescriptorSetData(
724 Uniforms(textures: loadImageArray[BGRA](["art.png", "art1.png"]))
725 )
726 uploadImages(renderdata, uniforms1)
727 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
728
729 var start = getMonoTime()
730 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
731 withNextFrame(framebuffer, commandbuffer):
732 withRenderPass(
733 vulkan.swapchain.renderPass,
734 framebuffer,
735 commandbuffer,
736 vulkan.swapchain.width,
737 vulkan.swapchain.height,
738 vec4(0, 0, 0, 0),
739 ):
740 withPipeline(commandbuffer, pipeline):
741 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline)
742 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
743
744 # cleanup
745 checkVkResult vkDeviceWaitIdle(vulkan.device)
746 destroyPipeline(pipeline)
747 destroyRenderData(renderdata)
748
669 proc test_07_png_texture(time: float32) = 749 proc test_07_png_texture(time: float32) =
670 var renderdata = initRenderData() 750 var renderdata = initRenderData()
671 751
672 type 752 type
673 Uniforms = object 753 Uniforms = object
739 # cleanup 819 # cleanup
740 checkVkResult vkDeviceWaitIdle(vulkan.device) 820 checkVkResult vkDeviceWaitIdle(vulkan.device)
741 destroyPipeline(pipeline) 821 destroyPipeline(pipeline)
742 destroyRenderData(renderdata) 822 destroyRenderData(renderdata)
743 823
744 proc test_08_triangle_2pass( 824 proc test_09_triangle_2pass(
745 time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits 825 time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits
746 ) = 826 ) =
747 var (offscreenRP, presentRP) = 827 var (offscreenRP, presentRP) =
748 createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) 828 createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
749 829
950 var renderpass = 1030 var renderpass =
951 createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) 1031 createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
952 setupSwapchain(renderpass = renderpass) 1032 setupSwapchain(renderpass = renderpass)
953 1033
954 # tests a simple triangle with minimalistic shader and vertex format 1034 # tests a simple triangle with minimalistic shader and vertex format
955 test_01_triangle(time) 1035 # test_01_triangle(time)
956 1036
957 # tests instanced triangles and quads, mixing meshes and instances 1037 # tests instanced triangles and quads, mixing meshes and instances
958 test_02_triangle_quad_instanced(time) 1038 # test_02_triangle_quad_instanced(time)
959 1039
960 # teste descriptor sets 1040 # teste descriptor sets
961 test_03_simple_descriptorset(time) 1041 # test_03_simple_descriptorset(time)
962 1042
963 # tests multiple descriptor sets and arrays 1043 # tests multiple descriptor sets and arrays
964 test_04_multiple_descriptorsets(time) 1044 # test_04_multiple_descriptorsets(time)
965 1045
966 # rotating cube 1046 # rotating cube
967 test_05_cube(time) 1047 # test_05_cube(time)
968 1048
969 # different draw modes (lines, points, and topologies) 1049 # different draw modes (lines, points, and topologies)
970 test_06_different_draw_modes(time) 1050 # test_06_different_draw_modes(time)
971 1051
972 # load PNG texture 1052 # test_07_png_texture(time)
973 test_07_png_texture(time) 1053
1054 test_08_texture_array(time)
974 1055
975 checkVkResult vkDeviceWaitIdle(vulkan.device) 1056 checkVkResult vkDeviceWaitIdle(vulkan.device)
976 destroyRenderPass(renderpass) 1057 destroyRenderPass(renderpass)
977 clearSwapchain() 1058 clearSwapchain()
978 1059
979 # test multiple render passes 1060 # test multiple render passes
980 for i, (depthBuffer, samples) in renderPasses: 1061 # for i, (depthBuffer, samples) in renderPasses:
981 test_08_triangle_2pass(time, depthBuffer, samples) 1062 # test_09_triangle_2pass(time, depthBuffer, samples)
982 1063
983 destroyVulkan() 1064 destroyVulkan()