annotate tests/test_text.nim @ 1331:1abdd42f5cfe

add: image layers
author sam <sam@basx.dev>
date Thu, 22 Aug 2024 18:31:03 +0700
parents ad09d41abd1e
children df3c075e5dea
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
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
2 import std/algorithm
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
3 import std/strutils
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
4 import std/sequtils
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
5 import std/monotimes
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
6 import std/times
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
7 import std/options
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
8 import std/random
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
9
1267
4cf9872f7bb6 did: rename back to old name (no v2)
sam <sam@basx.dev>
parents: 1241
diff changeset
10 import ../semicongine
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
11
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
12 type
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
13 FontDS = object
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
14 fontAtlas: Image[Gray]
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
15
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
16 proc test_01_static_label(time: float32) =
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
17 var font = loadFont("Overhaul.ttf", lineHeightPixels = 160)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
18 var renderdata = initRenderData()
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
19 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
20
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
21 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas))
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
22 uploadImages(renderdata, ds)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
23 initDescriptorSet(
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
24 renderdata,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
25 pipeline.layout(0),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
26 ds,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
27 )
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
28
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
29 var label1 = initTextbox(
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
30 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
31 pipeline.layout(0),
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
32 font,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
33 0.0005,
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
34 "Hello semicongine!",
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
35 )
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
36
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
37 var start = getMonoTime()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
38 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
39 label1.refresh()
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
40 withNextFrame(framebuffer, commandbuffer):
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
41 bindDescriptorSet(commandbuffer, ds, 0, pipeline)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
42 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
43 withPipeline(commandbuffer, pipeline):
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
44 render(commandbuffer, pipeline, label1, vec3(), vec4(1, 1, 1, 1))
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
45
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
46 # cleanup
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
47 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
48 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
49 destroyRenderData(renderdata)
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
50
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
51 proc test_02_multiple_animated(time: float32) =
1282
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
52 var font1 = loadFont("Overhaul.ttf", lineHeightPixels = 40)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
53 var font2 = loadFont("Overhaul.ttf", lineHeightPixels = 160)
3308b88e53a6 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
54 var font3 = loadFont("DejaVuSans.ttf", lineHeightPixels = 160)
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
55 var renderdata = initRenderData()
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
56
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
57 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
58
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
59 var ds1 = asDescriptorSetData(FontDS(fontAtlas: font1.fontAtlas))
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
60 uploadImages(renderdata, ds1)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
61 initDescriptorSet(renderdata, pipeline.layout(0), ds1)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
62
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
63 var ds2 = asDescriptorSetData(FontDS(fontAtlas: font2.fontAtlas))
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
64 uploadImages(renderdata, ds2)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
65 initDescriptorSet(renderdata, pipeline.layout(0), ds2)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
66
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
67 var ds3 = asDescriptorSetData(FontDS(fontAtlas: font3.fontAtlas))
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
68 uploadImages(renderdata, ds3)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
69 initDescriptorSet(renderdata, pipeline.layout(0), ds3)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
70
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
71 var labels = [
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
72 initTextbox(
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
73 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
74 pipeline.layout(0),
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
75 font1,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
76 0.004,
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
77 " 0",
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
78 ),
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
79 initTextbox(
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
80 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
81 pipeline.layout(0),
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
82 font2,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
83 0.001,
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
84 " 1",
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
85 ),
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
86 initTextbox(
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
87 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
88 pipeline.layout(0),
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
89 font3,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
90 0.001,
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
91 " 2",
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
92 )
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
93 ]
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
94
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
95 var start = getMonoTime()
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
96 var p = 0
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
97 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
98 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
99 for i in 0 ..< labels.len:
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
100 labels[i].text = $(p + i)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
101 labels[i].refresh()
1238
03634915bbdb add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents: 1237
diff changeset
102 inc p
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
103 withNextFrame(framebuffer, commandbuffer):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
104 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
105 withPipeline(commandbuffer, pipeline):
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
106 bindDescriptorSet(commandbuffer, ds1, 0, pipeline)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
107 render(
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
108 commandbuffer,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
109 pipeline,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
110 labels[0],
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
111 position=vec3(0 / labels.len, 0.1 + progress * 0.5),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
112 color=vec4(1, 1, 1, 1),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
113 )
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
114 bindDescriptorSet(commandbuffer, ds2, 0, pipeline)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
115 render(
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
116 commandbuffer,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
117 pipeline,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
118 labels[1],
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
119 position=vec3(1 / labels.len, 0.1 + progress * 0.5),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
120 color=vec4(1, 1, 1, 1),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
121 )
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
122 bindDescriptorSet(commandbuffer, ds3, 0, pipeline)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
123 render(
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
124 commandbuffer,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
125 pipeline,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
126 labels[2],
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
127 position=vec3(2 / labels.len, 0.1 + progress * 0.5),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
128 color=vec4(1, 1, 1, 1),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
129 )
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
130
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
131 # cleanup
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
132 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
133 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
134 destroyRenderData(renderdata)
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
135
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
136 proc test_03_layouting(time: float32) =
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
137 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 40)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
138 var renderdata = initRenderData()
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
139
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
140 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
141
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
142 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas))
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
143 uploadImages(renderdata, ds)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
144 initDescriptorSet(
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
145 renderdata,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
146 pipeline.layout(0),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
147 ds,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
148 )
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
149
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
150 var labels: seq[Textbox]
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
151
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
152 for horizontal in HorizontalAlignment:
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
153 labels.add initTextbox(
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
154 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
155 pipeline.layout(0),
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
156 font,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
157 0.001,
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
158 $horizontal & " aligned",
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
159 horizontalAlignment = horizontal,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
160 )
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
161 for vertical in VerticalAlignment:
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
162 labels.add initTextbox(
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
163 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
164 pipeline.layout(0),
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
165 font,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
166 0.001,
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
167 $vertical & " aligned",
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
168 verticalAlignment = vertical,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
169 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
170 labels.add initTextbox(
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
171 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
172 pipeline.layout(0),
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
173 font,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
174 0.001,
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
175 """Paragraph
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
176 This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2.
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
177
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
178 It should display with some space above and have a pleasing appearance overall! :)""",
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
179 maxWidth = 0.6,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
180 verticalAlignment = Top,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
181 horizontalAlignment = Left,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
182 )
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
183
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
184 var start = getMonoTime()
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
185 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
186 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
187 withNextFrame(framebuffer, commandbuffer):
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
188 bindDescriptorSet(commandbuffer, ds, 0, pipeline)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
189 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
190 withPipeline(commandbuffer, pipeline):
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
191 for i in 0 ..< labels.len:
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
192 render(commandbuffer, pipeline, labels[i], vec3(0.5 - i.float32 * 0.1, 0.5 - i.float32 * 0.1), vec4(1, 1, 1, 1))
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
193
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
194 # cleanup
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
195 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
196 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
197 destroyRenderData(renderdata)
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
198
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
199 proc test_04_lots_of_texts(time: float32) =
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
200 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 160)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
201 var renderdata = initRenderData()
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
202
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
203 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
204
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
205 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas))
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
206 uploadImages(renderdata, ds)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
207 initDescriptorSet(
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
208 renderdata,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
209 pipeline.layout(0),
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
210 ds,
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
211 )
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
212
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
213 var labels: seq[Textbox]
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
214 var positions = newSeq[Vec3f](100)
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
215 var colors = newSeq[Vec4f](100)
1329
ad09d41abd1e add: 2d-scaling for text
sam <sam@basx.dev>
parents: 1327
diff changeset
216 var scales = newSeq[Vec2f](100)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
217 for i in 0 ..< 100:
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
218 positions[i] = vec3(rand(-0.5 .. 0.5), rand(-0.5 .. 0.5), rand(-0.1 .. 0.1))
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
219 colors[i] = vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0))
1329
ad09d41abd1e add: 2d-scaling for text
sam <sam@basx.dev>
parents: 1327
diff changeset
220 scales[i] = vec2(rand(0.5'f32 .. 1.5'f32), rand(0.5'f32 .. 1.5'f32))
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
221 labels.add initTextbox(
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
222 renderdata,
1292
5de466f5f087 add: color utils and textbox refactoring
sam <sam@basx.dev>
parents: 1283
diff changeset
223 pipeline.layout(0),
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
224 font,
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
225 0.001,
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
226 $i,
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
227 )
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
228
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
229 var start = getMonoTime()
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
230 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
231 for l in labels.mitems:
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
232 l.refresh()
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
233 withNextFrame(framebuffer, commandbuffer):
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
234 bindDescriptorSet(commandbuffer, ds, 0, pipeline)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
235 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
236 withPipeline(commandbuffer, pipeline):
1327
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
237 for i in 0 ..< labels.len:
373a4888f6ac did: rework font-rendering
sam <sam@basx.dev>
parents: 1321
diff changeset
238 render(commandbuffer, pipeline, labels[i], positions[i], colors[i], scales[i])
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
239
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
240 # cleanup
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
241 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
242 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
243 destroyRenderData(renderdata)
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
244
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
245 when isMainModule:
1241
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
246 var time = 1'f32
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
247 initVulkan()
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
248
1241
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
249 for depthBuffer in [true, false]:
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
250 var renderpass = createDirectPresentationRenderPass(depthBuffer = depthBuffer)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
251 setupSwapchain(renderpass = renderpass)
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
252
1241
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
253 # tests a simple triangle with minimalistic shader and vertex format
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
254 test_01_static_label(time)
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
255 test_02_multiple_animated(time)
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
256 test_03_layouting(time)
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
257 test_04_lots_of_texts(time)
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1238
diff changeset
258
1241
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
259 checkVkResult vkDeviceWaitIdle(vulkan.device)
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1240
diff changeset
260 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
261 clearSwapchain()
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
262
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1282
diff changeset
263 destroyVulkan()