changeset 1499:1f58458b7ef7 default tip

did: fix a few issues with tests
author sam <sam@basx.dev>
date Tue, 14 Oct 2025 00:27:28 +0700
parents d3d667bbdda4
children
files semicongine/rendering/renderer.nim tests/hello_triangle.nim tests/test_gltf.nim tests/test_rendering.nim tests/test_storage.nim
diffstat 5 files changed, 87 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/rendering/renderer.nim	Thu Sep 25 23:53:41 2025 +0700
+++ b/semicongine/rendering/renderer.nim	Tue Oct 14 00:27:28 2025 +0700
@@ -227,10 +227,29 @@
     index: static DescriptorSetIndex,
     pipeline: Pipeline[TShader],
 ) =
-  static:
-    assertCompatibleDescriptorSet(TDescriptorSet, TShader, index)
+  static: assertCompatibleDescriptorSet(TDescriptorSet, TShader, index)
   bindDescriptorSet(commandBuffer, descriptorSet, index, pipeline.layout)
 
+proc bindDescriptorSet*[TDescriptorSet](
+    commandBuffer: VkCommandBuffer,
+    descriptorSet: DescriptorSetData[TDescriptorSet],
+    index: static DescriptorSetIndex,
+    layout: VkPipelineLayout,
+    fif: int,
+) =
+  assert descriptorSet.vk[currentFiF()].Valid, "DescriptorSetData not initialized, maybe forgot to call initDescriptorSet"
+  svkCmdBindDescriptorSet(commandBuffer, descriptorSet.vk[fif], index, layout)
+
+proc bindDescriptorSet*[TDescriptorSet, TShader](
+    commandBuffer: VkCommandBuffer,
+    descriptorSet: DescriptorSetData[TDescriptorSet],
+    index: static DescriptorSetIndex,
+    pipeline: Pipeline[TShader],
+    fif: int,
+) =
+  static: assertCompatibleDescriptorSet(TDescriptorSet, TShader, index)
+  bindDescriptorSet(commandBuffer, descriptorSet, index, pipeline.layout, fif)
+
 proc assertCanRenderMesh(TShader, TMesh, TInstance: typedesc) =
   for attrName, attrValue in default(TShader).fieldPairs:
     when hasCustomPragma(attrValue, VertexAttribute):
--- a/tests/hello_triangle.nim	Thu Sep 25 23:53:41 2025 +0700
+++ b/tests/hello_triangle.nim	Tue Oct 14 00:27:28 2025 +0700
@@ -11,7 +11,7 @@
 )
 
 # the swapchain, needs to be attached to the main renderpass
-setupSwapchain(renderpass = renderpass)
+setupSwapchain(renderpass = renderpass, vSync=false, tripleBuffering=true)
 
 # render data is used for memory management on the GPU
 var renderdata = initRenderData()
--- a/tests/test_gltf.nim	Thu Sep 25 23:53:41 2025 +0700
+++ b/tests/test_gltf.nim	Tue Oct 14 00:27:28 2025 +0700
@@ -124,7 +124,8 @@
     )
   )
   for i in 0 ..< gltfData.materials.len:
