comparison tests/test_vulkan_wrapper.nim @ 575:eaedc0369c38

yay: first triangle rendering with new engine implmentation
author Sam <sam@basx.dev>
date Mon, 03 Apr 2023 00:06:24 +0700
parents bbeec60e25ca
children e18538442837
comparison
equal deleted inserted replaced
574:bbeec60e25ca 575:eaedc0369c38
43 # create instance 43 # create instance
44 var thewindow = createWindow("Test") 44 var thewindow = createWindow("Test")
45 var instance = thewindow.createInstance( 45 var instance = thewindow.createInstance(
46 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), 46 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
47 instanceExtensions= @["VK_EXT_debug_utils"], 47 instanceExtensions= @["VK_EXT_debug_utils"],
48 layers= @["VK_LAYER_KHRONOS_validation"] 48 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"]
49 ) 49 )
50 var debugger = instance.createDebugMessenger() 50 var debugger = instance.createDebugMessenger()
51 51
52 # create devices 52 # create devices
53 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() 53 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
56 @[], 56 @[],
57 @[], 57 @[],
58 selectedPhysicalDevice.filterForGraphicsPresentationQueues() 58 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
59 ) 59 )
60 60
61 const inputs = AttributeGroup(attributes: @[attr(name="position", thetype=Float32, components=3)]) 61 const
62 const uniforms = AttributeGroup() 62 vertexInput = initAttributeGroup(
63 const outputs = AttributeGroup(attributes: @[attr(name="fragpos", thetype=Float32, components=3)]) 63 asAttribute(default(Vec3f), "position"),
64 const fragOutput = AttributeGroup(attributes: @[attr(name="color", thetype=Float32, components=4)]) 64 asAttribute(default(Vec3f), "color"),
65 const vertexBinary = shaderCode(inputs=inputs, uniforms=uniforms, outputs=outputs, stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = position;") 65 )
66 const fragmentBinary = shaderCode(inputs=outputs, uniforms=uniforms, outputs=fragOutput, stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 1);") 66 vertexOutput = initAttributeGroup(asAttribute(default(Vec3f), "outcolor"))
67 fragOutput = initAttributeGroup(asAttribute(default(Vec4f), "color"))
68 vertexCode = compileGlslShader(
69 stage=VK_SHADER_STAGE_VERTEX_BIT,
70 inputs=vertexInput,
71 outputs=vertexOutput,
72 body="""gl_Position = vec4(position, 1.0); outcolor = color;"""
73 )
74 fragmentCode = compileGlslShader(
75 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
76 inputs=vertexOutput,
77 outputs=fragOutput,
78 body="color = vec4(outcolor, 1);"
79 )
67 var 80 var
68 vertexshader = device.createShader(inputs, uniforms, outputs, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary) 81 vertexshader = device.createShaderModule(vertexCode)
69 fragmentshader = device.createShader(inputs, uniforms, outputs, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary) 82 fragmentshader = device.createShaderModule(fragmentCode)
70 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat() 83 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
71 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2) 84 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
72 var (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2) 85 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
73 if res != VK_SUCCESS: 86 if res != VK_SUCCESS:
74 raise newException(Exception, "Unable to create swapchain") 87 raise newException(Exception, "Unable to create swapchain")
75 88
76 var thescene = Scene( 89 var thescene = Scene(
77 name: "main", 90 name: "main",
78 root: newEntity("root", 91 root: newEntity("root",
79 newEntity("triangle1", newMesh([newVec3f(-0.5, -0.5), newVec3f(0.5, 0.5), newVec3f(0.5, -0.5)])), 92 newEntity("triangle1", initMesh(
80 newEntity("triangle2", newMesh([newVec3f(-0.5, -0.5), newVec3f(0.5, -0.5), newVec3f(0.5, 0.5)])), 93 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
94 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
95 )),
81 ) 96 )
82 ) 97 )
83 thescene.setupDrawables(renderPass) 98 thescene.setupDrawables(renderPass)
84 99
85 echo "Setup successfull, start rendering" 100 echo "Setup successfull, start rendering"
86 for i in 0 ..< 1: 101 for i in 0 ..< 1000:
87 discard swapchain.drawScene(thescene) 102 discard swapchain.drawScene(thescene)
88 echo "Rendered ", swapchain.framesRendered, " frames" 103 echo "Rendered ", swapchain.framesRendered, " frames"
104 checkVkResult device.vk.vkDeviceWaitIdle()
105
106 # cleanup
89 echo "Start cleanup" 107 echo "Start cleanup"
90 108
109 # logical
110 thescene.destroy()
91 111
92 # cleanup 112 # rendering objects
93 checkVkResult device.vk.vkDeviceWaitIdle()
94 thescene.destroy()
95 vertexshader.destroy() 113 vertexshader.destroy()
96 fragmentshader.destroy() 114 fragmentshader.destroy()
97 renderPass.destroy() 115 renderPass.destroy()
98 swapchain.destroy() 116 swapchain.destroy()
117
118 # global objects
99 device.destroy() 119 device.destroy()
100
101 debugger.destroy() 120 debugger.destroy()
102 instance.destroy() 121 instance.destroy()