comparison tests/test_vulkan_wrapper.nim @ 588:008592db0442

did: big refactoring
author Sam <sam@basx.dev>
date Wed, 12 Apr 2023 01:20:53 +0700
parents 713ac2886355
children 9901ec3831d1
comparison
equal deleted inserted replaced
587:713ac2886355 588:008592db0442
111 name: "main", 111 name: "main",
112 root: newEntity("root", t, r, c) 112 root: newEntity("root", t, r, c)
113 ) 113 )
114 114
115 when isMainModule: 115 when isMainModule:
116 # INIT ENGINE: 116 var engine = initEngine("Test")
117 # create instance
118 var thewindow = createWindow("Test")
119 var instance = thewindow.createInstance(
120 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
121 instanceExtensions= @["VK_EXT_debug_utils"],
122 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"]
123 )
124 var debugger = instance.createDebugMessenger()
125 # create devices
126 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
127 var device = instance.createDevice(
128 selectedPhysicalDevice,
129 @[],
130 @["VK_EXT_index_type_uint8"],
131 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
132 )
133 117
134 # INIT RENDERER: 118 # INIT RENDERER:
135 const 119 const
136 vertexInput = @[ 120 vertexInput = @[
137 attr[Vec3f]("position", memoryLocation=VRAM), 121 attr[Vec3f]("position", memoryLocation=VRAM),
144 vertexCode = compileGlslShader( 128 vertexCode = compileGlslShader(
145 stage=VK_SHADER_STAGE_VERTEX_BIT, 129 stage=VK_SHADER_STAGE_VERTEX_BIT,
146 inputs=vertexInput, 130 inputs=vertexInput,
147 uniforms=uniforms, 131 uniforms=uniforms,
148 outputs=vertexOutput, 132 outputs=vertexOutput,
149 body="""gl_Position = vec4(position + translate, 1.0); outcolor = color;""" 133 main="""gl_Position = vec4(position + translate, 1.0); outcolor = color;"""
150 ) 134 )
151 fragmentCode = compileGlslShader( 135 fragmentCode = compileGlslShader(
152 stage=VK_SHADER_STAGE_FRAGMENT_BIT, 136 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
153 inputs=vertexOutput, 137 inputs=vertexOutput,
154 uniforms=uniforms, 138 uniforms=uniforms,
155 outputs=fragOutput, 139 outputs=fragOutput,
156 body="color = vec4(outcolor, 1);" 140 main="color = vec4(outcolor, 1);"
157 ) 141 )
158 var 142 var
159 vertexshader = device.createShaderModule(vertexCode) 143 vertexshader = engine.gpuDevice.createShaderModule(vertexCode)
160 fragmentshader = device.createShaderModule(fragmentCode) 144 fragmentshader = engine.gpuDevice.createShaderModule(fragmentCode)
161 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat() 145 surfaceFormat = engine.gpuDevice.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
162 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2) 146 renderPass = engine.gpuDevice.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
163 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2) 147 (swapchain, res) = engine.gpuDevice.createSwapchain(renderPass, surfaceFormat, engine.gpuDevice.firstGraphicsQueue().get().family, 2)
164 if res != VK_SUCCESS: 148 if res != VK_SUCCESS:
165 raise newException(Exception, "Unable to create swapchain") 149 raise newException(Exception, "Unable to create swapchain")
166 150
167 # INIT SCENE 151 # INIT SCENE
168 var time = initShaderGlobal("time", 0.0'f32)
169 152
170 # var thescene = scene_simple() 153 # var thescene = scene_simple()
171 # var thescene = scene_different_mesh_types() 154 # var thescene = scene_different_mesh_types()
172 var thescene = scene_primitives() 155 var thescene = scene_primitives()
156 var time = initShaderGlobal("time", 0.0'f32)
173 thescene.root.components.add time 157 thescene.root.components.add time
174 thescene.setupDrawables(renderPass) 158 thescene.setupDrawableBuffers(engine.gpuDevice, vertexInput)
175 swapchain.setupUniforms(thescene)
176 159
177 # MAINLOOP 160 # MAINLOOP
178 echo "Setup successfull, start rendering" 161 echo "Setup successfull, start rendering"
179 for i in 0 ..< 10000: 162 for i in 0 ..< 10000:
180 setValue[float32](time.value, get[float32](time.value) + 0.0005) 163 setValue[float32](time.value, get[float32](time.value) + 0.0005)
181 discard swapchain.drawScene(thescene) 164 discard swapchain.drawScene(thescene)
182 echo "Rendered ", swapchain.framesRendered, " frames" 165 echo "Rendered ", swapchain.framesRendered, " frames"
183 checkVkResult device.vk.vkDeviceWaitIdle()
184 166
185 # cleanup 167 # cleanup
186 echo "Start cleanup" 168 echo "Start cleanup"
169 checkVkResult engine.gpuDevice.vk.vkDeviceWaitIdle()
187 170
188 # destroy scene 171 # destroy scene
189 thescene.destroy() 172 thescene.destroy()
190 173
191 # destroy renderer 174 # destroy renderer
193 fragmentshader.destroy() 176 fragmentshader.destroy()
194 renderPass.destroy() 177 renderPass.destroy()
195 swapchain.destroy() 178 swapchain.destroy()
196 179
197 # destroy engine 180 # destroy engine
198 device.destroy() 181 engine.destroy()
199 debugger.destroy()
200 instance.destroy()
201 thewindow.destroy()