annotate tests/test_vulkan_wrapper.nim @ 563:fb42da98c1aa

did: update to use new shader concept
author Sam <sam@basx.dev>
date Fri, 17 Mar 2023 01:11:58 +0700
parents 8de8a2102071
children 1e2027dfc642
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
563
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
10 Uniforms = object
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
11 time: float32
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
12
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
13
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
14 when isMainModule:
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
15 # print basic driver infos
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
16 echo "Layers"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
17 for layer in getLayers():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
18 echo " " & layer
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
19 echo "Instance extensions"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
20 for extension in getInstanceExtensions():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
21 echo " " & extension
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
22
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
23 # create instance
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
24 var thewindow = createWindow("Test")
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
25 var instance = thewindow.createInstance(
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
26 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
27 instanceExtensions= @["VK_EXT_debug_utils"],
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
28 layers= @["VK_LAYER_KHRONOS_validation"]
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
29 )
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
30 var debugger = instance.createDebugMessenger()
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
31
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
32 # diagnostic output
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
33 echo "Devices"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
34 for device in instance.getPhysicalDevices():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
35 echo " " & $device
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
36 echo " Rating: " & $device.rateGraphics()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
37 echo " Extensions"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
38 for extension in device.getExtensions():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
39 echo " " & $extension
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
40 echo " Properties"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
41 echo " " & $device.getProperties()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
42 echo " Features"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
43 echo " " & $device.getFeatures()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
44 echo " Queue families"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
45 for queueFamily in device.getQueueFamilies():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
46 echo " " & $queueFamily
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
47 echo " Surface present modes"
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
48 for mode in device.getSurfacePresentModes():
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
49 echo " " & $mode
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
50 echo " Surface formats"
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
51 for format in device.getSurfaceFormats():
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
52 echo " " & $format
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
53
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
54 # create devices
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
55 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
56 var device = instance.createDevice(
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
57 selectedPhysicalDevice,
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
58 @[],
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 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
61 )
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
62
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
63 # setup render pipeline
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
64 var (swapchain, res) = device.createSwapchain(device.physicalDevice.getSurfaceFormats().filterSurfaceFormat())
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
65 if res != VK_SUCCESS:
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
66 raise newException(Exception, "Unable to create swapchain")
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
67 var renderpass = device.simpleForwardRenderPass(swapchain.format)
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
68 var framebuffers: seq[Framebuffer]
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
69 for imageview in swapchain.imageviews:
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
70 framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension)
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
71
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
72 # 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
73 var
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
74 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
75 imageAvailable = device.createSemaphore()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
76 renderFinished = device.createSemaphore()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
77 inflight = device.createFence()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
78
563
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
79 var vertexshader = shader(Vertex, Uniforms, device):
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
80 shadertype: VK_SHADER_STAGE_VERTEX_BIT
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
81 entrypoint: "main"
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
82 version: 450
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
83 code: """"""
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
84 var fragmentshader = shader(Vertex, Uniforms, device):
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
85 shadertype: VK_SHADER_STAGE_FRAGMENT_BIT
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
86 entrypoint: "main"
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
87 version: 450
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
88 code: ""
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
89
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
90 #var vertexshader = loadShaderCode[Vertex, Uniforms](device, vertexshadercode)
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
91 #var fragmentshader = loadShaderCode[Vertex, Uniforms](device, fragmentshadercode)
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
92 #var pipeline = renderpass.createPipeline(vertexshaderhandle, fragmentshaderhandle)
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
93
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
94 echo "All successfull"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
95 echo "Start cleanup"
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
96
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
97 # cleanup
563
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
98 #pipeline.destroy()
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
99 #vertexshader.destroy()
fb42da98c1aa did: update to use new shader concept
Sam <sam@basx.dev>
parents: 560
diff changeset
100 #fragmentshader.destroy()
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
101 inflight.destroy()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
102 imageAvailable.destroy()
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
103 renderFinished.destroy()
559
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
104 commandPool.destroy()
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
105 for fb in framebuffers.mitems:
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
106 fb.destroy()
baaa620887b4 add: more vulkan objects
Sam <sam@basx.dev>
parents: 558
diff changeset
107 renderpass.destroy()
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
108 swapchain.destroy()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
109 device.destroy()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
110
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
111 debugger.destroy()
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
112 instance.destroy()