annotate tests/test_vulkan_wrapper.nim @ 561:4a9fc283c3af

add: initial version of better shader-shit
author Sam <sam@basx.dev>
date Fri, 17 Mar 2023 01:11:18 +0700
parents 8de8a2102071
children c782d7e52b25
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
1 import std/options
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
2
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine/vulkan
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
4 import semicongine/platform/window
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
5 import semicongine/math
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
6
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
7 type
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
8 Vertex = object
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
9 pos: Vec3
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
10
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
11
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
12 when isMainModule:
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
13 # print basic driver infos
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
14 echo "Layers"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
15 for layer in getLayers():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
16 echo " " & layer
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
17 echo "Instance extensions"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
18 for extension in getInstanceExtensions():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
19 echo " " & extension
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
20
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
21 # create instance
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
22 var thewindow = createWindow("Test")
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
23 var instance = thewindow.createInstance(
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
24 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
25 instanceExtensions= @["VK_EXT_debug_utils"],
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
26 layers= @["VK_LAYER_KHRONOS_validation"]
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
27 )
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
28 var debugger = instance.createDebugMessenger()
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
29
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
30 # diagnostic output
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
31 echo "Devices"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
32 for device in instance.getPhysicalDevices():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
33 echo " " & $device
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
34 echo " Rating: " & $device.rateGraphics()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
35 echo " Extensions"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
36 for extension in device.getExtensions():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
37 echo " " & $extension
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
38 echo " Properties"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
39 echo " " & $device.getProperties()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
40 echo " Features"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
41 echo " " & $device.getFeatures()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
42 echo " Queue families"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
43 for queueFamily in device.getQueueFamilies():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
44 echo " " & $queueFamily
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
45 echo " Surface present modes"
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
46 for mode in device.getSurfacePresentModes():
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
47 echo " " & $mode
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
48 echo " Surface formats"
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
49 for format in device.getSurfaceFormats():
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
50 echo " " & $format
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
51
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
52 # create devices
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
53 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
54 var device = instance.createDevice(
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
55 selectedPhysicalDevice,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
56 @[],
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
57 @[],
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
58 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
59 )
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
60
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
61 # setup render pipeline
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
62 var (swapchain, res) = device.createSwapchain(device.physicalDevice.getSurfaceFormats().filterSurfaceFormat())
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
63 if res != VK_SUCCESS:
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
64 raise newException(Exception, "Unable to create swapchain")
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
65 var renderpass = device.simpleForwardRenderPass(swapchain.format)
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
66 var framebuffers: seq[Framebuffer]
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
67 for imageview in swapchain.imageviews:
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
68 framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension)
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
69
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
70 # todo: could be create inside "device", but it would be nice to have nim v2 with support for circular dependencies first
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
71 var
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
72 commandPool = device.createCommandPool(family=device.firstGraphicsQueue().get().family, nBuffers=1)
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
73 imageAvailable = device.createSemaphore()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
74 renderFinished = device.createSemaphore()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
75 inflight = device.createFence()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
76
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
77 var vertexshader = device.createVertexShader("#version 450\nvoid main() {}", Vertex())
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
78 var fragmentshader = device.createFragmentShader("#version 450\nvoid main() {}")
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
79 var pipeline = renderpass.createPipeline(vertexshader, fragmentshader)
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
80
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
81 echo "All successfull"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
82 echo "Start cleanup"
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
83
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
84 # cleanup
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
85 pipeline.destroy()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
86 vertexshader.destroy()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
87 fragmentshader.destroy()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
88 inflight.destroy()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
89 imageAvailable.destroy()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
90 renderFinished.destroy()
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
91 commandPool.destroy()
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
92 for fb in framebuffers.mitems:
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
93 fb.destroy()
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
94 renderpass.destroy()
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
95 swapchain.destroy()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
96 device.destroy()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
97
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
98 debugger.destroy()
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
99 instance.destroy()