Mercurial > games > semicongine
annotate tests/test_vulkan_wrapper.nim @ 99:4deffc94484a
add: vertex and (initial) shader types and methods
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 14 Mar 2023 13:21:40 +0700 |
parents | 335a65d3f892 |
children | c782d7e52b25 |
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 |
93 | 6 |
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
7 type |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
8 Vertex = object |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
9 pos: Vec3 |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
10 |
93 | 11 |
12 when isMainModule: | |
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 | |
21 # create instance | |
96 | 22 var thewindow = createWindow("Test") |
23 var instance = thewindow.createInstance( | |
93 | 24 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), |
94 | 25 instanceExtensions= @["VK_EXT_debug_utils"], |
26 layers= @["VK_LAYER_KHRONOS_validation"] | |
93 | 27 ) |
28 var debugger = instance.createDebugMessenger() | |
94 | 29 |
30 # diagnostic output | |
93 | 31 echo "Devices" |
32 for device in instance.getPhysicalDevices(): | |
33 echo " " & $device | |
96 | 34 echo " Rating: " & $device.rateGraphics() |
93 | 35 echo " Extensions" |
36 for extension in device.getExtensions(): | |
37 echo " " & $extension | |
96 | 38 echo " Properties" |
39 echo " " & $device.getProperties() | |
40 echo " Features" | |
41 echo " " & $device.getFeatures() | |
93 | 42 echo " Queue families" |
43 for queueFamily in device.getQueueFamilies(): | |
44 echo " " & $queueFamily | |
94 | 45 echo " Surface present modes" |
96 | 46 for mode in device.getSurfacePresentModes(): |
94 | 47 echo " " & $mode |
48 echo " Surface formats" | |
96 | 49 for format in device.getSurfaceFormats(): |
94 | 50 echo " " & $format |
93 | 51 |
52 # create devices | |
96 | 53 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
54 var device = instance.createDevice( | |
55 selectedPhysicalDevice, | |
56 @[], | |
57 @[], | |
58 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
59 ) | |
60 | |
98 | 61 # setup render pipeline |
96 | 62 var (swapchain, res) = device.createSwapchain(device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()) |
63 if res != VK_SUCCESS: | |
64 raise newException(Exception, "Unable to create swapchain") | |
98 | 65 var renderpass = device.simpleForwardRenderPass(swapchain.format) |
66 var framebuffers: seq[Framebuffer] | |
67 for imageview in swapchain.imageviews: | |
68 framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension) | |
69 | |
70 # todo: could be create inside "device", but it would be nice to have nim v2 with support for circular dependencies first | |
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
71 var |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
72 commandPool = device.createCommandPool(family=device.firstGraphicsQueue().get().family, nBuffers=1) |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
73 imageAvailable = device.createSemaphore() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
74 renderFinished = device.createSemaphore() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
75 inflight = device.createFence() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
76 |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
77 var vertexshader = device.createVertexShader("#version 450\nvoid main() {}", Vertex()) |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
78 var fragmentshader = device.createFragmentShader("#version 450\nvoid main() {}") |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
79 var pipeline = renderpass.createPipeline(vertexshader, fragmentshader) |
96 | 80 |
81 echo "All successfull" | |
82 echo "Start cleanup" | |
93 | 83 |
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
84 # cleanup |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
85 pipeline.destroy() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
86 vertexshader.destroy() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
87 fragmentshader.destroy() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
88 inflight.destroy() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
89 imageAvailable.destroy() |
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
90 renderFinished.destroy() |
98 | 91 commandPool.destroy() |
92 for fb in framebuffers.mitems: | |
93 fb.destroy() | |
94 renderpass.destroy() | |
96 | 95 swapchain.destroy() |
96 device.destroy() | |
93 | 97 |
98 debugger.destroy() | |
99 instance.destroy() |