Mercurial > games > semicongine
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 |
rev | line source |
---|---|
557 | 1 import std/options |
2 | |
554 | 3 import semicongine/vulkan |
555 | 4 import semicongine/platform/window |
559 | 5 import semicongine/math |
570 | 6 import semicongine/entity |
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 | 11 proc diagnostics(instance: Instance) = |
555 | 12 # diagnostic output |
576 | 13 # print basic driver infos |
14 echo "Layers" | |
15 for layer in getLayers(): | |
16 echo " " & layer | |
17 echo "Instance extensions" | |
18 for extension in getInstanceExtensions(): | |
19 echo " " & extension | |
20 | |
554 | 21 echo "Devices" |
22 for device in instance.getPhysicalDevices(): | |
23 echo " " & $device | |
557 | 24 echo " Rating: " & $device.rateGraphics() |
554 | 25 echo " Extensions" |
26 for extension in device.getExtensions(): | |
27 echo " " & $extension | |
557 | 28 echo " Properties" |
29 echo " " & $device.getProperties() | |
30 echo " Features" | |
31 echo " " & $device.getFeatures() | |
554 | 32 echo " Queue families" |
33 for queueFamily in device.getQueueFamilies(): | |
34 echo " " & $queueFamily | |
555 | 35 echo " Surface present modes" |
557 | 36 for mode in device.getSurfacePresentModes(): |
555 | 37 echo " " & $mode |
38 echo " Surface formats" | |
557 | 39 for format in device.getSurfaceFormats(): |
555 | 40 echo " " & $format |
554 | 41 |
565 | 42 when isMainModule: |
43 | |
576 | 44 # INIT ENGINE: |
565 | 45 # create instance |
46 var thewindow = createWindow("Test") | |
47 var instance = thewindow.createInstance( | |
48 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), | |
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 | 51 ) |
52 var debugger = instance.createDebugMessenger() | |
554 | 53 # create devices |
557 | 54 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
55 var device = instance.createDevice( | |
56 selectedPhysicalDevice, | |
57 @[], | |
58 @[], | |
59 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
60 ) | |
61 | |
576 | 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 | 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 | 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 | 88 if res != VK_SUCCESS: |
89 raise newException(Exception, "Unable to create swapchain") | |
557 | 90 |
576 | 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 | 102 |
576 | 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 | 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 | 111 echo "Start cleanup" |
554 | 112 |
576 | 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 | 115 |
576 | 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 | 120 swapchain.destroy() |
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
121 |
576 | 122 # destroy engine |
557 | 123 device.destroy() |
554 | 124 debugger.destroy() |
125 instance.destroy() |