annotate tests/test_text.nim @ 1238:03634915bbdb

add: tests for updating texts, does not pass validation atm
author sam <sam@basx.dev>
date Sun, 21 Jul 2024 11:31:11 +0700
parents 97813ac43cfb
children 69489a678141
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
2 import std/strutils
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
3 import std/sequtils
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
4 import std/monotimes
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
5 import std/times
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
6 import std/options
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
7 import std/random
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
8
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
9 import ../semiconginev2
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
10
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
11 proc test_01_static_label(time: float32, swapchain: var Swapchain) =
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
12 var renderdata = InitRenderData()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
13
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
14 var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
15
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
16 var font = LoadFont("Overhaul.ttf", lineHeightPixels = 160)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
17 var label1 = InitTextbox(
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
18 renderdata,
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
19 pipeline.descriptorSetLayouts[0],
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
20 font,
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
21 "Hello semicongine!",
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
22 color = NewVec4f(1, 1, 1, 1),
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
23 scale = 0.0005,
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
24 )
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
25
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
26 var start = getMonoTime()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
27 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
28 label1.Refresh(swapchain.GetAspectRatio())
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
29 WithNextFrame(swapchain, framebuffer, commandbuffer):
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
30 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
31 WithPipeline(commandbuffer, pipeline):
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
32 Render(label1, commandbuffer, pipeline, swapchain.currentFiF)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
33
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
34 # cleanup
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
35 checkVkResult vkDeviceWaitIdle(vulkan.device)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
36 DestroyPipeline(pipeline)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
37 DestroyRenderData(renderdata)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
38
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
39 proc test_02_multiple_animated(time: float32, swapchain: var Swapchain) =
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
40 var renderdata = InitRenderData()
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
41
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
42 var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
43
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
44 var font1 = LoadFont("Overhaul.ttf", lineHeightPixels = 40)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
45 var font2 = LoadFont("Overhaul.ttf", lineHeightPixels = 160)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
46 var font3 = LoadFont("DejaVuSans.ttf", lineHeightPixels = 160)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
47 var labels = [
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
48 InitTextbox(
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
49 renderdata,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
50 pipeline.descriptorSetLayouts[0],
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
51 font1,
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
52 " 0",
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
53 color = NewVec4f(0, 1, 1, 1),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
54 scale = 0.004,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
55 position = NewVec3f(-0.3, 0.5)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
56 ),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
57 InitTextbox(
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
58 renderdata,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
59 pipeline.descriptorSetLayouts[0],
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
60 font2,
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
61 " 1",
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
62 color = NewVec4f(1, 0, 1, 1),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
63 scale = 0.001,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
64 position = NewVec3f(0, 0)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
65 ),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
66 InitTextbox(
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
67 renderdata,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
68 pipeline.descriptorSetLayouts[0],
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
69 font3,
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
70 " 2",
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
71 color = NewVec4f(1, 1, 0, 1),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
72 scale = 0.001,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
73 position = NewVec3f(0.3, -0.5)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
74 )
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
75 ]
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
76
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
77 var start = getMonoTime()
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
78 var p = 0
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
79 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
80 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
81 for i in 0 ..< labels.len:
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
82 var c = labels[i].Color
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
83 c[i] = progress
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
84 labels[i].Color = c
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
85 labels[i].Scale = labels[i].Scale * (1.0 + (i + 1).float * 0.001)
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
86 labels[i].Position = labels[i].Position + NewVec3f(0.001 * (i.float - 1'f))
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
87 labels[i].text = $(p + i)
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
88 labels[i].Refresh(swapchain.GetAspectRatio())
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
89 inc p
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
90 WithNextFrame(swapchain, framebuffer, commandbuffer):
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
91 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
92 WithPipeline(commandbuffer, pipeline):
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
93 for label in labels:
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
94 Render(label, commandbuffer, pipeline, swapchain.currentFiF)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
95
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
96 # cleanup
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
97 checkVkResult vkDeviceWaitIdle(vulkan.device)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
98 DestroyPipeline(pipeline)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
99 DestroyRenderData(renderdata)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
100
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
101 proc test_03_layouting(time: float32, swapchain: var Swapchain) =
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
102 discard # TODO
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
103 proc test_04_lots_of_texts(time: float32, swapchain: var Swapchain) =
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
104 discard # TODO
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
105
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
106 when isMainModule:
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
107 var time = 10'f32
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
108 InitVulkan()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
109
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
110 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
111 var swapchain = InitSwapchain(renderpass = renderpass).get()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
112
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
113 # tests a simple triangle with minimalistic shader and vertex format
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
114 # test_01_static_label(time, swapchain)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
115 test_02_multiple_animated(time, swapchain)
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
116
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
117 checkVkResult vkDeviceWaitIdle(vulkan.device)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
118 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
119 DestroySwapchain(swapchain)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
120
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
121 DestroyVulkan()