annotate tests/test_vulkan_wrapper.nim @ 576:677e4f7fb567

add: comments for further refactoring
author Sam <sam@basx.dev>
date Mon, 03 Apr 2023 00:10:08 +0700
parents eaedc0369c38
children 40ff2453855e
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
570
1cf928eb0a10 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 569
diff changeset
6 import semicongine/entity
1cf928eb0a10 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 569
diff changeset
7 import semicongine/scene
572
9f1de117aaca did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents: 570
diff changeset
8 import semicongine/gpu_data
573
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
9 import semicongine/mesh
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents: 559
diff changeset
10
565
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
11 proc diagnostics(instance: Instance) =
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
12 # diagnostic output
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
13 # print basic driver infos
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
14 echo "Layers"
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
15 for layer in getLayers():
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
16 echo " " & layer
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
17 echo "Instance extensions"
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
18 for extension in getInstanceExtensions():
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
19 echo " " & extension
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
20
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
21 echo "Devices"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
22 for device in instance.getPhysicalDevices():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
23 echo " " & $device
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
24 echo " Rating: " & $device.rateGraphics()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
25 echo " Extensions"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
26 for extension in device.getExtensions():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
27 echo " " & $extension
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
28 echo " Properties"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
29 echo " " & $device.getProperties()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
30 echo " Features"
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
31 echo " " & $device.getFeatures()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
32 echo " Queue families"
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
33 for queueFamily in device.getQueueFamilies():
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
34 echo " " & $queueFamily
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
35 echo " Surface present modes"
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
36 for mode in device.getSurfacePresentModes():
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
37 echo " " & $mode
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
38 echo " Surface formats"
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
39 for format in device.getSurfaceFormats():
555
7c44f714bd03 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 554
diff changeset
40 echo " " & $format
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
41
565
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
42 when isMainModule:
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
43
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
44 # INIT ENGINE:
565
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
45 # create instance
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
46 var thewindow = createWindow("Test")
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
47 var instance = thewindow.createInstance(
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
48 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
49 instanceExtensions= @["VK_EXT_debug_utils"],
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
50 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"]
565
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
51 )
a0b01e84607d did: some refactoring
Sam <sam@basx.dev>
parents: 564
diff changeset
52 var debugger = instance.createDebugMessenger()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
53 # create devices
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
54 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
55 var device = instance.createDevice(
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
56 selectedPhysicalDevice,
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 @[],
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
59 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
60 )
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
61
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
62 # INIT RENDERER:
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
63 const
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
64 vertexInput = initAttributeGroup(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
65 asAttribute(default(Vec3f), "position"),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
66 asAttribute(default(Vec3f), "color"),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
67 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
68 vertexOutput = initAttributeGroup(asAttribute(default(Vec3f), "outcolor"))
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
69 fragOutput = initAttributeGroup(asAttribute(default(Vec4f), "color"))
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
70 vertexCode = compileGlslShader(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
71 stage=VK_SHADER_STAGE_VERTEX_BIT,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
72 inputs=vertexInput,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
73 outputs=vertexOutput,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
74 body="""gl_Position = vec4(position, 1.0); outcolor = color;"""
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
75 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
76 fragmentCode = compileGlslShader(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
77 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
78 inputs=vertexOutput,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
79 outputs=fragOutput,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
80 body="color = vec4(outcolor, 1);"
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
81 )
567
05ac2455ff60 add: render main loop structure
Sam <sam@basx.dev>
parents: 566
diff changeset
82 var
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
83 vertexshader = device.createShaderModule(vertexCode)
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
84 fragmentshader = device.createShaderModule(fragmentCode)
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
85 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
572
9f1de117aaca did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents: 570
diff changeset
86 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
87 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
88 if res != VK_SUCCESS:
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
89 raise newException(Exception, "Unable to create swapchain")
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
90
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
91 # INIT SCENE
574
bbeec60e25ca did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 573
diff changeset
92 var thescene = Scene(
bbeec60e25ca did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 573
diff changeset
93 name: "main",
bbeec60e25ca did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 573
diff changeset
94 root: newEntity("root",
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
95 newEntity("triangle1", initMesh(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
96 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
97 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
98 )),
574
bbeec60e25ca did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 573
diff changeset
99 )
bbeec60e25ca did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 573
diff changeset
100 )
572
9f1de117aaca did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents: 570
diff changeset
101 thescene.setupDrawables(renderPass)
570
1cf928eb0a10 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 569
diff changeset
102
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
103 # MAINLOOP
572
9f1de117aaca did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents: 570
diff changeset
104 echo "Setup successfull, start rendering"
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
105 for i in 0 ..< 1000:
572
9f1de117aaca did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents: 570
diff changeset
106 discard swapchain.drawScene(thescene)
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
107 echo "Rendered ", swapchain.framesRendered, " frames"
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
108 checkVkResult device.vk.vkDeviceWaitIdle()
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
109
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
110 # cleanup
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
111 echo "Start cleanup"
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
112
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
113 # destroy scene
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
114 thescene.destroy()
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
115
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
116 # destroy renderer
564
20837928dc63 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 563
diff changeset
117 vertexshader.destroy()
20837928dc63 add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents: 563
diff changeset
118 fragmentshader.destroy()
572
9f1de117aaca did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents: 570
diff changeset
119 renderPass.destroy()
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
120 swapchain.destroy()
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 574
diff changeset
121
576
677e4f7fb567 add: comments for further refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
122 # destroy engine
557
e2d8c02f1548 add: swapchain API, more refactoring
Sam <sam@basx.dev>
parents: 555
diff changeset
123 device.destroy()
554
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
124 debugger.destroy()
f9a5ada2df1e add: testing for vulkan API wrapper
Sam <sam@basx.dev>
parents:
diff changeset
125 instance.destroy()