comparison tests/test_vulkan_wrapper.nim @ 115:e18538442837

add: comments for further refactoring
author Sam <sam@basx.dev>
date Mon, 03 Apr 2023 00:10:08 +0700
parents 056e08dfad10
children 40ff2453855e
comparison
equal deleted inserted replaced
114:056e08dfad10 115:e18538442837
8 import semicongine/gpu_data 8 import semicongine/gpu_data
9 import semicongine/mesh 9 import semicongine/mesh
10 10
11 proc diagnostics(instance: Instance) = 11 proc diagnostics(instance: Instance) =
12 # diagnostic output 12 # diagnostic output
13 # print basic driver infos
14 echo "Layers"
15 for layer in getLayers():
16 echo " " & layer
17 echo "Instance extensions"
18 for extension in getInstanceExtensions():
19 echo " " & extension
20
13 echo "Devices" 21 echo "Devices"
14 for device in instance.getPhysicalDevices(): 22 for device in instance.getPhysicalDevices():
15 echo " " & $device 23 echo " " & $device
16 echo " Rating: " & $device.rateGraphics() 24 echo " Rating: " & $device.rateGraphics()
17 echo " Extensions" 25 echo " Extensions"
30 echo " Surface formats" 38 echo " Surface formats"
31 for format in device.getSurfaceFormats(): 39 for format in device.getSurfaceFormats():
32 echo " " & $format 40 echo " " & $format
33 41
34 when isMainModule: 42 when isMainModule:
35 # print basic driver infos
36 echo "Layers"
37 for layer in getLayers():
38 echo " " & layer
39 echo "Instance extensions"
40 for extension in getInstanceExtensions():
41 echo " " & extension
42 43
44 # INIT ENGINE:
43 # create instance 45 # create instance
44 var thewindow = createWindow("Test") 46 var thewindow = createWindow("Test")
45 var instance = thewindow.createInstance( 47 var instance = thewindow.createInstance(
46 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), 48 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
47 instanceExtensions= @["VK_EXT_debug_utils"], 49 instanceExtensions= @["VK_EXT_debug_utils"],
48 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"] 50 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"]
49 ) 51 )
50 var debugger = instance.createDebugMessenger() 52 var debugger = instance.createDebugMessenger()
51
52 # create devices 53 # create devices
53 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() 54 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
54 var device = instance.createDevice( 55 var device = instance.createDevice(
55 selectedPhysicalDevice, 56 selectedPhysicalDevice,
56 @[], 57 @[],
57 @[], 58 @[],
58 selectedPhysicalDevice.filterForGraphicsPresentationQueues() 59 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
59 ) 60 )
60 61
62 # INIT RENDERER:
61 const 63 const
62 vertexInput = initAttributeGroup( 64 vertexInput = initAttributeGroup(
63 asAttribute(default(Vec3f), "position"), 65 asAttribute(default(Vec3f), "position"),
64 asAttribute(default(Vec3f), "color"), 66 asAttribute(default(Vec3f), "color"),
65 ) 67 )
84 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2) 86 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
85 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2) 87 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
86 if res != VK_SUCCESS: 88 if res != VK_SUCCESS:
87 raise newException(Exception, "Unable to create swapchain") 89 raise newException(Exception, "Unable to create swapchain")
88 90
91 # INIT SCENE
89 var thescene = Scene( 92 var thescene = Scene(
90 name: "main", 93 name: "main",
91 root: newEntity("root", 94 root: newEntity("root",
92 newEntity("triangle1", initMesh( 95 newEntity("triangle1", initMesh(
93 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 96 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
95 )), 98 )),
96 ) 99 )
97 ) 100 )
98 thescene.setupDrawables(renderPass) 101 thescene.setupDrawables(renderPass)
99 102
103 # MAINLOOP
100 echo "Setup successfull, start rendering" 104 echo "Setup successfull, start rendering"
101 for i in 0 ..< 1000: 105 for i in 0 ..< 1000:
102 discard swapchain.drawScene(thescene) 106 discard swapchain.drawScene(thescene)
103 echo "Rendered ", swapchain.framesRendered, " frames" 107 echo "Rendered ", swapchain.framesRendered, " frames"
104 checkVkResult device.vk.vkDeviceWaitIdle() 108 checkVkResult device.vk.vkDeviceWaitIdle()
105 109
106 # cleanup 110 # cleanup
107 echo "Start cleanup" 111 echo "Start cleanup"
108 112
109 # logical 113 # destroy scene
110 thescene.destroy() 114 thescene.destroy()
111 115
112 # rendering objects 116 # destroy renderer
113 vertexshader.destroy() 117 vertexshader.destroy()
114 fragmentshader.destroy() 118 fragmentshader.destroy()
115 renderPass.destroy() 119 renderPass.destroy()
116 swapchain.destroy() 120 swapchain.destroy()
117 121
118 # global objects 122 # destroy engine
119 device.destroy() 123 device.destroy()
120 debugger.destroy() 124 debugger.destroy()
121 instance.destroy() 125 instance.destroy()