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