-    descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer)
+    for j in 0 ..< INFLIGHTFRAMES.int:
+      descriptors.data[j].materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer)
   for mesh in mitems(gltfData.meshes):
     for primitive in mitems(mesh.primitives):
       primitive.data.color = asGPUArray(
@@ -191,12 +192,12 @@
     camPos += camDirSide * sideward * dt
 
     let view = rotate(-camPitch, X) * rotate(-camYaw, Y) * translate(-camPos)
-    descriptors.data.camera.data.view = view
-    descriptors.data.camera.data.normal = view
-    descriptors.data.camera.data.projection =
+    descriptors.data[currentFiF()].camera.data.view = view
+    descriptors.data[currentFiF()].camera.data.normal = view
+    descriptors.data[currentFiF()].camera.data.projection =
       projection(PI / 2, aspect = getAspectRatio(), zNear = 0.01, zFar = 20)
 
-    updateGPUBuffer(descriptors.data.camera)
+    updateGPUBuffer(descriptors.data[currentFiF()].camera, currentFiF())
 
     withNextFrame(framebuffer, commandbuffer):
       withRenderPass(
@@ -229,7 +230,7 @@
   var renderpass = createDirectPresentationRenderPass(
     depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT
   )
-  setupSwapchain(renderpass = renderpass)
+  setupSwapchain(renderpass = renderpass, vSync = false, tripleBuffering = true)
   lockMouse(true)
   # showSystemCursor(false)
 
--- a/tests/test_rendering.nim	Thu Sep 25 23:53:41 2025 +0700
+++ b/tests/test_rendering.nim	Tue Oct 14 00:27:28 2025 +0700
@@ -827,11 +827,8 @@
   destroyPipeline(pipeline)
   destroyRenderData(renderdata)
 
-proc test_09_triangle_2pass(
-    time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits
-) =
-  var (offscreenRP, presentRP) =
-    createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
+proc test_09_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) =
+  var (offscreenRP, presentRP) = createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
 
   setupSwapchain(renderpass = presentRP, vSync = false, tripleBuffering = false)
 
@@ -916,6 +913,7 @@
     depthImage: VkImage
     depthImageView: VkImageView
     depthMemory: VkDeviceMemory
+
   if offscreenRP.depthBuffer:
     depthImage = svkCreate2DImage(
       width = frameWidth(),
@@ -938,6 +936,7 @@
     msaaImage: VkImage
     msaaImageView: VkImageView
     msaaMemory: VkDeviceMemory
+
   if offscreenRP.samples != VK_SAMPLE_COUNT_1_BIT:
     msaaImage = svkCreate2DImage(
       width = frameWidth(),
@@ -952,45 +951,49 @@
     msaaImageView = svkCreate2DImageView(image = msaaImage, format = SURFACE_FORMAT)
 
   var attachments: seq[VkImageView]
-  if offscreenRP.samples == VK_SAMPLE_COUNT_1_BIT:
-    if offscreenRP.depthBuffer:
-      attachments = @[uniforms1.data[currentFiF()].frameTexture.imageview, depthImageView]
+  for j in 0 ..< INFLIGHTFRAMES:
+    if offscreenRP.samples == VK_SAMPLE_COUNT_1_BIT:
+      if offscreenRP.depthBuffer:
+        attachments = @[uniforms1.data[j].frameTexture.imageview, depthImageView]
+      else:
+        attachments = @[uniforms1.data[j].frameTexture.imageview]
     else:
-      attachments = @[uniforms1.data[currentFiF()].frameTexture.imageview]
-  else:
-    if offscreenRP.depthBuffer:
-      attachments = @[msaaImageView, depthImageView, uniforms1.data[currentFiF()].frameTexture.imageview]
-    else:
-      attachments = @[msaaImageView, uniforms1.data[currentFiF()].frameTexture.imageview]
+      if offscreenRP.depthBuffer:
+        attachments = @[msaaImageView, depthImageView, uniforms1.data[j].frameTexture.imageview]
+      else:
+        attachments = @[msaaImageView, uniforms1.data[j].frameTexture.imageview]
+
   var offscreenFB = svkCreateFramebuffer(offscreenRP.vk, frameWidth(), frameHeight(), attachments)
 
   renderdata.flushAllMemory()
 
   var start = getMonoTime()
   while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
-    withNextFrame(framebuffer, commandbuffer):
-      withRenderPass(
-        offscreenRP,
-        offscreenFB,
-        commandbuffer,
-        frameWidth(),
-        frameHeight(),
-        vec4(0, 0, 0, 0),
-      ):
-        withPipeline(commandbuffer, drawPipeline):
-          render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh)
-
-      withRenderPass(
-        presentRP,
-        framebuffer,
-        commandbuffer,
-        frameWidth(),
-        frameHeight(),
-        vec4(0, 0, 0, 0),
-      ):
-        withPipeline(commandbuffer, presentPipeline):
-          bindDescriptorSet(commandbuffer, uniforms1, 0, presentPipeline)
-          render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad)
+    discard updateInputs()
+    if keyWasPressed(Space):
+      withNextFrame(framebuffer, commandbuffer):
+        withRenderPass(
+          offscreenRP,
+          offscreenFB,
+          commandbuffer,
+          frameWidth(),
+          frameHeight(),
+          vec4(0.5, 0.5, 0.5, 1),
+        ):
+          withPipeline(commandbuffer, drawPipeline):
+            render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh)
+  
+        withRenderPass(
+          presentRP,
+          framebuffer,
+          commandbuffer,
+          frameWidth(),
+          frameHeight(),
+          vec4(1, 0, 1, 1),
+        ):
+          withPipeline(commandbuffer, presentPipeline):
+            bindDescriptorSet(commandbuffer, uniforms1, 0, presentPipeline)
+            render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad)
 
   # cleanup
   checkVkResult vkDeviceWaitIdle(engine().vulkan.device)
@@ -1056,7 +1059,8 @@
   ]#
 
   # test multiple render passes
-  for i, (depthBuffer, samples) in renderPasses:
-    test_09_triangle_2pass(time, depthBuffer, samples)
+  test_09_triangle_2pass(1000'f, true, VK_SAMPLE_COUNT_1_BIT)
+  # for i, (depthBuffer, samples) in renderPasses:
+    # test_09_triangle_2pass(time, depthBuffer, samples)
 
   destroyVulkan()
--- a/tests/test_storage.nim	Thu Sep 25 23:53:41 2025 +0700
+++ b/tests/test_storage.nim	Tue Oct 14 00:27:28 2025 +0700
@@ -1,4 +1,5 @@
 import std/strformat
+import std/sequtils
 
 import ../semicongine
 import ../semicongine/storage
@@ -34,8 +35,10 @@
 
   const obj1 = Obj1(value: 42, id: ID(1))
   "testWorld".storeWorld(obj1)
-  assert listWorlds() == @["testWorld"]
-  assert loadWorld[Obj1]("testWorld") == obj1
+  assert listWorlds()[0][1] == "testWorld"
+  var myWorld: Obj1
+  loadWorld[Obj1]("testWorld", myWorld)
+  assert myWorld == obj1
 
   const obj2 = Obj2(
     a: "Hello world",
@@ -47,16 +50,20 @@
     e: true,
   )
   "testWorld".storeWorld(obj2)
-  assert listWorlds() == @["testWorld"]
-  assert loadWorld[Obj2]("testWorld") == obj2
+  assert listWorlds()[0][1] == "testWorld"
+  var otherWorld: Obj2
+  loadWorld[Obj2]("testWorld", otherWorld)
+  assert otherWorld == obj2
 
   "earth".storeWorld(obj2)
-  assert "earth" in listWorlds()
-  assert "testWorld" in listWorlds()
-  assert loadWorld[Obj2]("earth") == obj2
+  assert "earth" in listWorlds().mapIt(it[1])
+  assert "testWorld" in listWorlds().mapIt(it[1])
+  var moreWorld: Obj2
+  loadWorld[Obj2]("earth", moreWorld)
+  assert moreWorld == obj2
 
   "earth".purgeWorld()
-  assert listWorlds() == @["testWorld"]
+  assert listWorlds()[0][1] == "testWorld"
 
   "testWorld".purgeWorld()
   assert listWorlds().len == 0