Mercurial > games > semicongine
comparison tests/test_rendering.nim @ 1332:df3c075e5dea
did: formatting
author | sam <sam@basx.dev> |
---|---|
date | Thu, 22 Aug 2024 18:31:59 +0700 |
parents | 4a1c2b1128bc |
children | d5a6f69dc855 |
comparison
equal
deleted
inserted
replaced
1331:1abdd42f5cfe | 1332:df3c075e5dea |
---|---|
12 var renderdata = initRenderData() | 12 var renderdata = initRenderData() |
13 | 13 |
14 type | 14 type |
15 PushConstant = object | 15 PushConstant = object |
16 scale: float32 | 16 scale: float32 |
17 | |
17 Shader = object | 18 Shader = object |
18 position {.VertexAttribute.}: Vec3f | 19 position {.VertexAttribute.}: Vec3f |
19 color {.VertexAttribute.}: Vec3f | 20 color {.VertexAttribute.}: Vec3f |
20 pushConstant {.PushConstant.}: PushConstant | 21 pushConstant {.PushConstant.}: PushConstant |
21 fragmentColor {.Pass.}: Vec3f | 22 fragmentColor {.Pass.}: Vec3f |
22 outColor {.ShaderOutput.}: Vec4f | 23 outColor {.ShaderOutput.}: Vec4f |
23 # code | 24 # code |
24 vertexCode: string = """void main() { | 25 vertexCode: string = |
26 """void main() { | |
25 fragmentColor = color; | 27 fragmentColor = color; |
26 gl_Position = vec4(position * pushConstant.scale, 1);}""" | 28 gl_Position = vec4(position * pushConstant.scale, 1);}""" |
27 fragmentCode: string = """void main() { | 29 fragmentCode: string = |
30 """void main() { | |
28 outColor = vec4(fragmentColor, 1);}""" | 31 outColor = vec4(fragmentColor, 1);}""" |
32 | |
29 TriangleMesh = object | 33 TriangleMesh = object |
30 position: GPUArray[Vec3f, VertexBuffer] | 34 position: GPUArray[Vec3f, VertexBuffer] |
31 color: GPUArray[Vec3f, VertexBuffer] | 35 color: GPUArray[Vec3f, VertexBuffer] |
36 | |
32 var mesh = TriangleMesh( | 37 var mesh = TriangleMesh( |
33 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), | 38 position: asGPUArray( |
39 [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer | |
40 ), | |
34 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), | 41 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), |
35 ) | 42 ) |
36 assignBuffers(renderdata, mesh) | 43 assignBuffers(renderdata, mesh) |
37 renderdata.flushAllMemory() | 44 renderdata.flushAllMemory() |
38 | 45 |
39 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass) | 46 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass) |
40 | 47 |
41 var start = getMonoTime() | 48 var start = getMonoTime() |
42 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 49 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
43 | |
44 withNextFrame(framebuffer, commandbuffer): | 50 withNextFrame(framebuffer, commandbuffer): |
45 | 51 withRenderPass( |
46 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 52 vulkan.swapchain.renderPass, |
47 | 53 framebuffer, |
54 commandbuffer, | |
55 vulkan.swapchain.width, | |
56 vulkan.swapchain.height, | |
57 vec4(0, 0, 0, 0), | |
58 ): | |
48 withPipeline(commandbuffer, pipeline): | 59 withPipeline(commandbuffer, pipeline): |
49 | 60 renderWithPushConstant( |
50 renderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh, pushConstant = PushConstant(scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000))) | 61 commandbuffer = commandbuffer, |
62 pipeline = pipeline, | |
63 mesh = mesh, | |
64 pushConstant = PushConstant( | |
65 scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000) | |
66 ), | |
67 ) | |
51 | 68 |
52 # cleanup | 69 # cleanup |
53 checkVkResult vkDeviceWaitIdle(vulkan.device) | 70 checkVkResult vkDeviceWaitIdle(vulkan.device) |
54 destroyPipeline(pipeline) | 71 destroyPipeline(pipeline) |
55 destroyRenderData(renderdata) | 72 destroyRenderData(renderdata) |
56 | |
57 | 73 |
58 proc test_02_triangle_quad_instanced(time: float32) = | 74 proc test_02_triangle_quad_instanced(time: float32) = |
59 var renderdata = initRenderData() | 75 var renderdata = initRenderData() |
60 | 76 |
61 type | 77 type |
65 pos {.InstanceAttribute.}: Vec3f | 81 pos {.InstanceAttribute.}: Vec3f |
66 scale {.InstanceAttribute.}: float32 | 82 scale {.InstanceAttribute.}: float32 |
67 fragmentColor {.Pass.}: Vec3f | 83 fragmentColor {.Pass.}: Vec3f |
68 outColor {.ShaderOutput.}: Vec4f | 84 outColor {.ShaderOutput.}: Vec4f |
69 # code | 85 # code |
70 vertexCode: string = """void main() { | 86 vertexCode: string = |
87 """void main() { | |
71 fragmentColor = color; | 88 fragmentColor = color; |
72 gl_Position = vec4((position * scale) + pos, 1);}""" | 89 gl_Position = vec4((position * scale) + pos, 1);}""" |
73 fragmentCode: string = """void main() { | 90 fragmentCode: string = |
91 """void main() { | |
74 outColor = vec4(fragmentColor, 1);}""" | 92 outColor = vec4(fragmentColor, 1);}""" |
93 | |
75 TriangleMesh = object | 94 TriangleMesh = object |
76 position: GPUArray[Vec3f, VertexBuffer] | 95 position: GPUArray[Vec3f, VertexBuffer] |
77 color: GPUArray[Vec3f, VertexBuffer] | 96 color: GPUArray[Vec3f, VertexBuffer] |
97 | |
78 QuadMesh = object | 98 QuadMesh = object |
79 position: GPUArray[Vec3f, VertexBuffer] | 99 position: GPUArray[Vec3f, VertexBuffer] |
80 color: GPUArray[Vec3f, VertexBuffer] | 100 color: GPUArray[Vec3f, VertexBuffer] |
81 indices: GPUArray[uint16, IndexBuffer] | 101 indices: GPUArray[uint16, IndexBuffer] |
102 | |
82 Instances = object | 103 Instances = object |
83 pos: GPUArray[Vec3f, VertexBuffer] | 104 pos: GPUArray[Vec3f, VertexBuffer] |
84 scale: GPUArray[float32, VertexBuffer] | 105 scale: GPUArray[float32, VertexBuffer] |
106 | |
85 var tri = TriangleMesh( | 107 var tri = TriangleMesh( |
86 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), | 108 position: asGPUArray( |
109 [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer | |
110 ), | |
87 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), | 111 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), |
88 ) | 112 ) |
89 var quad = QuadMesh( | 113 var quad = QuadMesh( |
90 position: asGPUArray([vec3(-0.3, -0.3, 0), vec3(-0.3, 0.3, 0), vec3(0.3, 0.3, 0), vec3(0.3, -0.3, 0)], VertexBuffer), | 114 position: asGPUArray( |
115 [vec3(-0.3, -0.3, 0), vec3(-0.3, 0.3, 0), vec3(0.3, 0.3, 0), vec3(0.3, -0.3, 0)], | |
116 VertexBuffer, | |
117 ), | |
91 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | 118 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
92 color: asGPUArray([vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1)], VertexBuffer), | 119 color: asGPUArray( |
120 [vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1)], VertexBuffer | |
121 ), | |
93 ) | 122 ) |
94 | 123 |
95 var instancesA: Instances | 124 var instancesA: Instances |
96 for n in 1..100: | 125 for n in 1 .. 100: |
97 instancesA.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32), 0) | 126 instancesA.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32), 0) |
98 instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32) | 127 instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32) |
99 var instancesB: Instances | 128 var instancesB: Instances |
100 for n in 1..100: | 129 for n in 1 .. 100: |
101 instancesB.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32), 0) | 130 instancesB.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32), 0) |
102 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) | 131 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) |
103 | 132 |
104 assignBuffers(renderdata, tri) | 133 assignBuffers(renderdata, tri) |
105 assignBuffers(renderdata, quad) | 134 assignBuffers(renderdata, quad) |
109 | 138 |
110 var pipeline = createPipeline[SomeShader](renderPass = vulkan.swapchain.renderPass) | 139 var pipeline = createPipeline[SomeShader](renderPass = vulkan.swapchain.renderPass) |
111 | 140 |
112 var start = getMonoTime() | 141 var start = getMonoTime() |
113 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 142 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
114 | |
115 withNextFrame(framebuffer, commandbuffer): | 143 withNextFrame(framebuffer, commandbuffer): |
116 | 144 withRenderPass( |
117 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 145 vulkan.swapchain.renderPass, |
118 | 146 framebuffer, |
147 commandbuffer, | |
148 vulkan.swapchain.width, | |
149 vulkan.swapchain.height, | |
150 vec4(0, 0, 0, 0), | |
151 ): | |
119 withPipeline(commandbuffer, pipeline): | 152 withPipeline(commandbuffer, pipeline): |
120 | 153 render( |
121 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) | 154 commandbuffer = commandbuffer, |
122 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) | 155 pipeline = pipeline, |
123 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) | 156 mesh = quad, |
124 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) | 157 instances = instancesA, |
158 ) | |
159 render( | |
160 commandbuffer = commandbuffer, | |
161 pipeline = pipeline, | |
162 mesh = quad, | |
163 instances = instancesB, | |
164 ) | |
165 render( | |
166 commandbuffer = commandbuffer, | |
167 pipeline = pipeline, | |
168 mesh = tri, | |
169 instances = instancesA, | |
170 ) | |
171 render( | |
172 commandbuffer = commandbuffer, | |
173 pipeline = pipeline, | |
174 mesh = tri, | |
175 instances = instancesB, | |
176 ) | |
125 | 177 |
126 # cleanup | 178 # cleanup |
127 checkVkResult vkDeviceWaitIdle(vulkan.device) | 179 checkVkResult vkDeviceWaitIdle(vulkan.device) |
128 destroyPipeline(pipeline) | 180 destroyPipeline(pipeline) |
129 destroyRenderData(renderdata) | 181 destroyRenderData(renderdata) |
144 fragmentColor {.Pass.}: Vec3f | 196 fragmentColor {.Pass.}: Vec3f |
145 uv {.Pass.}: Vec2f | 197 uv {.Pass.}: Vec2f |
146 outColor {.ShaderOutput.}: Vec4f | 198 outColor {.ShaderOutput.}: Vec4f |
147 descriptorSets {.DescriptorSet: 0.}: Uniforms | 199 descriptorSets {.DescriptorSet: 0.}: Uniforms |
148 # code | 200 # code |
149 vertexCode: string = """void main() { | 201 vertexCode: string = |
202 """void main() { | |
150 fragmentColor = material.baseColor; | 203 fragmentColor = material.baseColor; |
151 gl_Position = vec4(position, 1); | 204 gl_Position = vec4(position, 1); |
152 gl_Position.x += ((material.baseColor.b - 0.5) * 2) - 0.5; | 205 gl_Position.x += ((material.baseColor.b - 0.5) * 2) - 0.5; |
153 uv = position.xy + 0.5; | 206 uv = position.xy + 0.5; |
154 }""" | 207 }""" |
155 fragmentCode: string = """void main() { | 208 fragmentCode: string = |
209 """void main() { | |
156 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}""" | 210 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}""" |
211 | |
157 QuadMesh = object | 212 QuadMesh = object |
158 position: GPUArray[Vec3f, VertexBuffer] | 213 position: GPUArray[Vec3f, VertexBuffer] |
159 indices: GPUArray[uint16, IndexBuffer] | 214 indices: GPUArray[uint16, IndexBuffer] |
160 | 215 |
161 let R = BGRA([255'u8, 0'u8, 0'u8, 255'u8]) | 216 let R = BGRA([255'u8, 0'u8, 0'u8, 255'u8]) |
162 let G = BGRA([0'u8, 255'u8, 0'u8, 255'u8]) | 217 let G = BGRA([0'u8, 255'u8, 0'u8, 255'u8]) |
163 let B = BGRA([0'u8, 0'u8, 255'u8, 255'u8]) | 218 let B = BGRA([0'u8, 0'u8, 255'u8, 255'u8]) |
164 let W = BGRA([255'u8, 255'u8, 255'u8, 255'u8]) | 219 let W = BGRA([255'u8, 255'u8, 255'u8, 255'u8]) |
165 var | 220 var |
166 quad = QuadMesh( | 221 quad = QuadMesh( |
167 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(-0.5, 0.5, 0), vec3(0.5, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), | 222 position: asGPUArray( |
223 [vec3(-0.5, -0.5, 0), vec3(-0.5, 0.5, 0), vec3(0.5, 0.5, 0), vec3(0.5, -0.5, 0)], | |
224 VertexBuffer, | |
225 ), | |
168 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | 226 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
169 ) | 227 ) |
170 uniforms1 = asDescriptorSetData( | 228 uniforms1 = asDescriptorSetData( |
171 Uniforms( | 229 Uniforms( |
172 material: asGPUValue(Material(baseColor: vec3(1, 1, 1)), UniformBuffer), | 230 material: asGPUValue(Material(baseColor: vec3(1, 1, 1)), UniformBuffer), |
173 texture1: Image[BGRA](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), | 231 texture1: Image[BGRA]( |
232 width: 3, | |
233 height: 3, | |
234 data: @[R, G, B, G, B, R, B, R, G], | |
235 minInterpolation: VK_FILTER_NEAREST, | |
236 magInterpolation: VK_FILTER_NEAREST, | |
237 ), | |
174 ) | 238 ) |
175 ) | 239 ) |
176 uniforms2 = asDescriptorSetData( | 240 uniforms2 = asDescriptorSetData( |
177 Uniforms( | 241 Uniforms( |
178 material: asGPUValue(Material(baseColor: vec3(0.5, 0.5, 0.5)), UniformBuffer), | 242 material: asGPUValue(Material(baseColor: vec3(0.5, 0.5, 0.5)), UniformBuffer), |
179 texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]), | 243 texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]), |
180 ) | 244 ) |
181 ) | 245 ) |
182 | 246 |
183 assignBuffers(renderdata, quad) | 247 assignBuffers(renderdata, quad) |
184 assignBuffers(renderdata, uniforms1) | 248 assignBuffers(renderdata, uniforms1) |
185 assignBuffers(renderdata, uniforms2) | 249 assignBuffers(renderdata, uniforms2) |
192 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | 256 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) |
193 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) | 257 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) |
194 | 258 |
195 var start = getMonoTime() | 259 var start = getMonoTime() |
196 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 260 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
197 | |
198 withNextFrame(framebuffer, commandbuffer): | 261 withNextFrame(framebuffer, commandbuffer): |
199 | 262 withRenderPass( |
200 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 263 vulkan.swapchain.renderPass, |
201 | 264 framebuffer, |
265 commandbuffer, | |
266 vulkan.swapchain.width, | |
267 vulkan.swapchain.height, | |
268 vec4(0, 0, 0, 0), | |
269 ): | |
202 withPipeline(commandbuffer, pipeline): | 270 withPipeline(commandbuffer, pipeline): |
203 | |
204 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) | 271 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) |
205 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 272 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
206 | 273 |
207 bindDescriptorSet(commandbuffer, uniforms2, 0, pipeline) | 274 bindDescriptorSet(commandbuffer, uniforms2, 0, pipeline) |
208 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 275 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
216 var renderdata = initRenderData() | 283 var renderdata = initRenderData() |
217 | 284 |
218 type | 285 type |
219 RenderSettings = object | 286 RenderSettings = object |
220 brigthness: float32 | 287 brigthness: float32 |
288 | |
221 Material = object | 289 Material = object |
222 baseColor: Vec3f | 290 baseColor: Vec3f |
291 | |
223 ObjectSettings = object | 292 ObjectSettings = object |
224 scale: float32 | 293 scale: float32 |
225 materialIndex: uint32 | 294 materialIndex: uint32 |
295 | |
226 Constants = object | 296 Constants = object |
227 offset: Vec2f | 297 offset: Vec2f |
228 | 298 |
229 ConstSet = object | 299 ConstSet = object |
230 constants: GPUValue[Constants, UniformBuffer] | 300 constants: GPUValue[Constants, UniformBuffer] |
301 | |
231 MainSet = object | 302 MainSet = object |
232 renderSettings: GPUValue[RenderSettings, UniformBufferMapped] | 303 renderSettings: GPUValue[RenderSettings, UniformBufferMapped] |
233 material: array[2, GPUValue[Material, UniformBuffer]] | 304 material: array[2, GPUValue[Material, UniformBuffer]] |
234 texture1: array[2, Image[Gray]] | 305 texture1: array[2, Image[Gray]] |
306 | |
235 OtherSet = object | 307 OtherSet = object |
236 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped] | 308 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped] |
237 | 309 |
238 QuadShader = object | 310 QuadShader = object |
239 position {.VertexAttribute.}: Vec3f | 311 position {.VertexAttribute.}: Vec3f |
242 outColor {.ShaderOutput.}: Vec4f | 314 outColor {.ShaderOutput.}: Vec4f |
243 descriptorSets0 {.DescriptorSet: 0.}: ConstSet | 315 descriptorSets0 {.DescriptorSet: 0.}: ConstSet |
244 descriptorSets1 {.DescriptorSet: 1.}: MainSet | 316 descriptorSets1 {.DescriptorSet: 1.}: MainSet |
245 descriptorSets2 {.DescriptorSet: 2.}: OtherSet | 317 descriptorSets2 {.DescriptorSet: 2.}: OtherSet |
246 # code | 318 # code |
247 vertexCode: string = """void main() { | 319 vertexCode: string = |
320 """void main() { | |
248 fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness; | 321 fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness; |
249 gl_Position = vec4(position * objectSettings.scale, 1); | 322 gl_Position = vec4(position * objectSettings.scale, 1); |
250 gl_Position.xy += constants.offset.xy; | 323 gl_Position.xy += constants.offset.xy; |
251 gl_Position.x += material[objectSettings.materialIndex].baseColor.b - 0.5; | 324 gl_Position.x += material[objectSettings.materialIndex].baseColor.b - 0.5; |
252 uv = position.xy + 0.5; | 325 uv = position.xy + 0.5; |
253 }""" | 326 }""" |
254 fragmentCode: string = """void main() { | 327 fragmentCode: string = |
328 """void main() { | |
255 outColor = vec4(fragmentColor * texture(texture1[objectSettings.materialIndex], uv).rrr, 1); | 329 outColor = vec4(fragmentColor * texture(texture1[objectSettings.materialIndex], uv).rrr, 1); |
256 }""" | 330 }""" |
331 | |
257 QuadMesh = object | 332 QuadMesh = object |
258 position: GPUArray[Vec3f, VertexBuffer] | 333 position: GPUArray[Vec3f, VertexBuffer] |
259 indices: GPUArray[uint16, IndexBuffer] | 334 indices: GPUArray[uint16, IndexBuffer] |
260 | 335 |
261 var quad = QuadMesh( | 336 var quad = QuadMesh( |
262 position: asGPUArray([vec3(-0.5, -0.5), vec3(-0.5, 0.5), vec3(0.5, 0.5), vec3(0.5, -0.5)], VertexBuffer), | 337 position: asGPUArray( |
338 [vec3(-0.5, -0.5), vec3(-0.5, 0.5), vec3(0.5, 0.5), vec3(0.5, -0.5)], VertexBuffer | |
339 ), | |
263 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | 340 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
264 ) | 341 ) |
265 var constset = asDescriptorSetData( | 342 var constset = asDescriptorSetData( |
266 ConstSet( | 343 ConstSet(constants: asGPUValue(Constants(offset: vec2(-0.3, 0.2)), UniformBuffer)) |
267 constants: asGPUValue(Constants(offset: vec2(-0.3, 0.2)), UniformBuffer), | |
268 ) | |
269 ) | 344 ) |
270 let G = Gray([50'u8]) | 345 let G = Gray([50'u8]) |
271 let W = Gray([255'u8]) | 346 let W = Gray([255'u8]) |
272 var mainset = asDescriptorSetData( | 347 var mainset = asDescriptorSetData( |
273 MainSet( | 348 MainSet( |
274 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped), | 349 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped), |
275 material: [ | 350 material: [ |
276 asGPUValue(Material(baseColor: vec3(1, 1, 0)), UniformBuffer), | 351 asGPUValue(Material(baseColor: vec3(1, 1, 0)), UniformBuffer), |
277 asGPUValue(Material(baseColor: vec3(1, 0, 1)), UniformBuffer), | 352 asGPUValue(Material(baseColor: vec3(1, 0, 1)), UniformBuffer), |
278 ], | 353 ], |
279 texture1: [ | 354 texture1: [ |
280 Image[Gray](width: 2, height: 2, data: @[W, G, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), | 355 Image[Gray]( |
281 Image[Gray](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), | 356 width: 2, |
282 ], | 357 height: 2, |
283 ), | 358 data: @[W, G, G, W], |
359 minInterpolation: VK_FILTER_NEAREST, | |
360 magInterpolation: VK_FILTER_NEAREST, | |
361 ), | |
362 Image[Gray]( | |
363 width: 3, | |
364 height: 3, | |
365 data: @[W, G, W, G, W, G, W, G, W], | |
366 minInterpolation: VK_FILTER_NEAREST, | |
367 magInterpolation: VK_FILTER_NEAREST, | |
368 ), | |
369 ], | |
370 ) | |
284 ) | 371 ) |
285 var otherset1 = asDescriptorSetData( | 372 var otherset1 = asDescriptorSetData( |
286 OtherSet( | 373 OtherSet( |
287 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped), | 374 objectSettings: |
375 asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped) | |
288 ) | 376 ) |
289 ) | 377 ) |
290 var otherset2 = asDescriptorSetData( | 378 var otherset2 = asDescriptorSetData( |
291 OtherSet( | 379 OtherSet( |
292 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped), | 380 objectSettings: |
381 asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped) | |
293 ) | 382 ) |
294 ) | 383 ) |
295 | 384 |
296 assignBuffers(renderdata, quad) | 385 assignBuffers(renderdata, quad) |
297 assignBuffers(renderdata, constset) | 386 assignBuffers(renderdata, constset) |
308 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) | 397 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) |
309 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) | 398 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) |
310 | 399 |
311 var start = getMonoTime() | 400 var start = getMonoTime() |
312 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 401 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
313 | |
314 withNextFrame(framebuffer, commandbuffer): | 402 withNextFrame(framebuffer, commandbuffer): |
315 bindDescriptorSet(commandbuffer, constset, 0, pipeline) | 403 bindDescriptorSet(commandbuffer, constset, 0, pipeline) |
316 bindDescriptorSet(commandbuffer, mainset, 1, pipeline) | 404 bindDescriptorSet(commandbuffer, mainset, 1, pipeline) |
317 | 405 |
318 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 406 withRenderPass( |
319 | 407 vulkan.swapchain.renderPass, |
408 framebuffer, | |
409 commandbuffer, | |
410 vulkan.swapchain.width, | |
411 vulkan.swapchain.height, | |
412 vec4(0, 0, 0, 0), | |
413 ): | |
320 withPipeline(commandbuffer, pipeline): | 414 withPipeline(commandbuffer, pipeline): |
321 | |
322 bindDescriptorSet(commandbuffer, otherset1, 2, pipeline) | 415 bindDescriptorSet(commandbuffer, otherset1, 2, pipeline) |
323 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 416 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
324 | 417 |
325 bindDescriptorSet(commandbuffer, otherset2, 2, pipeline) | 418 bindDescriptorSet(commandbuffer, otherset2, 2, pipeline) |
326 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | 419 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
327 | 420 |
328 mainset.data.renderSettings.data.brigthness = ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 421 mainset.data.renderSettings.data.brigthness = |
329 otherset1.data.objectSettings.data.scale = 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 422 ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
423 otherset1.data.objectSettings.data.scale = | |
424 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time | |
330 updateGPUBuffer(mainset.data.renderSettings) | 425 updateGPUBuffer(mainset.data.renderSettings) |
331 updateGPUBuffer(otherset1.data.objectSettings) | 426 updateGPUBuffer(otherset1.data.objectSettings) |
332 renderdata.flushAllMemory() | 427 renderdata.flushAllMemory() |
333 | 428 |
334 # cleanup | 429 # cleanup |
336 destroyPipeline(pipeline) | 431 destroyPipeline(pipeline) |
337 destroyRenderData(renderdata) | 432 destroyRenderData(renderdata) |
338 | 433 |
339 proc test_05_cube(time: float32) = | 434 proc test_05_cube(time: float32) = |
340 type | 435 type |
341 | |
342 UniformData = object | 436 UniformData = object |
343 mvp: Mat4 | 437 mvp: Mat4 |
438 | |
344 Uniforms = object | 439 Uniforms = object |
345 data: GPUValue[UniformData, UniformBufferMapped] | 440 data: GPUValue[UniformData, UniformBufferMapped] |
441 | |
346 CubeShader = object | 442 CubeShader = object |
347 position {.VertexAttribute.}: Vec3f | 443 position {.VertexAttribute.}: Vec3f |
348 color {.VertexAttribute.}: Vec4f | 444 color {.VertexAttribute.}: Vec4f |
349 fragmentColor {.Pass.}: Vec4f | 445 fragmentColor {.Pass.}: Vec4f |
350 outColor {.ShaderOutput.}: Vec4f | 446 outColor {.ShaderOutput.}: Vec4f |
351 descriptorSets {.DescriptorSet: 0.}: Uniforms | 447 descriptorSets {.DescriptorSet: 0.}: Uniforms |
352 # code | 448 # code |
353 vertexCode = """void main() { | 449 vertexCode = |
450 """void main() { | |
354 fragmentColor = color; | 451 fragmentColor = color; |
355 gl_Position = vec4(position, 1) * data.mvp; | 452 gl_Position = vec4(position, 1) * data.mvp; |
356 }""" | 453 }""" |
357 fragmentCode = """void main() { | 454 fragmentCode = |
455 """void main() { | |
358 outColor = fragmentColor; | 456 outColor = fragmentColor; |
359 }""" | 457 }""" |
458 | |
360 Mesh = object | 459 Mesh = object |
361 position: GPUArray[Vec3f, VertexBuffer] | 460 position: GPUArray[Vec3f, VertexBuffer] |
362 normals: GPUArray[Vec3f, VertexBuffer] | 461 normals: GPUArray[Vec3f, VertexBuffer] |
363 color: GPUArray[Vec4f, VertexBuffer] | 462 color: GPUArray[Vec4f, VertexBuffer] |
364 | 463 |
365 let quad = @[ | 464 let quad = |
366 vec3(-0.5, -0.5), vec3(-0.5, +0.5), vec3(+0.5, +0.5), | 465 @[ |
367 vec3(+0.5, +0.5), vec3(+0.5, -0.5), vec3(-0.5, -0.5), | 466 vec3(-0.5, -0.5), |
368 ] | 467 vec3(-0.5, +0.5), |
468 vec3(+0.5, +0.5), | |
469 vec3(+0.5, +0.5), | |
470 vec3(+0.5, -0.5), | |
471 vec3(-0.5, -0.5), | |
472 ] | |
369 proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] = | 473 proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] = |
370 for v in data: | 474 for v in data: |
371 result.add mat * v | 475 result.add mat * v |
372 | 476 |
373 var | 477 var |
413 normals: asGPUArray(normals, VertexBuffer), | 517 normals: asGPUArray(normals, VertexBuffer), |
414 ) | 518 ) |
415 assignBuffers(renderdata, mesh) | 519 assignBuffers(renderdata, mesh) |
416 | 520 |
417 var floor = Mesh( | 521 var floor = Mesh( |
418 position: asGPUArray(quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), VertexBuffer), | 522 position: asGPUArray( |
523 quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), | |
524 VertexBuffer, | |
525 ), | |
419 color: asGPUArray(newSeqWith(6, vec4(0.1, 0.1, 0.1, 1)), VertexBuffer), | 526 color: asGPUArray(newSeqWith(6, vec4(0.1, 0.1, 0.1, 1)), VertexBuffer), |
420 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), | 527 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), |
421 ) | 528 ) |
422 assignBuffers(renderdata, floor) | 529 assignBuffers(renderdata, floor) |
423 | 530 |
424 var uniforms1 = asDescriptorSetData( | 531 var uniforms1 = asDescriptorSetData( |
425 Uniforms( | 532 Uniforms(data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped)) |
426 data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped) | |
427 ) | |
428 ) | 533 ) |
429 assignBuffers(renderdata, uniforms1) | 534 assignBuffers(renderdata, uniforms1) |
430 | 535 |
431 renderdata.flushAllMemory() | 536 renderdata.flushAllMemory() |
432 | 537 |
439 var start = getMonoTime() | 544 var start = getMonoTime() |
440 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 545 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
441 let tStartLoop = getMonoTime() - tStart | 546 let tStartLoop = getMonoTime() - tStart |
442 | 547 |
443 uniforms1.data.data.data.mvp = ( | 548 uniforms1.data.data.data.mvp = ( |
444 projection(-PI / 2, getAspectRatio(), 0.01, 100) * | 549 projection(-PI / 2, getAspectRatio(), 0.01, 100) * translate(0, 0, 2) * |
445 translate(0, 0, 2) * | 550 rotate(PI / 4, X) * rotate( |
446 rotate(PI / 4, X) * | 551 PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y |
447 rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y) | 552 ) |
448 ) | 553 ) |
449 updateGPUBuffer(uniforms1.data.data, flush = true) | 554 updateGPUBuffer(uniforms1.data.data, flush = true) |
450 | 555 |
451 withNextFrame(framebuffer, commandbuffer): | 556 withNextFrame(framebuffer, commandbuffer): |
452 | 557 withRenderPass( |
453 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 558 vulkan.swapchain.renderPass, |
559 framebuffer, | |
560 commandbuffer, | |
561 vulkan.swapchain.width, | |
562 vulkan.swapchain.height, | |
563 vec4(0, 0, 0, 0), | |
564 ): | |
454 withPipeline(commandbuffer, pipeline): | 565 withPipeline(commandbuffer, pipeline): |
455 | |
456 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) | 566 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) |
457 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) | 567 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
458 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor) | 568 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor) |
459 | 569 |
460 let tEndLoop = getMonoTime() - tStart | 570 let tEndLoop = getMonoTime() - tStart |
476 position {.VertexAttribute.}: Vec3f | 586 position {.VertexAttribute.}: Vec3f |
477 color {.VertexAttribute.}: Vec3f | 587 color {.VertexAttribute.}: Vec3f |
478 fragmentColor {.Pass.}: Vec3f | 588 fragmentColor {.Pass.}: Vec3f |
479 outColor {.ShaderOutput.}: Vec4f | 589 outColor {.ShaderOutput.}: Vec4f |
480 # code | 590 # code |
481 vertexCode: string = """void main() { | 591 vertexCode: string = |
592 """void main() { | |
482 gl_PointSize = 100; | 593 gl_PointSize = 100; |
483 fragmentColor = color; | 594 fragmentColor = color; |
484 gl_Position = vec4(position, 1);}""" | 595 gl_Position = vec4(position, 1);}""" |
485 fragmentCode: string = """void main() { | 596 fragmentCode: string = |
597 """void main() { | |
486 outColor = vec4(fragmentColor, 1);}""" | 598 outColor = vec4(fragmentColor, 1);}""" |
599 | |
487 TriangleMesh = object | 600 TriangleMesh = object |
488 position: GPUArray[Vec3f, VertexBuffer] | 601 position: GPUArray[Vec3f, VertexBuffer] |
489 color: GPUArray[Vec3f, VertexBuffer] | 602 color: GPUArray[Vec3f, VertexBuffer] |
603 | |
490 var triangle = TriangleMesh( | 604 var triangle = TriangleMesh( |
491 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), | 605 position: asGPUArray( |
606 [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer | |
607 ), | |
492 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), | 608 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), |
493 ) | 609 ) |
494 var lines = TriangleMesh( | 610 var lines = TriangleMesh( |
495 position: asGPUArray([vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], VertexBuffer), | 611 position: asGPUArray( |
496 color: asGPUArray([vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer), | 612 [vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], |
613 VertexBuffer, | |
614 ), | |
615 color: asGPUArray( | |
616 [vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer | |
617 ), | |
497 ) | 618 ) |
498 assignBuffers(renderdata, triangle) | 619 assignBuffers(renderdata, triangle) |
499 assignBuffers(renderdata, lines) | 620 assignBuffers(renderdata, lines) |
500 renderdata.flushAllMemory() | 621 renderdata.flushAllMemory() |
501 | 622 |
502 var pipeline1 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_LINE, lineWidth = 20'f32) | 623 var pipeline1 = createPipeline[Shader]( |
503 var pipeline2 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT) | 624 renderPass = vulkan.swapchain.renderPass, |
504 var pipeline3 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, lineWidth = 5) | 625 polygonMode = VK_POLYGON_MODE_LINE, |
505 var pipeline4 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST) | 626 lineWidth = 20'f32, |
627 ) | |
628 var pipeline2 = createPipeline[Shader]( | |
629 renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT | |
630 ) | |
631 var pipeline3 = createPipeline[Shader]( | |
632 renderPass = vulkan.swapchain.renderPass, | |
633 topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, | |
634 lineWidth = 5, | |
635 ) | |
636 var pipeline4 = createPipeline[Shader]( | |
637 renderPass = vulkan.swapchain.renderPass, | |
638 topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST, | |
639 ) | |
506 | 640 |
507 var start = getMonoTime() | 641 var start = getMonoTime() |
508 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 642 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
509 withNextFrame(framebuffer, commandbuffer): | 643 withNextFrame(framebuffer, commandbuffer): |
510 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 644 withRenderPass( |
645 vulkan.swapchain.renderPass, | |
646 framebuffer, | |
647 commandbuffer, | |
648 vulkan.swapchain.width, | |
649 vulkan.swapchain.height, | |
650 vec4(0, 0, 0, 0), | |
651 ): | |
511 withPipeline(commandbuffer, pipeline1): | 652 withPipeline(commandbuffer, pipeline1): |
512 render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle) | 653 render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle) |
513 withPipeline(commandbuffer, pipeline2): | 654 withPipeline(commandbuffer, pipeline2): |
514 render(commandbuffer = commandbuffer, pipeline = pipeline2, mesh = triangle) | 655 render(commandbuffer = commandbuffer, pipeline = pipeline2, mesh = triangle) |
515 withPipeline(commandbuffer, pipeline3): | 656 withPipeline(commandbuffer, pipeline3): |
529 var renderdata = initRenderData() | 670 var renderdata = initRenderData() |
530 | 671 |
531 type | 672 type |
532 Uniforms = object | 673 Uniforms = object |
533 texture1: Image[BGRA] | 674 texture1: Image[BGRA] |
675 | |
534 Shader = object | 676 Shader = object |
535 position {.VertexAttribute.}: Vec3f | 677 position {.VertexAttribute.}: Vec3f |
536 uv {.VertexAttribute.}: Vec2f | 678 uv {.VertexAttribute.}: Vec2f |
537 fragmentUv {.Pass.}: Vec2f | 679 fragmentUv {.Pass.}: Vec2f |
538 outColor {.ShaderOutput.}: Vec4f | 680 outColor {.ShaderOutput.}: Vec4f |
539 descriptorSets {.DescriptorSet: 0.}: Uniforms | 681 descriptorSets {.DescriptorSet: 0.}: Uniforms |
540 # code | 682 # code |
541 vertexCode: string = """ | 683 vertexCode: string = |
684 """ | |
542 void main() { | 685 void main() { |
543 fragmentUv = uv; | 686 fragmentUv = uv; |
544 gl_Position = vec4(position, 1); | 687 gl_Position = vec4(position, 1); |
545 }""" | 688 }""" |
546 fragmentCode: string = """ | 689 fragmentCode: string = |
690 """ | |
547 void main() { | 691 void main() { |
548 outColor = texture(texture1, fragmentUv); | 692 outColor = texture(texture1, fragmentUv); |
549 }""" | 693 }""" |
694 | |
550 Quad = object | 695 Quad = object |
551 position: GPUArray[Vec3f, VertexBuffer] | 696 position: GPUArray[Vec3f, VertexBuffer] |
552 uv: GPUArray[Vec2f, VertexBuffer] | 697 uv: GPUArray[Vec2f, VertexBuffer] |
698 | |
553 var mesh = Quad( | 699 var mesh = Quad( |
554 position: asGPUArray([ | 700 position: asGPUArray( |
555 vec3(-0.8, -0.5), vec3(-0.8, 0.5), vec3(0.8, 0.5), | 701 [ |
556 vec3(0.8, 0.5), vec3(0.8, -0.5), vec3(-0.8, -0.5), | 702 vec3(-0.8, -0.5), |
557 ], VertexBuffer), | 703 vec3(-0.8, 0.5), |
558 uv: asGPUArray([ | 704 vec3(0.8, 0.5), |
559 vec2(0, 1), vec2(0, 0), vec2(1, 0), | 705 vec3(0.8, 0.5), |
560 vec2(1, 0), vec2(1, 1), vec2(0, 1), | 706 vec3(0.8, -0.5), |
561 ], VertexBuffer), | 707 vec3(-0.8, -0.5), |
708 ], | |
709 VertexBuffer, | |
710 ), | |
711 uv: asGPUArray( | |
712 [vec2(0, 1), vec2(0, 0), vec2(1, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1)], | |
713 VertexBuffer, | |
714 ), | |
562 ) | 715 ) |
563 assignBuffers(renderdata, mesh) | 716 assignBuffers(renderdata, mesh) |
564 renderdata.flushAllMemory() | 717 renderdata.flushAllMemory() |
565 | 718 |
566 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass) | 719 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass) |
567 var uniforms1 = asDescriptorSetData( | 720 var uniforms1 = asDescriptorSetData(Uniforms(texture1: loadImage[BGRA]("art.png"))) |
568 Uniforms( | |
569 texture1: loadImage[BGRA]("art.png"), | |
570 ) | |
571 ) | |
572 uploadImages(renderdata, uniforms1) | 721 uploadImages(renderdata, uniforms1) |
573 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | 722 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) |
574 | 723 |
575 var start = getMonoTime() | 724 var start = getMonoTime() |
576 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 725 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
577 | |
578 withNextFrame(framebuffer, commandbuffer): | 726 withNextFrame(framebuffer, commandbuffer): |
579 | 727 withRenderPass( |
580 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 728 vulkan.swapchain.renderPass, |
581 | 729 framebuffer, |
730 commandbuffer, | |
731 vulkan.swapchain.width, | |
732 vulkan.swapchain.height, | |
733 vec4(0, 0, 0, 0), | |
734 ): | |
582 withPipeline(commandbuffer, pipeline): | 735 withPipeline(commandbuffer, pipeline): |
583 | |
584 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) | 736 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) |
585 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) | 737 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
586 | 738 |
587 # cleanup | 739 # cleanup |
588 checkVkResult vkDeviceWaitIdle(vulkan.device) | 740 checkVkResult vkDeviceWaitIdle(vulkan.device) |
589 destroyPipeline(pipeline) | 741 destroyPipeline(pipeline) |
590 destroyRenderData(renderdata) | 742 destroyRenderData(renderdata) |
591 | 743 |
592 proc test_08_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) = | 744 proc test_08_triangle_2pass( |
593 var (offscreenRP, presentRP) = createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 745 time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits |
746 ) = | |
747 var (offscreenRP, presentRP) = | |
748 createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | |
594 | 749 |
595 setupSwapchain(renderpass = presentRP) | 750 setupSwapchain(renderpass = presentRP) |
596 | 751 |
597 var renderdata = initRenderData() | 752 var renderdata = initRenderData() |
598 | 753 |
599 type | 754 type |
600 Uniforms = object | 755 Uniforms = object |
601 frameTexture: Image[BGRA] | 756 frameTexture: Image[BGRA] |
757 | |
602 TriangleShader = object | 758 TriangleShader = object |
603 position {.VertexAttribute.}: Vec3f | 759 position {.VertexAttribute.}: Vec3f |
604 color {.VertexAttribute.}: Vec3f | 760 color {.VertexAttribute.}: Vec3f |
605 fragmentColor {.Pass.}: Vec3f | 761 fragmentColor {.Pass.}: Vec3f |
606 outColor {.ShaderOutput.}: Vec4f | 762 outColor {.ShaderOutput.}: Vec4f |
607 # code | 763 # code |
608 vertexCode: string = """void main() { | 764 vertexCode: string = |
765 """void main() { | |
609 fragmentColor = color; | 766 fragmentColor = color; |
610 gl_Position = vec4(position, 1);}""" | 767 gl_Position = vec4(position, 1);}""" |
611 fragmentCode: string = """void main() { | 768 fragmentCode: string = |
769 """void main() { | |
612 outColor = vec4(fragmentColor, 1);}""" | 770 outColor = vec4(fragmentColor, 1);}""" |
771 | |
613 PresentShader = object | 772 PresentShader = object |
614 position {.VertexAttribute.}: Vec2f | 773 position {.VertexAttribute.}: Vec2f |
615 uv {.Pass.}: Vec2f | 774 uv {.Pass.}: Vec2f |
616 outColor {.ShaderOutput.}: Vec4f | 775 outColor {.ShaderOutput.}: Vec4f |
617 descriptorSets {.DescriptorSet: 0.}: Uniforms | 776 descriptorSets {.DescriptorSet: 0.}: Uniforms |
618 # code | 777 # code |
619 vertexCode: string = """void main() { | 778 vertexCode: string = |
779 """void main() { | |
620 uv = ((position + 1) * 0.5) * vec2(1, -1); | 780 uv = ((position + 1) * 0.5) * vec2(1, -1); |
621 gl_Position = vec4(position, 0, 1);}""" | 781 gl_Position = vec4(position, 0, 1);}""" |
622 fragmentCode: string = """void main() { | 782 fragmentCode: string = |
783 """void main() { | |
623 vec2 uv1 = uv + vec2(0.001, 0.001); | 784 vec2 uv1 = uv + vec2(0.001, 0.001); |
624 vec2 uv2 = uv + vec2(0.001, -0.001); | 785 vec2 uv2 = uv + vec2(0.001, -0.001); |
625 vec2 uv3 = uv + vec2(-0.001, 0.001); | 786 vec2 uv3 = uv + vec2(-0.001, 0.001); |
626 vec2 uv4 = uv + vec2(-0.001, -0.001); | 787 vec2 uv4 = uv + vec2(-0.001, -0.001); |
627 outColor = ( | 788 outColor = ( |
629 texture(frameTexture, uv2) + | 790 texture(frameTexture, uv2) + |
630 texture(frameTexture, uv3) + | 791 texture(frameTexture, uv3) + |
631 texture(frameTexture, uv4) | 792 texture(frameTexture, uv4) |
632 ) / 4; | 793 ) / 4; |
633 }""" | 794 }""" |
795 | |
634 TriangleMesh = object | 796 TriangleMesh = object |
635 position: GPUArray[Vec3f, VertexBuffer] | 797 position: GPUArray[Vec3f, VertexBuffer] |
636 color: GPUArray[Vec3f, VertexBuffer] | 798 color: GPUArray[Vec3f, VertexBuffer] |
799 | |
637 QuadMesh = object | 800 QuadMesh = object |
638 position: GPUArray[Vec2f, VertexBuffer] | 801 position: GPUArray[Vec2f, VertexBuffer] |
639 indices: GPUArray[uint16, IndexBuffer] | 802 indices: GPUArray[uint16, IndexBuffer] |
803 | |
640 var mesh = TriangleMesh( | 804 var mesh = TriangleMesh( |
641 position: asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer), | 805 position: |
806 asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer), | |
642 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), | 807 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), |
643 ) | 808 ) |
644 var quad = QuadMesh( | 809 var quad = QuadMesh( |
645 position: asGPUArray([vec2(-1, -1), vec2(-1, 1), vec2(1, 1), vec2(1, -1)], VertexBuffer), | 810 position: |
811 asGPUArray([vec2(-1, -1), vec2(-1, 1), vec2(1, 1), vec2(1, -1)], VertexBuffer), | |
646 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | 812 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
647 ) | 813 ) |
648 var uniforms1 = asDescriptorSetData( | 814 var uniforms1 = asDescriptorSetData( |
649 Uniforms( | 815 Uniforms( |
650 frameTexture: Image[BGRA](width: vulkan.swapchain.width, height: vulkan.swapchain.height, isRenderTarget: true), | 816 frameTexture: Image[BGRA]( |
817 width: vulkan.swapchain.width, | |
818 height: vulkan.swapchain.height, | |
819 isRenderTarget: true, | |
820 ) | |
651 ) | 821 ) |
652 ) | 822 ) |
653 assignBuffers(renderdata, mesh) | 823 assignBuffers(renderdata, mesh) |
654 assignBuffers(renderdata, quad) | 824 assignBuffers(renderdata, quad) |
655 uploadImages(renderdata, uniforms1) | 825 uploadImages(renderdata, uniforms1) |
674 usage = [VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT], | 844 usage = [VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT], |
675 samples = offscreenRP.samples, | 845 samples = offscreenRP.samples, |
676 ) | 846 ) |
677 let requirements = svkGetImageMemoryRequirements(depthImage) | 847 let requirements = svkGetImageMemoryRequirements(depthImage) |
678 depthMemory = svkAllocateMemory( | 848 depthMemory = svkAllocateMemory( |
679 requirements.size, | 849 requirements.size, bestMemory(mappable = false, filter = requirements.memoryTypes) |
680 bestMemory(mappable = false, filter = requirements.memoryTypes) | 850 ) |
681 ) | 851 checkVkResult vkBindImageMemory(vulkan.device, depthImage, depthMemory, 0) |
682 checkVkResult vkBindImageMemory( | |
683 vulkan.device, | |
684 depthImage, | |
685 depthMemory, | |
686 0, | |
687 ) | |
688 depthImageView = svkCreate2DImageView( | 852 depthImageView = svkCreate2DImageView( |
689 image = depthImage, | 853 image = depthImage, format = DEPTH_FORMAT, aspect = VK_IMAGE_ASPECT_DEPTH_BIT |
690 format = DEPTH_FORMAT, | |
691 aspect = VK_IMAGE_ASPECT_DEPTH_BIT | |
692 ) | 854 ) |
693 | 855 |
694 # create msaa images (will not use the one in the swapchain | 856 # create msaa images (will not use the one in the swapchain |
695 var | 857 var |
696 msaaImage: VkImage | 858 msaaImage: VkImage |
704 usage = [VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT], | 866 usage = [VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT], |
705 samples = offscreenRP.samples, | 867 samples = offscreenRP.samples, |
706 ) | 868 ) |
707 let requirements = svkGetImageMemoryRequirements(msaaImage) | 869 let requirements = svkGetImageMemoryRequirements(msaaImage) |
708 msaaMemory = svkAllocateMemory( | 870 msaaMemory = svkAllocateMemory( |
709 requirements.size, | 871 requirements.size, bestMemory(mappable = false, filter = requirements.memoryTypes) |
710 bestMemory(mappable = false, filter = requirements.memoryTypes) | 872 ) |
711 ) | 873 checkVkResult vkBindImageMemory(vulkan.device, msaaImage, msaaMemory, 0) |
712 checkVkResult vkBindImageMemory( | |
713 vulkan.device, | |
714 msaaImage, | |
715 msaaMemory, | |
716 0, | |
717 ) | |
718 msaaImageView = svkCreate2DImageView(image = msaaImage, format = SURFACE_FORMAT) | 874 msaaImageView = svkCreate2DImageView(image = msaaImage, format = SURFACE_FORMAT) |
719 | 875 |
720 var attachments: seq[VkImageView] | 876 var attachments: seq[VkImageView] |
721 if offscreenRP.samples == VK_SAMPLE_COUNT_1_BIT: | 877 if offscreenRP.samples == VK_SAMPLE_COUNT_1_BIT: |
722 if offscreenRP.depthBuffer: | 878 if offscreenRP.depthBuffer: |
723 attachments = @[uniforms1.data.frameTexture.imageview, depthImageView] | 879 attachments = @[uniforms1.data.frameTexture.imageview, depthImageView] |
724 else: | 880 else: |
725 attachments = @[uniforms1.data.frameTexture.imageview] | 881 attachments = @[uniforms1.data.frameTexture.imageview] |
726 else: | 882 else: |
727 if offscreenRP.depthBuffer: | 883 if offscreenRP.depthBuffer: |
728 attachments = @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview] | 884 attachments = |
885 @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview] | |
729 else: | 886 else: |
730 attachments = @[msaaImageView, uniforms1.data.frameTexture.imageview] | 887 attachments = @[msaaImageView, uniforms1.data.frameTexture.imageview] |
731 var offscreenFB = svkCreateFramebuffer( | 888 var offscreenFB = svkCreateFramebuffer( |
732 offscreenRP.vk, | 889 offscreenRP.vk, vulkan.swapchain.width, vulkan.swapchain.height, attachments |
733 vulkan.swapchain.width, | |
734 vulkan.swapchain.height, | |
735 attachments | |
736 ) | 890 ) |
737 | 891 |
738 var start = getMonoTime() | 892 var start = getMonoTime() |
739 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 893 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
740 | |
741 withNextFrame(framebuffer, commandbuffer): | 894 withNextFrame(framebuffer, commandbuffer): |
742 | 895 withRenderPass( |
743 withRenderPass(offscreenRP, offscreenFB, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 896 offscreenRP, |
897 offscreenFB, | |
898 commandbuffer, | |
899 vulkan.swapchain.width, | |
900 vulkan.swapchain.height, | |
901 vec4(0, 0, 0, 0), | |
902 ): | |
744 withPipeline(commandbuffer, drawPipeline): | 903 withPipeline(commandbuffer, drawPipeline): |
745 render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) | 904 render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) |
746 | 905 |
747 withRenderPass(presentRP, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 906 withRenderPass( |
748 | 907 presentRP, |
908 framebuffer, | |
909 commandbuffer, | |
910 vulkan.swapchain.width, | |
911 vulkan.swapchain.height, | |
912 vec4(0, 0, 0, 0), | |
913 ): | |
749 withPipeline(commandbuffer, presentPipeline): | 914 withPipeline(commandbuffer, presentPipeline): |
750 | |
751 bindDescriptorSet(commandbuffer, uniforms1, 0, presentPipeline) | 915 bindDescriptorSet(commandbuffer, uniforms1, 0, presentPipeline) |
752 render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) | 916 render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) |
753 | 917 |
754 # cleanup | 918 # cleanup |
755 checkVkResult vkDeviceWaitIdle(vulkan.device) | 919 checkVkResult vkDeviceWaitIdle(vulkan.device) |
773 var time = 1'f32 | 937 var time = 1'f32 |
774 initVulkan() | 938 initVulkan() |
775 | 939 |
776 var mainRenderpass: RenderPass | 940 var mainRenderpass: RenderPass |
777 var renderPasses = [ | 941 var renderPasses = [ |
778 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), | 942 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), |
779 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), | 943 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), |
780 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), | 944 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), |
781 (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), | 945 (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), |
782 ] | 946 ] |
783 | |
784 | 947 |
785 # test normal | 948 # test normal |
786 for i, (depthBuffer, samples) in renderPasses: | 949 for i, (depthBuffer, samples) in renderPasses: |
787 var renderpass = createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 950 var renderpass = |
951 createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | |
788 setupSwapchain(renderpass = renderpass) | 952 setupSwapchain(renderpass = renderpass) |
789 | 953 |
790 # tests a simple triangle with minimalistic shader and vertex format | 954 # tests a simple triangle with minimalistic shader and vertex format |
791 test_01_triangle(time) | 955 test_01_triangle(time) |
792 | 956 |