diff tests/test_text.nim @ 1239:69489a678141

add: better syncing, better swapchain access, correct font offset, two font-rendering tests
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 00:46:10 +0700
parents 03634915bbdb
children 42eeb59f3a43
line wrap: on
line diff
--- a/tests/test_text.nim	Sun Jul 21 11:31:11 2024 +0700
+++ b/tests/test_text.nim	Mon Jul 22 00:46:10 2024 +0700
@@ -8,10 +8,10 @@
 
 import ../semiconginev2
 
-proc test_01_static_label(time: float32, swapchain: var Swapchain) =
+proc test_01_static_label(time: float32) =
   var renderdata = InitRenderData()
 
-  var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass)
+  var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass)
 
   var font = LoadFont("Overhaul.ttf", lineHeightPixels = 160)
   var label1 = InitTextbox(
@@ -25,21 +25,21 @@
 
   var start = getMonoTime()
   while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
-    label1.Refresh(swapchain.GetAspectRatio())
-    WithNextFrame(swapchain, framebuffer, commandbuffer):
-      WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
+    label1.Refresh()
+    WithNextFrame(framebuffer, commandbuffer):
+      WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
         WithPipeline(commandbuffer, pipeline):
-          Render(label1, commandbuffer, pipeline, swapchain.currentFiF)
+          Render(label1, commandbuffer, pipeline)
 
         # cleanup
   checkVkResult vkDeviceWaitIdle(vulkan.device)
   DestroyPipeline(pipeline)
   DestroyRenderData(renderdata)
 
-proc test_02_multiple_animated(time: float32, swapchain: var Swapchain) =
+proc test_02_multiple_animated(time: float32) =
   var renderdata = InitRenderData()
 
-  var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass)
+  var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass)
 
   var font1 = LoadFont("Overhaul.ttf", lineHeightPixels = 40)
   var font2 = LoadFont("Overhaul.ttf", lineHeightPixels = 160)
@@ -85,22 +85,81 @@
       labels[i].Scale = labels[i].Scale * (1.0 + (i + 1).float * 0.001)
       labels[i].Position = labels[i].Position + NewVec3f(0.001 * (i.float - 1'f))
       labels[i].text = $(p + i)
-      labels[i].Refresh(swapchain.GetAspectRatio())
+      labels[i].Refresh()
     inc p
-    WithNextFrame(swapchain, framebuffer, commandbuffer):
-      WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
+    WithNextFrame(framebuffer, commandbuffer):
+      WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
         WithPipeline(commandbuffer, pipeline):
           for label in labels:
-            Render(label, commandbuffer, pipeline, swapchain.currentFiF)
+            Render(label, commandbuffer, pipeline)
 
-        # cleanup
+      # cleanup
   checkVkResult vkDeviceWaitIdle(vulkan.device)
   DestroyPipeline(pipeline)
   DestroyRenderData(renderdata)
 
-proc test_03_layouting(time: float32, swapchain: var Swapchain) =
-  discard # TODO
-proc test_04_lots_of_texts(time: float32, swapchain: var Swapchain) =
+proc test_03_layouting(time: float32) =
+  var renderdata = InitRenderData()
+
+  var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass)
+
+  var font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 40)
+  var labels: seq[Textbox]
+
+  for horizontal in HorizontalAlignment:
+    labels.add InitTextbox(
+      renderdata,
+      pipeline.descriptorSetLayouts[0],
+      font,
+      $horizontal & " aligned",
+      color = NewVec4f(1, 1, 1, 1),
+      scale = 0.001,
+      position = NewVec3f(0, 0.9 - (horizontal.float * 0.15)),
+      horizontalAlignment = horizontal,
+    )
+  for vertical in VerticalAlignment:
+    labels.add InitTextbox(
+      renderdata,
+      pipeline.descriptorSetLayouts[0],
+      font,
+      $vertical & " aligned",
+      color = NewVec4f(1, 1, 1, 1),
+      scale = 0.001,
+      position = NewVec3f(-0.35 + (vertical.float * 0.35), 0.3),
+      verticalAlignment = vertical,
+    )
+  labels.add InitTextbox(
+    renderdata,
+    pipeline.descriptorSetLayouts[0],
+    font,
+    """Paragraph
+This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2.
+
+It should display with some space above and have a pleasing appearance overall! :)""",
+    maxWidth = 0.6,
+    color = NewVec4f(1, 1, 1, 1),
+    scale = 0.001,
+    position = NewVec3f(-0.9, 0.1),
+    verticalAlignment = Top,
+    horizontalAlignment = Left,
+  )
+
+
+  var start = getMonoTime()
+  while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
+    let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time
+    WithNextFrame(framebuffer, commandbuffer):
+      WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
+        WithPipeline(commandbuffer, pipeline):
+          for label in labels:
+            Render(label, commandbuffer, pipeline)
+
+      # cleanup
+  checkVkResult vkDeviceWaitIdle(vulkan.device)
+  DestroyPipeline(pipeline)
+  DestroyRenderData(renderdata)
+
+proc test_04_lots_of_texts(time: float32) =
   discard # TODO
 
 when isMainModule:
@@ -108,14 +167,15 @@
   InitVulkan()
 
   var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true)
-  var swapchain = InitSwapchain(renderpass = renderpass).get()
+  SetupSwapchain(renderpass = renderpass)
 
   # tests a simple triangle with minimalistic shader and vertex format
   # test_01_static_label(time, swapchain)
-  test_02_multiple_animated(time, swapchain)
+  # test_02_multiple_animated(time)
+  test_03_layouting(time)
+
 
   checkVkResult vkDeviceWaitIdle(vulkan.device)
   vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
-  DestroySwapchain(swapchain)
 
   DestroyVulkan()