Mercurial > games > semicongine
annotate tests/test_vulkan_wrapper.nim @ 109:8d24727c9795
did: refactor rendering/scene concept
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 29 Mar 2023 23:35:39 +0700 |
parents | 70f92081d2c1 |
children | 6fd10b7e2d6a |
rev | line source |
---|---|
96 | 1 import std/options |
2 | |
93 | 3 import semicongine/vulkan |
94 | 4 import semicongine/platform/window |
98 | 5 import semicongine/math |
109 | 6 import semicongine/entity |
7 import semicongine/scene | |
93 | 8 |
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
9 type |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
10 Vertex = object |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
11 pos: Vec3 |
103
1e2027dfc642
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
102
diff
changeset
|
12 FragmentInput = object |
1e2027dfc642
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
102
diff
changeset
|
13 fragpos: Vec3 |
102 | 14 Uniforms = object |
15 time: float32 | |
103
1e2027dfc642
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
102
diff
changeset
|
16 Pixel = object |
1e2027dfc642
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
102
diff
changeset
|
17 color: Vec4 |
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
18 |
104 | 19 proc diagnostics(instance: Instance) = |
94 | 20 # diagnostic output |
93 | 21 echo "Devices" |
22 for device in instance.getPhysicalDevices(): | |
23 echo " " & $device | |
96 | 24 echo " Rating: " & $device.rateGraphics() |
93 | 25 echo " Extensions" |
26 for extension in device.getExtensions(): | |
27 echo " " & $extension | |
96 | 28 echo " Properties" |
29 echo " " & $device.getProperties() | |
30 echo " Features" | |
31 echo " " & $device.getFeatures() | |
93 | 32 echo " Queue families" |
33 for queueFamily in device.getQueueFamilies(): | |
34 echo " " & $queueFamily | |
94 | 35 echo " Surface present modes" |
96 | 36 for mode in device.getSurfacePresentModes(): |
94 | 37 echo " " & $mode |
38 echo " Surface formats" | |
96 | 39 for format in device.getSurfaceFormats(): |
94 | 40 echo " " & $format |
93 | 41 |
104 | 42 when isMainModule: |
43 # print basic driver infos | |
44 echo "Layers" | |
45 for layer in getLayers(): | |
46 echo " " & layer | |
47 echo "Instance extensions" | |
48 for extension in getInstanceExtensions(): | |
49 echo " " & extension | |
50 | |
51 # create instance | |
52 var thewindow = createWindow("Test") | |
53 var instance = thewindow.createInstance( | |
54 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), | |
55 instanceExtensions= @["VK_EXT_debug_utils"], | |
56 layers= @["VK_LAYER_KHRONOS_validation"] | |
57 ) | |
58 var debugger = instance.createDebugMessenger() | |
59 | |
93 | 60 # create devices |
96 | 61 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
62 var device = instance.createDevice( | |
63 selectedPhysicalDevice, | |
64 @[], | |
65 @[], | |
66 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
67 ) | |
68 | |
104 | 69 const vertexBinary = shaderCode[Vertex, Uniforms, FragmentInput](stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;") |
70 const fragmentBinary = shaderCode[FragmentInput, void, Pixel](stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);") | |
106 | 71 var |
72 vertexshader = createShader[Vertex, Uniforms, FragmentInput](device, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary) | |
73 fragmentshader = createShader[FragmentInput, void, Pixel](device, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary) | |
108 | 74 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat() |
75 renderpass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2) | |
76 var (swapchain, res) = device.createSwapchain(renderpass, surfaceFormat, device.firstGraphicsQueue().get().family, 2) | |
77 if res != VK_SUCCESS: | |
78 raise newException(Exception, "Unable to create swapchain") | |
96 | 79 |
109 | 80 var thescene = Scene(root: newEntity("scene")) |
81 | |
96 | 82 echo "All successfull" |
108 | 83 for i in 0 ..< 2: |
109 | 84 discard swapchain.drawNextFrame(thescene) |
108 | 85 echo "Rendered ", swapchain.framesRendered, " frames" |
96 | 86 echo "Start cleanup" |
93 | 87 |
108 | 88 |
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
89 # cleanup |
105
4059aa0d689b
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
104
diff
changeset
|
90 checkVkResult device.vk.vkDeviceWaitIdle() |
103
1e2027dfc642
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
102
diff
changeset
|
91 vertexshader.destroy() |
1e2027dfc642
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
102
diff
changeset
|
92 fragmentshader.destroy() |
98 | 93 renderpass.destroy() |
96 | 94 swapchain.destroy() |
95 device.destroy() | |
93 | 96 |
97 debugger.destroy() | |
98 instance.destroy() |