Mercurial > games > semicongine
annotate tests/test_vulkan_wrapper.nim @ 583:90537a8887ae
did: implement uniforms, some refactoring
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sun, 09 Apr 2023 01:04:54 +0700 |
| parents | 8cf5d3f921ae |
| children | 55be3579dc30 |
| rev | line source |
|---|---|
| 557 | 1 import std/options |
| 2 | |
| 578 | 3 import semicongine |
|
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
4 |
| 565 | 5 proc diagnostics(instance: Instance) = |
| 555 | 6 # diagnostic output |
| 576 | 7 # print basic driver infos |
| 8 echo "Layers" | |
| 9 for layer in getLayers(): | |
| 10 echo " " & layer | |
| 11 echo "Instance extensions" | |
| 12 for extension in getInstanceExtensions(): | |
| 13 echo " " & extension | |
| 14 | |
| 554 | 15 echo "Devices" |
| 16 for device in instance.getPhysicalDevices(): | |
| 17 echo " " & $device | |
| 557 | 18 echo " Rating: " & $device.rateGraphics() |
| 554 | 19 echo " Extensions" |
| 20 for extension in device.getExtensions(): | |
| 21 echo " " & $extension | |
| 557 | 22 echo " Properties" |
| 23 echo " " & $device.getProperties() | |
| 24 echo " Features" | |
| 25 echo " " & $device.getFeatures() | |
| 554 | 26 echo " Queue families" |
| 27 for queueFamily in device.getQueueFamilies(): | |
| 28 echo " " & $queueFamily | |
| 555 | 29 echo " Surface present modes" |
| 557 | 30 for mode in device.getSurfacePresentModes(): |
| 555 | 31 echo " " & $mode |
| 32 echo " Surface formats" | |
| 557 | 33 for format in device.getSurfaceFormats(): |
| 555 | 34 echo " " & $format |
| 554 | 35 |
| 565 | 36 when isMainModule: |
| 576 | 37 # INIT ENGINE: |
| 565 | 38 # create instance |
| 39 var thewindow = createWindow("Test") | |
| 40 var instance = thewindow.createInstance( | |
| 41 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), | |
| 42 instanceExtensions= @["VK_EXT_debug_utils"], | |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
43 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"] |
| 565 | 44 ) |
| 45 var debugger = instance.createDebugMessenger() | |
| 554 | 46 # create devices |
| 557 | 47 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
| 48 var device = instance.createDevice( | |
| 49 selectedPhysicalDevice, | |
| 50 @[], | |
| 581 | 51 @["VK_EXT_index_type_uint8"], |
| 557 | 52 selectedPhysicalDevice.filterForGraphicsPresentationQueues() |
| 53 ) | |
| 54 | |
| 576 | 55 # INIT RENDERER: |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
56 const |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
57 vertexInput = @[ |
|
582
8cf5d3f921ae
did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents:
581
diff
changeset
|
58 attr[Vec3f]("position"), |
|
8cf5d3f921ae
did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents:
581
diff
changeset
|
59 attr[Vec3f]("color"), |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
60 attr[Mat4]("transform", perInstance=true) |
|
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
61 ] |
|
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
62 vertexOutput = @[attr[Vec3f]("outcolor")] |
|
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
63 uniforms = @[attr[float32]("time")] |
|
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
64 fragOutput = @[attr[Vec4f]("color")] |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
65 vertexCode = compileGlslShader( |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
66 stage=VK_SHADER_STAGE_VERTEX_BIT, |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
67 inputs=vertexInput, |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
68 uniforms=uniforms, |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
69 outputs=vertexOutput, |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
70 body="""gl_Position = vec4(position, 1.0); outcolor = color * sin(Uniforms.time) * 0.5 + 0.5;""" |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
71 ) |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
72 fragmentCode = compileGlslShader( |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
73 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
74 inputs=vertexOutput, |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
75 uniforms=uniforms, |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
76 outputs=fragOutput, |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
77 body="color = vec4(outcolor, 1);" |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
78 ) |
| 567 | 79 var |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
80 vertexshader = device.createShaderModule(vertexCode) |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
81 fragmentshader = device.createShaderModule(fragmentCode) |
| 569 | 82 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat() |
|
572
9f1de117aaca
did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
570
diff
changeset
|
83 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
|
84 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2) |
| 569 | 85 if res != VK_SUCCESS: |
| 86 raise newException(Exception, "Unable to create swapchain") | |
| 557 | 87 |
| 576 | 88 # INIT SCENE |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
89 var time = initShaderGlobal("time", 0.0'f32) |
|
574
bbeec60e25ca
did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents:
573
diff
changeset
|
90 var thescene = Scene( |
|
bbeec60e25ca
did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents:
573
diff
changeset
|
91 name: "main", |
|
bbeec60e25ca
did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents:
573
diff
changeset
|
92 root: newEntity("root", |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
93 newEntity("stuff", time), |
| 578 | 94 newEntity("triangle1", newMesh( |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
95 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
|
96 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
|
97 )), |
| 581 | 98 newEntity("triangle1b", newMesh( |
| 99 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], | |
| 100 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | |
| 101 )), | |
| 102 newEntity("triangle2a", newMesh( | |
| 103 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], | |
| 104 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | |
| 105 indices=[[0'u16, 2'u16, 1'u16]] | |
| 106 )), | |
| 107 newEntity("triangle2b", newMesh( | |
| 108 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], | |
| 109 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | |
| 110 indices=[[0'u16, 2'u16, 1'u16]] | |
| 111 )), | |
| 112 newEntity("triangle3a", newMesh( | |
| 113 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | |
| 114 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], | |
| 115 indices=[[0'u32, 2'u32, 1'u32]], | |
| 116 autoResize=false | |
| 117 )), | |
| 118 newEntity("triangle3b", newMesh( | |
| 119 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | |
| 120 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], | |
| 121 indices=[[0'u32, 2'u32, 1'u32]], | |
| 122 autoResize=false | |
| 123 )), | |
|
574
bbeec60e25ca
did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents:
573
diff
changeset
|
124 ) |
|
bbeec60e25ca
did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents:
573
diff
changeset
|
125 ) |
|
572
9f1de117aaca
did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
570
diff
changeset
|
126 thescene.setupDrawables(renderPass) |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
127 swapchain.setupUniforms(thescene) |
| 570 | 128 |
| 576 | 129 # MAINLOOP |
|
572
9f1de117aaca
did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
570
diff
changeset
|
130 echo "Setup successfull, start rendering" |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
131 for i in 0 ..< 1: |
|
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
132 setValue[float32](time.value, get[float32](time.value) + 0.0005) |
|
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
133 echo get[float32](time.value) |
|
572
9f1de117aaca
did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
570
diff
changeset
|
134 discard swapchain.drawScene(thescene) |
| 569 | 135 echo "Rendered ", swapchain.framesRendered, " frames" |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
136 checkVkResult device.vk.vkDeviceWaitIdle() |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
137 |
|
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
138 # cleanup |
| 557 | 139 echo "Start cleanup" |
| 554 | 140 |
| 576 | 141 # destroy scene |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
142 thescene.destroy() |
| 569 | 143 |
| 576 | 144 # destroy renderer |
|
564
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
145 vertexshader.destroy() |
|
20837928dc63
add: finally working initial approach for shader definitions
Sam <sam@basx.dev>
parents:
563
diff
changeset
|
146 fragmentshader.destroy() |
|
572
9f1de117aaca
did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
570
diff
changeset
|
147 renderPass.destroy() |
| 557 | 148 swapchain.destroy() |
|
575
eaedc0369c38
yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents:
574
diff
changeset
|
149 |
| 576 | 150 # destroy engine |
| 557 | 151 device.destroy() |
| 554 | 152 debugger.destroy() |
| 153 instance.destroy() | |
|
583
90537a8887ae
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
582
diff
changeset
|
154 thewindow.destroy() |
