comparison tests/test_vulkan_wrapper.nim @ 111:6fd10b7e2d6a

did: allow runtime shader-input definitions
author Sam <sam@basx.dev>
date Fri, 31 Mar 2023 16:00:16 +0700
parents 8d24727c9795
children 0c5a74885796
comparison
equal deleted inserted replaced
110:3bbc94a83404 111:6fd10b7e2d6a
1 import std/os
1 import std/options 2 import std/options
2 3
3 import semicongine/vulkan 4 import semicongine/vulkan
4 import semicongine/platform/window 5 import semicongine/platform/window
5 import semicongine/math 6 import semicongine/math
6 import semicongine/entity 7 import semicongine/entity
7 import semicongine/scene 8 import semicongine/scene
9 import semicongine/gpu_data
8 10
9 type 11 type
10 Vertex = object 12 Vertex = object
11 pos: Vec3 13 pos: Vec3
12 FragmentInput = object 14 FragmentInput = object
64 @[], 66 @[],
65 @[], 67 @[],
66 selectedPhysicalDevice.filterForGraphicsPresentationQueues() 68 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
67 ) 69 )
68 70
69 const vertexBinary = shaderCode[Vertex, Uniforms, FragmentInput](stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;") 71 const inputs = AttributeGroup(attributes: @[attr(name="pos", thetype=Float32, components=3)])
70 const fragmentBinary = shaderCode[FragmentInput, void, Pixel](stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);") 72 const uniforms = AttributeGroup()
73 const outputs = AttributeGroup(attributes: @[attr(name="fragpos", thetype=Float32, components=3)])
74 const fragOutput = AttributeGroup(attributes: @[attr(name="color", thetype=Float32, components=4)])
75 const vertexBinary = shaderCode(inputs=inputs, uniforms=uniforms, outputs=outputs, stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;")
76 const fragmentBinary = shaderCode(inputs=outputs, uniforms=uniforms, outputs=fragOutput, stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);")
71 var 77 var
72 vertexshader = createShader[Vertex, Uniforms, FragmentInput](device, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary) 78 vertexshader = device.createShader(inputs, uniforms, outputs, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary)
73 fragmentshader = createShader[FragmentInput, void, Pixel](device, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary) 79 fragmentshader = device.createShader(inputs, uniforms, outputs, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary)
74 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat() 80 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
75 renderpass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2) 81 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
76 var (swapchain, res) = device.createSwapchain(renderpass, surfaceFormat, device.firstGraphicsQueue().get().family, 2) 82 var (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
77 if res != VK_SUCCESS: 83 if res != VK_SUCCESS:
78 raise newException(Exception, "Unable to create swapchain") 84 raise newException(Exception, "Unable to create swapchain")
79 85
80 var thescene = Scene(root: newEntity("scene")) 86 var thescene = Scene(root: newEntity("scene"))
87 thescene.setupDrawables(renderPass)
81 88
82 echo "All successfull" 89 echo "Setup successfull, start rendering"
83 for i in 0 ..< 2: 90 for i in 0 ..< 10:
84 discard swapchain.drawNextFrame(thescene) 91 discard swapchain.drawScene(thescene)
85 echo "Rendered ", swapchain.framesRendered, " frames" 92 echo "Rendered ", swapchain.framesRendered, " frames"
86 echo "Start cleanup" 93 echo "Start cleanup"
87 94
88 95
89 # cleanup 96 # cleanup
90 checkVkResult device.vk.vkDeviceWaitIdle() 97 checkVkResult device.vk.vkDeviceWaitIdle()
91 vertexshader.destroy() 98 vertexshader.destroy()
92 fragmentshader.destroy() 99 fragmentshader.destroy()
93 renderpass.destroy() 100 renderPass.destroy()
94 swapchain.destroy() 101 swapchain.destroy()
95 device.destroy() 102 device.destroy()
96 103
97 debugger.destroy() 104 debugger.destroy()
98 instance.destroy() 105 instance.destroy()