Mercurial > games > semicongine
comparison tests/test_vulkan_wrapper.nim @ 589:b434feaf2b67
did: finish refactoring of render pipeline, yipi! :)
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 17 Apr 2023 18:02:19 +0700 |
parents | 008592db0442 |
children | 15d37022625c |
comparison
equal
deleted
inserted
replaced
588:008592db0442 | 589:b434feaf2b67 |
---|---|
31 echo " " & $mode | 31 echo " " & $mode |
32 echo " Surface formats" | 32 echo " Surface formats" |
33 for format in device.getSurfaceFormats(): | 33 for format in device.getSurfaceFormats(): |
34 echo " " & $format | 34 echo " " & $format |
35 | 35 |
36 proc scene_different_mesh_types(): Scene = | 36 proc scene_different_mesh_types(): Entity = |
37 result = Scene( | 37 result = newEntity("root", |
38 name: "main", | 38 newEntity("triangle1", newMesh( |
39 root: newEntity("root", | 39 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], |
40 newEntity("triangle1", newMesh( | 40 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], |
41 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], | 41 )), |
42 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | 42 newEntity("triangle1b", newMesh( |
43 )), | 43 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], |
44 newEntity("triangle1b", newMesh( | 44 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], |
45 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], | 45 )), |
46 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | 46 newEntity("triangle2a", newMesh( |
47 )), | 47 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], |
48 newEntity("triangle2a", newMesh( | 48 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], |
49 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], | 49 indices=[[0'u16, 2'u16, 1'u16]] |
50 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | 50 )), |
51 indices=[[0'u16, 2'u16, 1'u16]] | 51 newEntity("triangle2b", newMesh( |
52 )), | 52 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], |
53 newEntity("triangle2b", newMesh( | 53 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], |
54 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], | 54 indices=[[0'u16, 2'u16, 1'u16]] |
55 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | 55 )), |
56 indices=[[0'u16, 2'u16, 1'u16]] | 56 newEntity("triangle3a", newMesh( |
57 )), | 57 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], |
58 newEntity("triangle3a", newMesh( | 58 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], |
59 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | 59 indices=[[0'u32, 2'u32, 1'u32]], |
60 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], | 60 autoResize=false |
61 indices=[[0'u32, 2'u32, 1'u32]], | 61 )), |
62 autoResize=false | 62 newEntity("triangle3b", newMesh( |
63 )), | 63 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], |
64 newEntity("triangle3b", newMesh( | 64 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], |
65 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | 65 indices=[[0'u32, 2'u32, 1'u32]], |
66 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], | 66 autoResize=false |
67 indices=[[0'u32, 2'u32, 1'u32]], | 67 )), |
68 autoResize=false | |
69 )), | |
70 ) | |
71 ) | 68 ) |
69 for mesh in allComponentsOfType[Mesh](result): | |
70 mesh.setInstanceData("translate", @[newVec3f()]) | |
72 | 71 |
73 proc scene_simple(): Scene = | 72 proc scene_simple(): Entity = |
74 var mymesh1 = newMesh( | 73 var mymesh1 = newMesh( |
75 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], | 74 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], |
76 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], | 75 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], |
77 ) | 76 ) |
78 var mymesh2 = newMesh( | 77 var mymesh2 = newMesh( |
93 ) | 92 ) |
94 mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)]) | 93 mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)]) |
95 mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)]) | 94 mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)]) |
96 mymesh3.setInstanceData("translate", @[newVec3f(-0.3, 0.0)]) | 95 mymesh3.setInstanceData("translate", @[newVec3f(-0.3, 0.0)]) |
97 mymesh4.setInstanceData("translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)]) | 96 mymesh4.setInstanceData("translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)]) |
98 result = Scene( | 97 result = newEntity("root", newEntity("triangle", mymesh4, mymesh3, mymesh2, mymesh1)) |
99 name: "main", | |
100 root: newEntity("root", newEntity("triangle", mymesh4, mymesh3, mymesh2, mymesh1)) | |
101 ) | |
102 | 98 |
103 proc scene_primitives(): Scene = | 99 proc scene_primitives(): Entity = |
104 var r = rect(color="ff0000") | 100 var r = rect(color="ff0000") |
105 var t = tri(color="0000ff") | 101 var t = tri(color="0000ff") |
106 var c = circle(color="00ff00") | 102 var c = circle(color="00ff00") |
107 setInstanceData[Vec3f](r, "translate", @[newVec3f(0.5, -0.3)]) | 103 setInstanceData[Vec3f](r, "translate", @[newVec3f(0.5, -0.3)]) |
108 setInstanceData[Vec3f](t, "translate", @[newVec3f(0.3, 0.3)]) | 104 setInstanceData[Vec3f](t, "translate", @[newVec3f(0.3, 0.3)]) |
109 setInstanceData[Vec3f](c, "translate", @[newVec3f(-0.3, 0.1)]) | 105 setInstanceData[Vec3f](c, "translate", @[newVec3f(-0.3, 0.1)]) |
110 result = Scene( | 106 result = newEntity("root", t, r, c) |
111 name: "main", | |
112 root: newEntity("root", t, r, c) | |
113 ) | |
114 | 107 |
115 when isMainModule: | 108 when isMainModule: |
116 var engine = initEngine("Test") | 109 var engine = initEngine("Test") |
117 | 110 |
118 # INIT RENDERER: | 111 # INIT RENDERER: |
138 uniforms=uniforms, | 131 uniforms=uniforms, |
139 outputs=fragOutput, | 132 outputs=fragOutput, |
140 main="color = vec4(outcolor, 1);" | 133 main="color = vec4(outcolor, 1);" |
141 ) | 134 ) |
142 var | 135 var |
143 vertexshader = engine.gpuDevice.createShaderModule(vertexCode) | |
144 fragmentshader = engine.gpuDevice.createShaderModule(fragmentCode) | |
145 surfaceFormat = engine.gpuDevice.physicalDevice.getSurfaceFormats().filterSurfaceFormat() | 136 surfaceFormat = engine.gpuDevice.physicalDevice.getSurfaceFormats().filterSurfaceFormat() |
146 renderPass = engine.gpuDevice.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2) | 137 renderPass = engine.gpuDevice.simpleForwardRenderPass(surfaceFormat.format, vertexCode, fragmentCode, 2) |
147 (swapchain, res) = engine.gpuDevice.createSwapchain(renderPass, surfaceFormat, engine.gpuDevice.firstGraphicsQueue().get().family, 2) | 138 renderer = engine.gpuDevice.initRenderer([renderPass]) |
148 if res != VK_SUCCESS: | |
149 raise newException(Exception, "Unable to create swapchain") | |
150 | 139 |
151 # INIT SCENE | 140 # INIT SCENE |
152 | 141 |
153 # var thescene = scene_simple() | 142 var scenes = [scene_simple(), scene_different_mesh_types(), scene_primitives()] |
154 # var thescene = scene_different_mesh_types() | |
155 var thescene = scene_primitives() | |
156 var time = initShaderGlobal("time", 0.0'f32) | 143 var time = initShaderGlobal("time", 0.0'f32) |
157 thescene.root.components.add time | 144 for scene in scenes.mitems: |
158 thescene.setupDrawableBuffers(engine.gpuDevice, vertexInput) | 145 scene.components.add time |
146 renderer.setupDrawableBuffers(scene, vertexInput) | |
159 | 147 |
160 # MAINLOOP | 148 # MAINLOOP |
161 echo "Setup successfull, start rendering" | 149 echo "Setup successfull, start rendering" |
162 for i in 0 ..< 10000: | 150 for i in 0 ..< 3: |
163 setValue[float32](time.value, get[float32](time.value) + 0.0005) | 151 for scene in scenes: |
164 discard swapchain.drawScene(thescene) | 152 for i in 0 ..< 1000: |
165 echo "Rendered ", swapchain.framesRendered, " frames" | 153 setValue[float32](time.value, get[float32](time.value) + 0.0005) |
154 discard renderer.render(scene) | |
155 echo "Rendered ", renderer.framesRendered, " frames" | |
166 | 156 |
167 # cleanup | 157 # cleanup |
168 echo "Start cleanup" | 158 echo "Start cleanup" |
169 checkVkResult engine.gpuDevice.vk.vkDeviceWaitIdle() | 159 checkVkResult engine.gpuDevice.vk.vkDeviceWaitIdle() |
170 | 160 renderer.destroy() |
171 # destroy scene | |
172 thescene.destroy() | |
173 | |
174 # destroy renderer | |
175 vertexshader.destroy() | |
176 fragmentshader.destroy() | |
177 renderPass.destroy() | |
178 swapchain.destroy() | |
179 | |
180 # destroy engine | |
181 engine.destroy() | 161 engine.destroy() |