diff tests/test_vulkan_wrapper.nim @ 128:9901ec3831d1

did: finish refactoring of render pipeline, yipi! :)
author Sam <sam@basx.dev>
date Mon, 17 Apr 2023 18:02:19 +0700
parents 5871acc2977e
children 15d37022625c
line wrap: on
line diff
--- a/tests/test_vulkan_wrapper.nim	Wed Apr 12 01:20:53 2023 +0700
+++ b/tests/test_vulkan_wrapper.nim	Mon Apr 17 18:02:19 2023 +0700
@@ -33,44 +33,43 @@
     for format in device.getSurfaceFormats():
       echo "    " & $format
 
-proc scene_different_mesh_types(): Scene =
-  result = Scene(
-    name: "main",
-    root: newEntity("root",
-      newEntity("triangle1", newMesh(
-        positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
-        colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
-      )),
-      newEntity("triangle1b", newMesh(
-        positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)],
-        colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
-      )),
-      newEntity("triangle2a", newMesh(
-        positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)],
-        colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
-        indices=[[0'u16, 2'u16, 1'u16]]
-      )),
-      newEntity("triangle2b", newMesh(
-        positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)],
-        colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
-        indices=[[0'u16, 2'u16, 1'u16]]
-      )),
-      newEntity("triangle3a", newMesh(
-        positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
-        colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)],
-        indices=[[0'u32, 2'u32, 1'u32]],
-        autoResize=false
-      )),
-      newEntity("triangle3b", newMesh(
-        positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
-        colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)],
-        indices=[[0'u32, 2'u32, 1'u32]],
-        autoResize=false
-      )),
-    )
+proc scene_different_mesh_types(): Entity =
+  result = newEntity("root",
+    newEntity("triangle1", newMesh(
+      positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
+      colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
+    )),
+    newEntity("triangle1b", newMesh(
+      positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)],
+      colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
+    )),
+    newEntity("triangle2a", newMesh(
+      positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)],
+      colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
+      indices=[[0'u16, 2'u16, 1'u16]]
+    )),
+    newEntity("triangle2b", newMesh(
+      positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)],
+      colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
+      indices=[[0'u16, 2'u16, 1'u16]]
+    )),
+    newEntity("triangle3a", newMesh(
+      positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
+      colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)],
+      indices=[[0'u32, 2'u32, 1'u32]],
+      autoResize=false
+    )),
+    newEntity("triangle3b", newMesh(
+      positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
+      colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)],
+      indices=[[0'u32, 2'u32, 1'u32]],
+      autoResize=false
+    )),
   )
+  for mesh in allComponentsOfType[Mesh](result):
+    mesh.setInstanceData("translate", @[newVec3f()])
 
-proc scene_simple(): Scene =
+proc scene_simple(): Entity =
   var mymesh1 = newMesh(
     positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)],
     colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
@@ -95,22 +94,16 @@
   mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)])
   mymesh3.setInstanceData("translate", @[newVec3f(-0.3, 0.0)])
   mymesh4.setInstanceData("translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)])
-  result = Scene(
-    name: "main",
-    root: newEntity("root", newEntity("triangle", mymesh4, mymesh3, mymesh2, mymesh1))
-  )
+  result = newEntity("root", newEntity("triangle", mymesh4, mymesh3, mymesh2, mymesh1))
 
-proc scene_primitives(): Scene =
+proc scene_primitives(): Entity =
   var r = rect(color="ff0000")
   var t = tri(color="0000ff")
   var c = circle(color="00ff00")
   setInstanceData[Vec3f](r, "translate", @[newVec3f(0.5, -0.3)])
   setInstanceData[Vec3f](t, "translate", @[newVec3f(0.3,  0.3)])
   setInstanceData[Vec3f](c, "translate", @[newVec3f(-0.3,  0.1)])
-  result = Scene(
-    name: "main",
-    root: newEntity("root", t, r, c)
-  )
+  result = newEntity("root", t, r, c)
 
 when isMainModule:
   var engine = initEngine("Test")
@@ -140,42 +133,29 @@
       main="color = vec4(outcolor, 1);"
     )
   var
-    vertexshader = engine.gpuDevice.createShaderModule(vertexCode)
-    fragmentshader = engine.gpuDevice.createShaderModule(fragmentCode)
     surfaceFormat = engine.gpuDevice.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
-    renderPass = engine.gpuDevice.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
-    (swapchain, res) = engine.gpuDevice.createSwapchain(renderPass, surfaceFormat, engine.gpuDevice.firstGraphicsQueue().get().family, 2)
-  if res != VK_SUCCESS:
-    raise newException(Exception, "Unable to create swapchain")
+    renderPass = engine.gpuDevice.simpleForwardRenderPass(surfaceFormat.format, vertexCode, fragmentCode, 2)
+    renderer = engine.gpuDevice.initRenderer([renderPass])
 
   # INIT SCENE
 
-  # var thescene = scene_simple()
-  # var thescene = scene_different_mesh_types()
-  var thescene = scene_primitives()
+  var scenes = [scene_simple(), scene_different_mesh_types(), scene_primitives()]
   var time = initShaderGlobal("time", 0.0'f32)
-  thescene.root.components.add time
-  thescene.setupDrawableBuffers(engine.gpuDevice, vertexInput)
+  for scene in scenes.mitems:
+    scene.components.add time
+    renderer.setupDrawableBuffers(scene, vertexInput)
 
   # MAINLOOP
   echo "Setup successfull, start rendering"
-  for i in 0 ..< 10000:
-    setValue[float32](time.value, get[float32](time.value) + 0.0005)
-    discard swapchain.drawScene(thescene)
-  echo "Rendered ", swapchain.framesRendered, " frames"
+  for i in 0 ..< 3:
+    for scene in scenes:
+      for i in 0 ..< 1000:
+        setValue[float32](time.value, get[float32](time.value) + 0.0005)
+        discard renderer.render(scene)
+  echo "Rendered ", renderer.framesRendered, " frames"
 
   # cleanup
   echo "Start cleanup"
   checkVkResult engine.gpuDevice.vk.vkDeviceWaitIdle()
-
-  # destroy scene
-  thescene.destroy()
-
-  # destroy renderer
-  vertexshader.destroy()
-  fragmentshader.destroy()
-  renderPass.destroy()
-  swapchain.destroy()
-
-  # destroy engine
+  renderer.destroy()
   engine.destroy()