Mercurial > games > semicongine
annotate tests/test_vulkan_wrapper.nim @ 570:1cf928eb0a10
did: refactor rendering/scene concept
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 29 Mar 2023 23:35:39 +0700 |
parents | f4d8f0018670 |
children | 6fd10b7e2d6a |
rev | line source |
---|---|
557 | 1 import std/options |
2 | |
554 | 3 import semicongine/vulkan |
555 | 4 import semicongine/platform/window |
559 | 5 import semicongine/math |
570 | 6 import semicongine/entity |
7 import semicongine/scene | |
554 | 8 |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
9 type |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
10 Vertex = object |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
11 pos: Vec3 |
564
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
12 FragmentInput = object |
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
13 fragpos: Vec3 |
563 | 14 Uniforms = object |
15 time: float32 | |
564
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
16 Pixel = object |
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
17 color: Vec4 |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
18 |
565 | 19 proc diagnostics(instance: Instance) = |
555 | 20 # diagnostic output |
554 | 21 echo "Devices" |
22 for device in instance.getPhysicalDevices(): | |
23 echo " " & $device | |
557 | 24 echo " Rating: " & $device.rateGraphics() |
554 | 25 echo " Extensions" |
26 for extension in device.getExtensions(): | |
27 echo " " & $extension | |
557 | 28 echo " Properties" |
29 echo " " & $device.getProperties() | |
30 echo " Features" | |
31 echo " " & $device.getFeatures() | |
554 | 32 echo " Queue families" |
33 for queueFamily in device.getQueueFamilies(): | |
34 echo " " & $queueFamily | |
555 | 35 echo " Surface present modes" |
557 | 36 for mode in device.getSurfacePresentModes(): |
555 | 37 echo " " & $mode |
38 echo " Surface formats" | |
557 | 39 for format in device.getSurfaceFormats(): |
555 | 40 echo " " & $format |
554 | 41 |
565 | 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 | |
554 | 60 # create devices |
557 | 61 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
62 var device = instance.createDevice( | |
63 selectedPhysicalDevice, | |
64 @[], | |
65 @[], | |
66 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
67 ) | |
68 | |
565 | 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);") | |
567 | 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) | |
569 | 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") | |
557 | 79 |
570 | 80 var thescene = Scene(root: newEntity("scene")) |
81 | |
557 | 82 echo "All successfull" |
569 | 83 for i in 0 ..< 2: |
570 | 84 discard swapchain.drawNextFrame(thescene) |
569 | 85 echo "Rendered ", swapchain.framesRendered, " frames" |
557 | 86 echo "Start cleanup" |
554 | 87 |
569 | 88 |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
89 # cleanup |
566
cc7ba46fe3c4
add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents:
565
diff
changeset
|
90 checkVkResult device.vk.vkDeviceWaitIdle() |
564
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
91 vertexshader.destroy() |
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
92 fragmentshader.destroy() |
559 | 93 renderpass.destroy() |
557 | 94 swapchain.destroy() |
95 device.destroy() | |
554 | 96 |
97 debugger.destroy() | |
98 instance.destroy() |