annotate tests/test_text.nim @ 1341:f65f25233127

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