annotate tests/test_vulkan_wrapper.nim @ 105:4059aa0d689b

add: descriptors, better swapchain implementation
author Sam <sam@basx.dev>
date Fri, 24 Mar 2023 00:11:42 +0700
parents 9eeb9a44d158
children 9c3e3dcb0328
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
1 import std/options
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
2
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine/vulkan
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 93
diff changeset
4 import semicongine/platform/window
98
335a65d3f892 add: more vulkan objects
Sam <sam@basx.dev>
parents: 97
diff changeset
5 import semicongine/math
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
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
103
1e2027dfc642 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 102
diff changeset
10 FragmentInput = object
1e2027dfc642 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 102
diff changeset
11 fragpos: Vec3
102
c782d7e52b25 did: update to use new shader concept
Sam <sam@basx.dev>
parents: 99
diff changeset
12 Uniforms = object
c782d7e52b25 did: update to use new shader concept
Sam <sam@basx.dev>
parents: 99
diff changeset
13 time: float32
103
1e2027dfc642 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 102
diff changeset
14 Pixel = object
1e2027dfc642 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 102
diff changeset
15 color: Vec4
99
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
16
104
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
17 proc diagnostics(instance: Instance) =
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 93
diff changeset
18 # diagnostic output
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
19 echo "Devices"
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
20 for device in instance.getPhysicalDevices():
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
21 echo " " & $device
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
22 echo " Rating: " & $device.rateGraphics()
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
23 echo " Extensions"
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
24 for extension in device.getExtensions():
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
25 echo " " & $extension
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
26 echo " Properties"
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
27 echo " " & $device.getProperties()
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
28 echo " Features"
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
29 echo " " & $device.getFeatures()
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
30 echo " Queue families"
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
31 for queueFamily in device.getQueueFamilies():
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
32 echo " " & $queueFamily
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 93
diff changeset
33 echo " Surface present modes"
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
34 for mode in device.getSurfacePresentModes():
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 93
diff changeset
35 echo " " & $mode
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 93
diff changeset
36 echo " Surface formats"
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
37 for format in device.getSurfaceFormats():
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 93
diff changeset
38 echo " " & $format
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
39
104
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
40 when isMainModule:
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
41 # print basic driver infos
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
42 echo "Layers"
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
43 for layer in getLayers():
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
44 echo " " & layer
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
45 echo "Instance extensions"
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
46 for extension in getInstanceExtensions():
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
47 echo " " & extension
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
48
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
49 # create instance
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
50 var thewindow = createWindow("Test")
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
51 var instance = thewindow.createInstance(
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
52 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
53 instanceExtensions= @["VK_EXT_debug_utils"],
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
54 layers= @["VK_LAYER_KHRONOS_validation"]
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
55 )
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
56 var debugger = instance.createDebugMessenger()
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
57
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
58 # create devices
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
59 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
60 var device = instance.createDevice(
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
61 selectedPhysicalDevice,
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
62 @[],
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
63 @[],
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
64 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
65 )
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
66
98
335a65d3f892 add: more vulkan objects
Sam <sam@basx.dev>
parents: 97
diff changeset
67 # setup render pipeline
105
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
68 var surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
69 var renderpass = device.simpleForwardRenderPass(surfaceFormat.format)
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
70 var (swapchain, res) = device.createSwapchain(renderpass, surfaceFormat)
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
71 if res != VK_SUCCESS:
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
72 raise newException(Exception, "Unable to create swapchain")
105
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
73 # var framebuffers: seq[Framebuffer]
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
74 # for imageview in swapchain.imageviews:
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
75 # framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension)
98
335a65d3f892 add: more vulkan objects
Sam <sam@basx.dev>
parents: 97
diff changeset
76
335a65d3f892 add: more vulkan objects
Sam <sam@basx.dev>
parents: 97
diff changeset
77 # 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
78 var
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
79 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
80 imageAvailable = device.createSemaphore()
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
81 renderFinished = device.createSemaphore()
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
82 inflight = device.createFence()
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
83
104
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
84 const vertexBinary = shaderCode[Vertex, Uniforms, FragmentInput](stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;")
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
85 const fragmentBinary = shaderCode[FragmentInput, void, Pixel](stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);")
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
86 var vertexshader = createShader[Vertex, Uniforms, FragmentInput](device, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary)
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
87 var fragmentshader = createShader[FragmentInput, void, Pixel](device, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary)
102
c782d7e52b25 did: update to use new shader concept
Sam <sam@basx.dev>
parents: 99
diff changeset
88
103
1e2027dfc642 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 102
diff changeset
89 var pipeline = renderpass.createPipeline(vertexshader, fragmentshader)
105
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
90 var descriptorPool = device.createDescriptorSetPool(@[(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1'u32)])
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
91 var descriptorSet = descriptorPool.allocateDescriptorSet(pipeline.descriptorSetLayout, 1)
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
92
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
93 echo "All successfull"
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
94 echo "Start cleanup"
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
95
99
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
96 # cleanup
105
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
97 checkVkResult device.vk.vkDeviceWaitIdle()
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 104
diff changeset
98 descriptorPool.destroy()
103
1e2027dfc642 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 102
diff changeset
99 vertexshader.destroy()
1e2027dfc642 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 102
diff changeset
100 fragmentshader.destroy()
104
9eeb9a44d158 did: some refactoring
Sam <sam@basx.dev>
parents: 103
diff changeset
101 pipeline.destroy()
99
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
102 inflight.destroy()
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
103 imageAvailable.destroy()
4deffc94484a add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 98
diff changeset
104 renderFinished.destroy()
98
335a65d3f892 add: more vulkan objects
Sam <sam@basx.dev>
parents: 97
diff changeset
105 commandPool.destroy()
335a65d3f892 add: more vulkan objects
Sam <sam@basx.dev>
parents: 97
diff changeset
106 renderpass.destroy()
96
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
107 swapchain.destroy()
b9fc90de1450 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 94
diff changeset
108 device.destroy()
93
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
109
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
110 debugger.destroy()
cb773e81032f add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
111 instance.destroy()