Mercurial > games > semicongine
comparison tests/test_text.nim @ 1332:df3c075e5dea
did: formatting
author | sam <sam@basx.dev> |
---|---|
date | Thu, 22 Aug 2024 18:31:59 +0700 |
parents | ad09d41abd1e |
children | f2ff6f624932 |
comparison
equal
deleted
inserted
replaced
1331:1abdd42f5cfe | 1332:df3c075e5dea |
---|---|
7 import std/options | 7 import std/options |
8 import std/random | 8 import std/random |
9 | 9 |
10 import ../semicongine | 10 import ../semicongine |
11 | 11 |
12 type | 12 type FontDS = object |
13 FontDS = object | 13 fontAtlas: Image[Gray] |
14 fontAtlas: Image[Gray] | |
15 | 14 |
16 proc test_01_static_label(time: float32) = | 15 proc test_01_static_label(time: float32) = |
17 var font = loadFont("Overhaul.ttf", lineHeightPixels = 160) | 16 var font = loadFont("Overhaul.ttf", lineHeightPixels = 160) |
18 var renderdata = initRenderData() | 17 var renderdata = initRenderData() |
19 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | 18 var pipeline = |
19 createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | |
20 | 20 |
21 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) | 21 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) |
22 uploadImages(renderdata, ds) | 22 uploadImages(renderdata, ds) |
23 initDescriptorSet( | 23 initDescriptorSet(renderdata, pipeline.layout(0), ds) |
24 renderdata, | 24 |
25 pipeline.layout(0), | 25 var label1 = |
26 ds, | 26 initTextbox(renderdata, pipeline.layout(0), font, 0.0005, "Hello semicongine!") |
27 ) | |
28 | |
29 var label1 = initTextbox( | |
30 renderdata, | |
31 pipeline.layout(0), | |
32 font, | |
33 0.0005, | |
34 "Hello semicongine!", | |
35 ) | |
36 | 27 |
37 var start = getMonoTime() | 28 var start = getMonoTime() |
38 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 29 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
39 label1.refresh() | 30 label1.refresh() |
40 withNextFrame(framebuffer, commandbuffer): | 31 withNextFrame(framebuffer, commandbuffer): |
41 bindDescriptorSet(commandbuffer, ds, 0, pipeline) | 32 bindDescriptorSet(commandbuffer, ds, 0, pipeline) |
42 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 33 withRenderPass( |
34 vulkan.swapchain.renderPass, | |
35 framebuffer, | |
36 commandbuffer, | |
37 vulkan.swapchain.width, | |
38 vulkan.swapchain.height, | |
39 vec4(0, 0, 0, 0), | |
40 ): | |
43 withPipeline(commandbuffer, pipeline): | 41 withPipeline(commandbuffer, pipeline): |
44 render(commandbuffer, pipeline, label1, vec3(), vec4(1, 1, 1, 1)) | 42 render(commandbuffer, pipeline, label1, vec3(), vec4(1, 1, 1, 1)) |
45 | 43 |
46 # cleanup | 44 # cleanup |
47 checkVkResult vkDeviceWaitIdle(vulkan.device) | 45 checkVkResult vkDeviceWaitIdle(vulkan.device) |
52 var font1 = loadFont("Overhaul.ttf", lineHeightPixels = 40) | 50 var font1 = loadFont("Overhaul.ttf", lineHeightPixels = 40) |
53 var font2 = loadFont("Overhaul.ttf", lineHeightPixels = 160) | 51 var font2 = loadFont("Overhaul.ttf", lineHeightPixels = 160) |
54 var font3 = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) | 52 var font3 = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) |
55 var renderdata = initRenderData() | 53 var renderdata = initRenderData() |
56 | 54 |
57 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | 55 var pipeline = |
56 createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | |
58 | 57 |
59 var ds1 = asDescriptorSetData(FontDS(fontAtlas: font1.fontAtlas)) | 58 var ds1 = asDescriptorSetData(FontDS(fontAtlas: font1.fontAtlas)) |
60 uploadImages(renderdata, ds1) | 59 uploadImages(renderdata, ds1) |
61 initDescriptorSet(renderdata, pipeline.layout(0), ds1) | 60 initDescriptorSet(renderdata, pipeline.layout(0), ds1) |
62 | 61 |
67 var ds3 = asDescriptorSetData(FontDS(fontAtlas: font3.fontAtlas)) | 66 var ds3 = asDescriptorSetData(FontDS(fontAtlas: font3.fontAtlas)) |
68 uploadImages(renderdata, ds3) | 67 uploadImages(renderdata, ds3) |
69 initDescriptorSet(renderdata, pipeline.layout(0), ds3) | 68 initDescriptorSet(renderdata, pipeline.layout(0), ds3) |
70 | 69 |
71 var labels = [ | 70 var labels = [ |
72 initTextbox( | 71 initTextbox(renderdata, pipeline.layout(0), font1, 0.004, " 0"), |
73 renderdata, | 72 initTextbox(renderdata, pipeline.layout(0), font2, 0.001, " 1"), |
74 pipeline.layout(0), | 73 initTextbox(renderdata, pipeline.layout(0), font3, 0.001, " 2"), |
75 font1, | |
76 0.004, | |
77 " 0", | |
78 ), | |
79 initTextbox( | |
80 renderdata, | |
81 pipeline.layout(0), | |
82 font2, | |
83 0.001, | |
84 " 1", | |
85 ), | |
86 initTextbox( | |
87 renderdata, | |
88 pipeline.layout(0), | |
89 font3, | |
90 0.001, | |
91 " 2", | |
92 ) | |
93 ] | 74 ] |
94 | 75 |
95 var start = getMonoTime() | 76 var start = getMonoTime() |
96 var p = 0 | 77 var p = 0 |
97 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 78 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
99 for i in 0 ..< labels.len: | 80 for i in 0 ..< labels.len: |
100 labels[i].text = $(p + i) | 81 labels[i].text = $(p + i) |
101 labels[i].refresh() | 82 labels[i].refresh() |
102 inc p | 83 inc p |
103 withNextFrame(framebuffer, commandbuffer): | 84 withNextFrame(framebuffer, commandbuffer): |
104 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 85 withRenderPass( |
86 vulkan.swapchain.renderPass, | |
87 framebuffer, | |
88 commandbuffer, | |
89 vulkan.swapchain.width, | |
90 vulkan.swapchain.height, | |
91 vec4(0, 0, 0, 0), | |
92 ): | |
105 withPipeline(commandbuffer, pipeline): | 93 withPipeline(commandbuffer, pipeline): |
106 bindDescriptorSet(commandbuffer, ds1, 0, pipeline) | 94 bindDescriptorSet(commandbuffer, ds1, 0, pipeline) |
107 render( | 95 render( |
108 commandbuffer, | 96 commandbuffer, |
109 pipeline, | 97 pipeline, |
110 labels[0], | 98 labels[0], |
111 position=vec3(0 / labels.len, 0.1 + progress * 0.5), | 99 position = vec3(0 / labels.len, 0.1 + progress * 0.5), |
112 color=vec4(1, 1, 1, 1), | 100 color = vec4(1, 1, 1, 1), |
113 ) | 101 ) |
114 bindDescriptorSet(commandbuffer, ds2, 0, pipeline) | 102 bindDescriptorSet(commandbuffer, ds2, 0, pipeline) |
115 render( | 103 render( |
116 commandbuffer, | 104 commandbuffer, |
117 pipeline, | 105 pipeline, |
118 labels[1], | 106 labels[1], |
119 position=vec3(1 / labels.len, 0.1 + progress * 0.5), | 107 position = vec3(1 / labels.len, 0.1 + progress * 0.5), |
120 color=vec4(1, 1, 1, 1), | 108 color = vec4(1, 1, 1, 1), |
121 ) | 109 ) |
122 bindDescriptorSet(commandbuffer, ds3, 0, pipeline) | 110 bindDescriptorSet(commandbuffer, ds3, 0, pipeline) |
123 render( | 111 render( |
124 commandbuffer, | 112 commandbuffer, |
125 pipeline, | 113 pipeline, |
126 labels[2], | 114 labels[2], |
127 position=vec3(2 / labels.len, 0.1 + progress * 0.5), | 115 position = vec3(2 / labels.len, 0.1 + progress * 0.5), |
128 color=vec4(1, 1, 1, 1), | 116 color = vec4(1, 1, 1, 1), |
129 ) | 117 ) |
130 | 118 |
131 # cleanup | 119 # cleanup |
132 checkVkResult vkDeviceWaitIdle(vulkan.device) | 120 checkVkResult vkDeviceWaitIdle(vulkan.device) |
133 destroyPipeline(pipeline) | 121 destroyPipeline(pipeline) |
135 | 123 |
136 proc test_03_layouting(time: float32) = | 124 proc test_03_layouting(time: float32) = |
137 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 40) | 125 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 40) |
138 var renderdata = initRenderData() | 126 var renderdata = initRenderData() |
139 | 127 |
140 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | 128 var pipeline = |
129 createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | |
141 | 130 |
142 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) | 131 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) |
143 uploadImages(renderdata, ds) | 132 uploadImages(renderdata, ds) |
144 initDescriptorSet( | 133 initDescriptorSet(renderdata, pipeline.layout(0), ds) |
145 renderdata, | |
146 pipeline.layout(0), | |
147 ds, | |
148 ) | |
149 | 134 |
150 var labels: seq[Textbox] | 135 var labels: seq[Textbox] |
151 | 136 |
152 for horizontal in HorizontalAlignment: | 137 for horizontal in HorizontalAlignment: |
153 labels.add initTextbox( | 138 labels.add initTextbox( |
184 var start = getMonoTime() | 169 var start = getMonoTime() |
185 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 170 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
186 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 171 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
187 withNextFrame(framebuffer, commandbuffer): | 172 withNextFrame(framebuffer, commandbuffer): |
188 bindDescriptorSet(commandbuffer, ds, 0, pipeline) | 173 bindDescriptorSet(commandbuffer, ds, 0, pipeline) |
189 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 174 withRenderPass( |
175 vulkan.swapchain.renderPass, | |
176 framebuffer, | |
177 commandbuffer, | |
178 vulkan.swapchain.width, | |
179 vulkan.swapchain.height, | |
180 vec4(0, 0, 0, 0), | |
181 ): | |
190 withPipeline(commandbuffer, pipeline): | 182 withPipeline(commandbuffer, pipeline): |
191 for i in 0 ..< labels.len: | 183 for i in 0 ..< labels.len: |
192 render(commandbuffer, pipeline, labels[i], vec3(0.5 - i.float32 * 0.1, 0.5 - i.float32 * 0.1), vec4(1, 1, 1, 1)) | 184 render( |
185 commandbuffer, | |
186 pipeline, | |
187 labels[i], | |
188 vec3(0.5 - i.float32 * 0.1, 0.5 - i.float32 * 0.1), | |
189 vec4(1, 1, 1, 1), | |
190 ) | |
193 | 191 |
194 # cleanup | 192 # cleanup |
195 checkVkResult vkDeviceWaitIdle(vulkan.device) | 193 checkVkResult vkDeviceWaitIdle(vulkan.device) |
196 destroyPipeline(pipeline) | 194 destroyPipeline(pipeline) |
197 destroyRenderData(renderdata) | 195 destroyRenderData(renderdata) |
198 | 196 |
199 proc test_04_lots_of_texts(time: float32) = | 197 proc test_04_lots_of_texts(time: float32) = |
200 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) | 198 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) |
201 var renderdata = initRenderData() | 199 var renderdata = initRenderData() |
202 | 200 |
203 var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | 201 var pipeline = |
202 createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) | |
204 | 203 |
205 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) | 204 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) |
206 uploadImages(renderdata, ds) | 205 uploadImages(renderdata, ds) |
207 initDescriptorSet( | 206 initDescriptorSet(renderdata, pipeline.layout(0), ds) |
208 renderdata, | |
209 pipeline.layout(0), | |
210 ds, | |
211 ) | |
212 | 207 |
213 var labels: seq[Textbox] | 208 var labels: seq[Textbox] |
214 var positions = newSeq[Vec3f](100) | 209 var positions = newSeq[Vec3f](100) |
215 var colors = newSeq[Vec4f](100) | 210 var colors = newSeq[Vec4f](100) |
216 var scales = newSeq[Vec2f](100) | 211 var scales = newSeq[Vec2f](100) |
217 for i in 0 ..< 100: | 212 for i in 0 ..< 100: |
218 positions[i] = vec3(rand(-0.5 .. 0.5), rand(-0.5 .. 0.5), rand(-0.1 .. 0.1)) | 213 positions[i] = vec3(rand(-0.5 .. 0.5), rand(-0.5 .. 0.5), rand(-0.1 .. 0.1)) |
219 colors[i] = vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0)) | 214 colors[i] = |
215 vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0)) | |
220 scales[i] = vec2(rand(0.5'f32 .. 1.5'f32), rand(0.5'f32 .. 1.5'f32)) | 216 scales[i] = vec2(rand(0.5'f32 .. 1.5'f32), rand(0.5'f32 .. 1.5'f32)) |
221 labels.add initTextbox( | 217 labels.add initTextbox(renderdata, pipeline.layout(0), font, 0.001, $i) |
222 renderdata, | |
223 pipeline.layout(0), | |
224 font, | |
225 0.001, | |
226 $i, | |
227 ) | |
228 | 218 |
229 var start = getMonoTime() | 219 var start = getMonoTime() |
230 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 220 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
231 for l in labels.mitems: | 221 for l in labels.mitems: |
232 l.refresh() | 222 l.refresh() |
233 withNextFrame(framebuffer, commandbuffer): | 223 withNextFrame(framebuffer, commandbuffer): |
234 bindDescriptorSet(commandbuffer, ds, 0, pipeline) | 224 bindDescriptorSet(commandbuffer, ds, 0, pipeline) |
235 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 225 withRenderPass( |
226 vulkan.swapchain.renderPass, | |
227 framebuffer, | |
228 commandbuffer, | |
229 vulkan.swapchain.width, | |
230 vulkan.swapchain.height, | |
231 vec4(0, 0, 0, 0), | |
232 ): | |
236 withPipeline(commandbuffer, pipeline): | 233 withPipeline(commandbuffer, pipeline): |
237 for i in 0 ..< labels.len: | 234 for i in 0 ..< labels.len: |
238 render(commandbuffer, pipeline, labels[i], positions[i], colors[i], scales[i]) | 235 render( |
236 commandbuffer, pipeline, labels[i], positions[i], colors[i], scales[i] | |
237 ) | |
239 | 238 |
240 # cleanup | 239 # cleanup |
241 checkVkResult vkDeviceWaitIdle(vulkan.device) | 240 checkVkResult vkDeviceWaitIdle(vulkan.device) |
242 destroyPipeline(pipeline) | 241 destroyPipeline(pipeline) |
243 destroyRenderData(renderdata) | 242 destroyRenderData(renderdata) |