annotate tests/test_rendering.nim @ 1337:164276e8697f

did: small fix with image arrays
author sam <sam@basx.dev>
date Sat, 24 Aug 2024 14:10:21 +0700
parents d5a6f69dc855
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1233
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
1 import std/os
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
2 import std/sequtils
1299
6d0162bfe48a did: finish mentioned refactoring, no API changes still
sam <sam@basx.dev>
parents: 1285
diff changeset
3 import std/math
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
4 import std/monotimes
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
5 import std/times
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
6 import std/options
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
7 import std/random
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
8
1267
4cf9872f7bb6 did: rename back to old name (no v2)
sam <sam@basx.dev>
parents: 1252
diff changeset
9 import ../semicongine
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
10
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
11 proc test_01_triangle(time: float32) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
12 var renderdata = initRenderData()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
13
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
14 type
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
15 PushConstant = object
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
16 scale: float32
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
17
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
18 Shader = object
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
19 position {.VertexAttribute.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
20 color {.VertexAttribute.}: Vec3f
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
21 pushConstant {.PushConstant.}: PushConstant
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
22 fragmentColor {.Pass.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
23 outColor {.ShaderOutput.}: Vec4f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
24 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
25 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
26 """void main() {
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
27 fragmentColor = color;
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
28 gl_Position = vec4(position * pushConstant.scale, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
29 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
30 """void main() {
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
31 outColor = vec4(fragmentColor, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
32
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
33 TriangleMesh = object
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
34 position: GPUArray[Vec3f, VertexBuffer]
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
35 color: GPUArray[Vec3f, VertexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
36
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
37 var mesh = TriangleMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
38 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
39 [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
40 ),
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
41 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer),
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
42 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
43 assignBuffers(renderdata, mesh)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
44 renderdata.flushAllMemory()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
45
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
46 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
47
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
48 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
49 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: 1280
diff changeset
50 withNextFrame(framebuffer, commandbuffer):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
51 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
52 vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
53 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
54 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
55 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
56 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
57 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
58 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
59 withPipeline(commandbuffer, pipeline):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
60 renderWithPushConstant(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
61 commandbuffer = commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
62 pipeline = pipeline,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
63 mesh = mesh,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
64 pushConstant = PushConstant(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
65 scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000)
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
66 ),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
67 )
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
68
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
69 # cleanup
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
70 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
71 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
72 destroyRenderData(renderdata)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
73
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
74 proc test_02_triangle_quad_instanced(time: float32) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
75 var renderdata = initRenderData()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
76
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
77 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
78 SomeShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
79 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
80 color {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
81 pos {.InstanceAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
82 scale {.InstanceAttribute.}: float32
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
83 fragmentColor {.Pass.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
84 outColor {.ShaderOutput.}: Vec4f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
85 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
86 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
87 """void main() {
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
88 fragmentColor = color;
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
89 gl_Position = vec4((position * scale) + pos, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
90 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
91 """void main() {
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
92 outColor = vec4(fragmentColor, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
93
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
94 TriangleMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
95 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
96 color: GPUArray[Vec3f, VertexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
97
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
98 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
99 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
100 color: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
101 indices: GPUArray[uint16, IndexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
102
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
103 Instances = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
104 pos: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
105 scale: GPUArray[float32, VertexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
106
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
107 var tri = TriangleMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
108 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
109 [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
110 ),
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
111 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
112 )
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
113 var quad = QuadMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
114 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
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)],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
116 VertexBuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
117 ),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
118 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
119 color: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
120 [vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1)], VertexBuffer
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
121 ),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
122 )
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
123
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
124 var instancesA: Instances
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
125 for n in 1 .. 100:
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
126 instancesA.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32), 0)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
127 instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
128 var instancesB: Instances
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
129 for n in 1 .. 100:
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
130 instancesB.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32), 0)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
131 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
132
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
133 assignBuffers(renderdata, tri)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
134 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
135 assignBuffers(renderdata, instancesA)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
136 assignBuffers(renderdata, instancesB)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
137 renderdata.flushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
138
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
139 var pipeline = createPipeline[SomeShader](renderPass = vulkan.swapchain.renderPass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
140
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
141 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
142 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: 1280
diff changeset
143 withNextFrame(framebuffer, commandbuffer):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
144 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
145 vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
146 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
147 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
148 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
149 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
150 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
151 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
152 withPipeline(commandbuffer, pipeline):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
153 render(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
154 commandbuffer = commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
155 pipeline = pipeline,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
156 mesh = quad,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
157 instances = instancesA,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
158 )
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
159 render(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
160 commandbuffer = commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
161 pipeline = pipeline,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
162 mesh = quad,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
163 instances = instancesB,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
164 )
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
165 render(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
166 commandbuffer = commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
167 pipeline = pipeline,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
168 mesh = tri,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
169 instances = instancesA,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
170 )
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
171 render(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
172 commandbuffer = commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
173 pipeline = pipeline,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
174 mesh = tri,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
175 instances = instancesB,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
176 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
177
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
178 # cleanup
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
179 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
180 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
181 destroyRenderData(renderdata)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
182
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
183 proc test_03_simple_descriptorset(time: float32) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
184 var renderdata = initRenderData()
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
185
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
186 type
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
187 Material = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
188 baseColor: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
189
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
190 Uniforms = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
191 material: GPUValue[Material, UniformBuffer]
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
192 texture1: Image[BGRA]
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
193
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
194 QuadShader = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
195 position {.VertexAttribute.}: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
196 fragmentColor {.Pass.}: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
197 uv {.Pass.}: Vec2f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
198 outColor {.ShaderOutput.}: Vec4f
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
199 descriptorSets {.DescriptorSet: 0.}: Uniforms
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
200 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
201 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
202 """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
203 fragmentColor = material.baseColor;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
204 gl_Position = vec4(position, 1);
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
205 gl_Position.x += ((material.baseColor.b - 0.5) * 2) - 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
206 uv = position.xy + 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
207 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
208 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
209 """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
210 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
211
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
212 QuadMesh = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
213 position: GPUArray[Vec3f, VertexBuffer]
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
214 indices: GPUArray[uint16, IndexBuffer]
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
215
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
216 let R = BGRA([255'u8, 0'u8, 0'u8, 255'u8])
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
217 let G = BGRA([0'u8, 255'u8, 0'u8, 255'u8])
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
218 let B = BGRA([0'u8, 0'u8, 255'u8, 255'u8])
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
219 let W = BGRA([255'u8, 255'u8, 255'u8, 255'u8])
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
220 var
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
221 quad = QuadMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
222 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
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)],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
224 VertexBuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
225 ),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
226 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
227 )
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
228 uniforms1 = asDescriptorSetData(
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
229 Uniforms(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
230 material: asGPUValue(Material(baseColor: vec3(1, 1, 1)), UniformBuffer),
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
231 texture1: Image[BGRA](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
232 width: 3,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
233 height: 3,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
234 data: @[R, G, B, G, B, R, B, R, G],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
235 minInterpolation: VK_FILTER_NEAREST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
236 magInterpolation: VK_FILTER_NEAREST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
237 ),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
238 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
239 )
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
240 uniforms2 = asDescriptorSetData(
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
241 Uniforms(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
242 material: asGPUValue(Material(baseColor: vec3(0.5, 0.5, 0.5)), UniformBuffer),
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
243 texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]),
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
244 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
245 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
246
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
247 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
248 assignBuffers(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
249 assignBuffers(renderdata, uniforms2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
250 uploadImages(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
251 uploadImages(renderdata, uniforms2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
252 renderdata.flushAllMemory()
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
253
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
254 var pipeline = createPipeline[QuadShader](renderPass = vulkan.swapchain.renderPass)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
255
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
256 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
257 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
258
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
259 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
260 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: 1280
diff changeset
261 withNextFrame(framebuffer, commandbuffer):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
262 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
263 vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
264 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
265 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
266 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
267 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
268 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
269 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
270 withPipeline(commandbuffer, pipeline):
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
271 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
272 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
273
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
274 bindDescriptorSet(commandbuffer, uniforms2, 0, pipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
275 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
276
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
277 # cleanup
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
278 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
279 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
280 destroyRenderData(renderdata)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
281
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
282 proc test_04_multiple_descriptorsets(time: float32) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
283 var renderdata = initRenderData()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
284
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
285 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
286 RenderSettings = object
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
287 brigthness: float32
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
288
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
289 Material = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
290 baseColor: Vec3f
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
291
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
292 ObjectSettings = object
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
293 scale: float32
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
294 materialIndex: uint32
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
295
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
296 Constants = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
297 offset: Vec2f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
298
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
299 ConstSet = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
300 constants: GPUValue[Constants, UniformBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
301
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
302 MainSet = object
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
303 renderSettings: GPUValue[RenderSettings, UniformBufferMapped]
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
304 material: array[2, GPUValue[Material, UniformBuffer]]
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
305 texture1: array[2, Image[Gray]]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
306
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
307 OtherSet = object
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
308 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped]
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
309
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
310 QuadShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
311 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
312 fragmentColor {.Pass.}: Vec3f
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
313 uv {.Pass.}: Vec2f
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
314 outColor {.ShaderOutput.}: Vec4f
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
315 descriptorSets0 {.DescriptorSet: 0.}: ConstSet
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
316 descriptorSets1 {.DescriptorSet: 1.}: MainSet
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
317 descriptorSets2 {.DescriptorSet: 2.}: OtherSet
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
318 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
319 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
320 """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
321 fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
322 gl_Position = vec4(position * objectSettings.scale, 1);
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
323 gl_Position.xy += constants.offset.xy;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
324 gl_Position.x += material[objectSettings.materialIndex].baseColor.b - 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
325 uv = position.xy + 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
326 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
327 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
328 """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
329 outColor = vec4(fragmentColor * texture(texture1[objectSettings.materialIndex], uv).rrr, 1);
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
330 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
331
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
332 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
333 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
334 indices: GPUArray[uint16, IndexBuffer]
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
335
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
336 var quad = QuadMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
337 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
338 [vec3(-0.5, -0.5), vec3(-0.5, 0.5), vec3(0.5, 0.5), vec3(0.5, -0.5)], VertexBuffer
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
339 ),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
340 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
341 )
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
342 var constset = asDescriptorSetData(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
343 ConstSet(constants: asGPUValue(Constants(offset: vec2(-0.3, 0.2)), UniformBuffer))
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
344 )
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
345 let G = Gray([50'u8])
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
346 let W = Gray([255'u8])
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
347 var mainset = asDescriptorSetData(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
348 MainSet(
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
349 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
350 material: [
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
351 asGPUValue(Material(baseColor: vec3(1, 1, 0)), UniformBuffer),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
352 asGPUValue(Material(baseColor: vec3(1, 0, 1)), UniformBuffer),
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
353 ],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
354 texture1: [
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
355 Image[Gray](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
356 width: 2,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
357 height: 2,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
358 data: @[W, G, G, W],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
359 minInterpolation: VK_FILTER_NEAREST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
360 magInterpolation: VK_FILTER_NEAREST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
361 ),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
362 Image[Gray](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
363 width: 3,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
364 height: 3,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
365 data: @[W, G, W, G, W, G, W, G, W],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
366 minInterpolation: VK_FILTER_NEAREST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
367 magInterpolation: VK_FILTER_NEAREST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
368 ),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
369 ],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
370 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
371 )
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
372 var otherset1 = asDescriptorSetData(
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
373 OtherSet(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
374 objectSettings:
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
375 asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped)
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
376 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
377 )
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
378 var otherset2 = asDescriptorSetData(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
379 OtherSet(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
380 objectSettings:
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
381 asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped)
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
382 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
383 )
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
384
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
385 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
386 assignBuffers(renderdata, constset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
387 assignBuffers(renderdata, mainset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
388 assignBuffers(renderdata, otherset1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
389 assignBuffers(renderdata, otherset2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
390 uploadImages(renderdata, mainset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
391 renderdata.flushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
392
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
393 var pipeline = createPipeline[QuadShader](renderPass = vulkan.swapchain.renderPass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
394
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
395 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
396 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
397 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
398 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2)
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
399
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
400 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
401 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: 1280
diff changeset
402 withNextFrame(framebuffer, commandbuffer):
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
403 bindDescriptorSet(commandbuffer, constset, 0, pipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
404 bindDescriptorSet(commandbuffer, mainset, 1, pipeline)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
405
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
406 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
407 vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
408 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
409 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
410 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
411 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
412 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
413 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
414 withPipeline(commandbuffer, pipeline):
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
415 bindDescriptorSet(commandbuffer, otherset1, 2, pipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
416 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
417
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
418 bindDescriptorSet(commandbuffer, otherset2, 2, pipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
419 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
420
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
421 mainset.data.renderSettings.data.brigthness =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
422 ((getMonoTime() - start).inMilliseconds().int / 1000) / time
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
423 otherset1.data.objectSettings.data.scale =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
424 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
425 updateGPUBuffer(mainset.data.renderSettings)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
426 updateGPUBuffer(otherset1.data.objectSettings)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
427 renderdata.flushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
428
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
429 # cleanup
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
430 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
431 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
432 destroyRenderData(renderdata)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
433
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
434 proc test_05_cube(time: float32) =
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
435 type
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
436 UniformData = object
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
437 mvp: Mat4
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
438
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
439 Uniforms = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
440 data: GPUValue[UniformData, UniformBufferMapped]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
441
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
442 CubeShader = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
443 position {.VertexAttribute.}: Vec3f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
444 color {.VertexAttribute.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
445 fragmentColor {.Pass.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
446 outColor {.ShaderOutput.}: Vec4f
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
447 descriptorSets {.DescriptorSet: 0.}: Uniforms
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
448 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
449 vertexCode =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
450 """void main() {
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
451 fragmentColor = color;
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
452 gl_Position = vec4(position, 1) * data.mvp;
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
453 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
454 fragmentCode =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
455 """void main() {
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
456 outColor = fragmentColor;
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
457 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
458
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
459 Mesh = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
460 position: GPUArray[Vec3f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
461 normals: GPUArray[Vec3f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
462 color: GPUArray[Vec4f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
463
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
464 let quad =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
465 @[
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
466 vec3(-0.5, -0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
467 vec3(-0.5, +0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
468 vec3(+0.5, +0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
469 vec3(+0.5, +0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
470 vec3(+0.5, -0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
471 vec3(-0.5, -0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
472 ]
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
473 proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] =
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
474 for v in data:
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
475 result.add mat * v
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
476
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
477 var
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
478 vertices: seq[Vec3f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
479 colors: seq[Vec4f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
480 normals: seq[Vec3f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
481
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
482 # front, red
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
483 vertices.add quad.transf(translate(0, 0, -0.5))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
484 colors.add newSeqWith(6, vec4(1, 0, 0, 1))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
485 normals.add newSeqWith(6, vec3(0, 0, -1))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
486
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
487 # back, cyan
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
488 vertices.add quad.transf(rotate(PI, Y) * translate(0, 0, -0.5))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
489 colors.add newSeqWith(6, vec4(0, 1, 1, 1))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
490 normals.add newSeqWith(6, vec3(0, 0, 1))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
491
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
492 # right, green
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
493 vertices.add quad.transf(rotate(PI / 2, Y) * translate(0, 0, -0.5))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
494 colors.add newSeqWith(6, vec4(0, 1, 0, 1))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
495 normals.add newSeqWith(6, vec3(-1, 0, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
496
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
497 # left, magenta
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
498 vertices.add quad.transf(rotate(-PI / 2, Y) * translate(0, 0, -0.5))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
499 colors.add newSeqWith(6, vec4(1, 0, 1, 1))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
500 normals.add newSeqWith(6, vec3(1, 0, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
501
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
502 # bottom, blue
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
503 vertices.add quad.transf(rotate(PI / 2, X) * translate(0, 0, -0.5))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
504 colors.add newSeqWith(6, vec4(0, 0, 1, 1))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
505 normals.add newSeqWith(6, vec3(0, -1, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
506
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
507 # top, yellow
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
508 vertices.add quad.transf(rotate(-PI / 2, X) * translate(0, 0, -0.5))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
509 colors.add newSeqWith(6, vec4(1, 1, 0, 1))
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
510 normals.add newSeqWith(6, vec3(0, 1, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
511
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
512 var renderdata = initRenderData()
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
513
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
514 var mesh = Mesh(
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
515 position: asGPUArray(vertices, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
516 color: asGPUArray(colors, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
517 normals: asGPUArray(normals, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
518 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
519 assignBuffers(renderdata, mesh)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
520
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
521 var floor = Mesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
522 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
523 quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
524 VertexBuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
525 ),
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
526 color: asGPUArray(newSeqWith(6, vec4(0.1, 0.1, 0.1, 1)), VertexBuffer),
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
527 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
528 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
529 assignBuffers(renderdata, floor)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
530
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
531 var uniforms1 = asDescriptorSetData(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
532 Uniforms(data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
533 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
534 assignBuffers(renderdata, uniforms1)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
535
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
536 renderdata.flushAllMemory()
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
537
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
538 var pipeline = createPipeline[CubeShader](renderPass = vulkan.swapchain.renderPass)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
539 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
540
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
541 var tStart = getMonoTime()
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
542 var t = tStart
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
543
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
544 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
545 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
1233
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
546 let tStartLoop = getMonoTime() - tStart
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
547
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
548 uniforms1.data.data.data.mvp = (
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
549 projection(-PI / 2, getAspectRatio(), 0.01, 100) * translate(0, 0, 2) *
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
550 rotate(PI / 4, X) * rotate(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
551 PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
552 )
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
553 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
554 updateGPUBuffer(uniforms1.data.data, flush = true)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
555
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
556 withNextFrame(framebuffer, commandbuffer):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
557 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
558 vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
559 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
560 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
561 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
562 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
563 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
564 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
565 withPipeline(commandbuffer, pipeline):
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
566 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
567 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
568 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor)
1233
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
569
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
570 let tEndLoop = getMonoTime() - tStart
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
571 let looptime = tEndLoop - tStartLoop
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
572 let waitTime = 16_666 - looptime.inMicroseconds
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
573 if waitTime > 0:
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
574 sleep((waitTime / 1000).int)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
575
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
576 # cleanup
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
577 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
578 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
579 destroyRenderData(renderdata)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
580
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
581 proc test_06_different_draw_modes(time: float32) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
582 var renderdata = initRenderData()
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
583
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
584 type
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
585 Shader = object
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
586 position {.VertexAttribute.}: Vec3f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
587 color {.VertexAttribute.}: Vec3f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
588 fragmentColor {.Pass.}: Vec3f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
589 outColor {.ShaderOutput.}: Vec4f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
590 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
591 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
592 """void main() {
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
593 gl_PointSize = 100;
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
594 fragmentColor = color;
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
595 gl_Position = vec4(position, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
596 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
597 """void main() {
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
598 outColor = vec4(fragmentColor, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
599
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
600 TriangleMesh = object
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
601 position: GPUArray[Vec3f, VertexBuffer]
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
602 color: GPUArray[Vec3f, VertexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
603
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
604 var triangle = TriangleMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
605 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
606 [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
607 ),
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
608 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer),
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
609 )
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
610 var lines = TriangleMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
611 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
612 [vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
613 VertexBuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
614 ),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
615 color: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
616 [vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
617 ),
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
618 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
619 assignBuffers(renderdata, triangle)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
620 assignBuffers(renderdata, lines)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
621 renderdata.flushAllMemory()
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
622
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
623 var pipeline1 = createPipeline[Shader](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
624 renderPass = vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
625 polygonMode = VK_POLYGON_MODE_LINE,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
626 lineWidth = 20'f32,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
627 )
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
628 var pipeline2 = createPipeline[Shader](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
629 renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
630 )
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
631 var pipeline3 = createPipeline[Shader](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
632 renderPass = vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
633 topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
634 lineWidth = 5,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
635 )
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
636 var pipeline4 = createPipeline[Shader](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
637 renderPass = vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
638 topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
639 )
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
640
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
641 var start = getMonoTime()
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
642 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: 1280
diff changeset
643 withNextFrame(framebuffer, commandbuffer):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
644 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
645 vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
646 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
647 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
648 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
649 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
650 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
651 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
652 withPipeline(commandbuffer, pipeline1):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
653 render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
654 withPipeline(commandbuffer, pipeline2):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
655 render(commandbuffer = commandbuffer, pipeline = pipeline2, mesh = triangle)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
656 withPipeline(commandbuffer, pipeline3):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
657 render(commandbuffer = commandbuffer, pipeline = pipeline3, mesh = lines)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
658 withPipeline(commandbuffer, pipeline4):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
659 render(commandbuffer = commandbuffer, pipeline = pipeline4, mesh = lines)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
660
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
661 # cleanup
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
662 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
663 destroyPipeline(pipeline1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
664 destroyPipeline(pipeline2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
665 destroyPipeline(pipeline3)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
666 destroyPipeline(pipeline4)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
667 destroyRenderData(renderdata)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
668
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
669 proc test_08_texture_array(time: float32) =
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
670 var renderdata = initRenderData()
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
671
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
672 type
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
673 Uniforms = object
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
674 textures: ImageArray[BGRA]
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
675
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
676 Shader = object
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
677 position {.VertexAttribute.}: Vec3f
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
678 uv {.VertexAttribute.}: Vec2f
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
679 fragmentUv {.Pass.}: Vec2f
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
680 outColor {.ShaderOutput.}: Vec4f
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
681 descriptorSets {.DescriptorSet: 0.}: Uniforms
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
682 # code
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
683 vertexCode: string =
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
684 """
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
685 void main() {
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
686 fragmentUv = uv;
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
687 gl_Position = vec4(position, 1);
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
688 }"""
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
689 fragmentCode: string =
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
690 """
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
691 void main() {
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
692 vec4 col1 = texture(textures, vec3(fragmentUv, 0));
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
693 vec4 col2 = texture(textures, vec3(fragmentUv, 1));
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
694 float w = length(fragmentUv * 2 - 1) / 1.41421;
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
695 outColor = (1 - w) * col1 + w * col2;
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
696 }"""
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
697
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
698 Quad = object
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
699 position: GPUArray[Vec3f, VertexBuffer]
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
700 uv: GPUArray[Vec2f, VertexBuffer]
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
701
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
702 var mesh = Quad(
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
703 position: asGPUArray(
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
704 [
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
705 vec3(-0.8, -0.5),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
706 vec3(-0.8, 0.5),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
707 vec3(0.8, 0.5),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
708 vec3(0.8, 0.5),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
709 vec3(0.8, -0.5),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
710 vec3(-0.8, -0.5),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
711 ],
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
712 VertexBuffer,
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
713 ),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
714 uv: asGPUArray(
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
715 [vec2(0, 1), vec2(0, 0), vec2(1, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1)],
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
716 VertexBuffer,
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
717 ),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
718 )
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
719 assignBuffers(renderdata, mesh)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
720 renderdata.flushAllMemory()
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
721
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
722 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
723 var uniforms1 = asDescriptorSetData(
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
724 Uniforms(textures: loadImageArray[BGRA](["art.png", "art1.png"]))
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
725 )
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
726 uploadImages(renderdata, uniforms1)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
727 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
728
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
729 var start = getMonoTime()
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
730 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
731 withNextFrame(framebuffer, commandbuffer):
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
732 withRenderPass(
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
733 vulkan.swapchain.renderPass,
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
734 framebuffer,
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
735 commandbuffer,
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
736 vulkan.swapchain.width,
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
737 vulkan.swapchain.height,
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
738 vec4(0, 0, 0, 0),
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
739 ):
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
740 withPipeline(commandbuffer, pipeline):
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
741 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
742 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
743
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
744 # cleanup
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
745 checkVkResult vkDeviceWaitIdle(vulkan.device)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
746 destroyPipeline(pipeline)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
747 destroyRenderData(renderdata)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
748
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
749 proc test_07_png_texture(time: float32) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
750 var renderdata = initRenderData()
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
751
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
752 type
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
753 Uniforms = object
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
754 texture1: Image[BGRA]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
755
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
756 Shader = object
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
757 position {.VertexAttribute.}: Vec3f
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
758 uv {.VertexAttribute.}: Vec2f
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
759 fragmentUv {.Pass.}: Vec2f
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
760 outColor {.ShaderOutput.}: Vec4f
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
761 descriptorSets {.DescriptorSet: 0.}: Uniforms
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
762 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
763 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
764 """
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
765 void main() {
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
766 fragmentUv = uv;
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
767 gl_Position = vec4(position, 1);
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
768 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
769 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
770 """
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
771 void main() {
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
772 outColor = texture(texture1, fragmentUv);
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
773 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
774
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
775 Quad = object
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
776 position: GPUArray[Vec3f, VertexBuffer]
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
777 uv: GPUArray[Vec2f, VertexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
778
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
779 var mesh = Quad(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
780 position: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
781 [
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
782 vec3(-0.8, -0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
783 vec3(-0.8, 0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
784 vec3(0.8, 0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
785 vec3(0.8, 0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
786 vec3(0.8, -0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
787 vec3(-0.8, -0.5),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
788 ],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
789 VertexBuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
790 ),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
791 uv: asGPUArray(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
792 [vec2(0, 1), vec2(0, 0), vec2(1, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1)],
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
793 VertexBuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
794 ),
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
795 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
796 assignBuffers(renderdata, mesh)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
797 renderdata.flushAllMemory()
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
798
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
799 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass)
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
800 var uniforms1 = asDescriptorSetData(Uniforms(texture1: loadImage[BGRA]("art.png")))
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
801 uploadImages(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
802 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
803
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
804 var start = getMonoTime()
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
805 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: 1280
diff changeset
806 withNextFrame(framebuffer, commandbuffer):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
807 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
808 vulkan.swapchain.renderPass,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
809 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
810 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
811 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
812 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
813 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
814 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
815 withPipeline(commandbuffer, pipeline):
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
816 bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
817 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
818
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
819 # cleanup
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
820 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
821 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
822 destroyRenderData(renderdata)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
823
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
824 proc test_09_triangle_2pass(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
825 time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
826 ) =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
827 var (offscreenRP, presentRP) =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
828 createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
829
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
830 setupSwapchain(renderpass = presentRP)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
831
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
832 var renderdata = initRenderData()
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
833
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
834 type
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
835 Uniforms = object
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
836 frameTexture: Image[BGRA]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
837
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
838 TriangleShader = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
839 position {.VertexAttribute.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
840 color {.VertexAttribute.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
841 fragmentColor {.Pass.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
842 outColor {.ShaderOutput.}: Vec4f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
843 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
844 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
845 """void main() {
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
846 fragmentColor = color;
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
847 gl_Position = vec4(position, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
848 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
849 """void main() {
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
850 outColor = vec4(fragmentColor, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
851
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
852 PresentShader = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
853 position {.VertexAttribute.}: Vec2f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
854 uv {.Pass.}: Vec2f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
855 outColor {.ShaderOutput.}: Vec4f
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
856 descriptorSets {.DescriptorSet: 0.}: Uniforms
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
857 # code
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
858 vertexCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
859 """void main() {
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
860 uv = ((position + 1) * 0.5) * vec2(1, -1);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
861 gl_Position = vec4(position, 0, 1);}"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
862 fragmentCode: string =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
863 """void main() {
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
864 vec2 uv1 = uv + vec2(0.001, 0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
865 vec2 uv2 = uv + vec2(0.001, -0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
866 vec2 uv3 = uv + vec2(-0.001, 0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
867 vec2 uv4 = uv + vec2(-0.001, -0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
868 outColor = (
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
869 texture(frameTexture, uv1) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
870 texture(frameTexture, uv2) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
871 texture(frameTexture, uv3) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
872 texture(frameTexture, uv4)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
873 ) / 4;
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
874 }"""
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
875
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
876 TriangleMesh = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
877 position: GPUArray[Vec3f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
878 color: GPUArray[Vec3f, VertexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
879
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
880 QuadMesh = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
881 position: GPUArray[Vec2f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
882 indices: GPUArray[uint16, IndexBuffer]
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
883
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
884 var mesh = TriangleMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
885 position:
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
886 asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer),
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
887 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer),
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
888 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
889 var quad = QuadMesh(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
890 position:
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
891 asGPUArray([vec2(-1, -1), vec2(-1, 1), vec2(1, 1), vec2(1, -1)], VertexBuffer),
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
892 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
893 )
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
894 var uniforms1 = asDescriptorSetData(
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
895 Uniforms(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
896 frameTexture: Image[BGRA](
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
897 width: vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
898 height: vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
899 isRenderTarget: true,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
900 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
901 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
902 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
903 assignBuffers(renderdata, mesh)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
904 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
905 uploadImages(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
906 renderdata.flushAllMemory()
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
907
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
908 var
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
909 drawPipeline = createPipeline[TriangleShader](renderPass = offscreenRP)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
910 presentPipeline = createPipeline[PresentShader](renderPass = presentRP)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
911
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
912 initDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms1)
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
913
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
914 # create depth buffer images (will not use the one in the swapchain
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
915 var
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
916 depthImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
917 depthImageView: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
918 depthMemory: VkDeviceMemory
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
919 if offscreenRP.depthBuffer:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
920 depthImage = svkCreate2DImage(
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
921 width = vulkan.swapchain.width,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
922 height = vulkan.swapchain.height,
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
923 format = DEPTH_FORMAT,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
924 usage = [VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
925 samples = offscreenRP.samples,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
926 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
927 let requirements = svkGetImageMemoryRequirements(depthImage)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
928 depthMemory = svkAllocateMemory(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
929 requirements.size, bestMemory(mappable = false, filter = requirements.memoryTypes)
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
930 )
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
931 checkVkResult vkBindImageMemory(vulkan.device, depthImage, depthMemory, 0)
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
932 depthImageView = svkCreate2DImageView(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
933 image = depthImage, format = DEPTH_FORMAT, aspect = VK_IMAGE_ASPECT_DEPTH_BIT
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
934 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
935
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
936 # create msaa images (will not use the one in the swapchain
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
937 var
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
938 msaaImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
939 msaaImageView: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
940 msaaMemory: VkDeviceMemory
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
941 if offscreenRP.samples != VK_SAMPLE_COUNT_1_BIT:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
942 msaaImage = svkCreate2DImage(
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
943 width = vulkan.swapchain.width,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
944 height = vulkan.swapchain.height,
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
945 format = SURFACE_FORMAT,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
946 usage = [VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT],
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
947 samples = offscreenRP.samples,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
948 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
949 let requirements = svkGetImageMemoryRequirements(msaaImage)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
950 msaaMemory = svkAllocateMemory(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
951 requirements.size, bestMemory(mappable = false, filter = requirements.memoryTypes)
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
952 )
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
953 checkVkResult vkBindImageMemory(vulkan.device, msaaImage, msaaMemory, 0)
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
954 msaaImageView = svkCreate2DImageView(image = msaaImage, format = SURFACE_FORMAT)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
955
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
956 var attachments: seq[VkImageView]
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
957 if offscreenRP.samples == VK_SAMPLE_COUNT_1_BIT:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
958 if offscreenRP.depthBuffer:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
959 attachments = @[uniforms1.data.frameTexture.imageview, depthImageView]
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
960 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
961 attachments = @[uniforms1.data.frameTexture.imageview]
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
962 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
963 if offscreenRP.depthBuffer:
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
964 attachments =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
965 @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview]
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
966 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
967 attachments = @[msaaImageView, uniforms1.data.frameTexture.imageview]
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
968 var offscreenFB = svkCreateFramebuffer(
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
969 offscreenRP.vk, vulkan.swapchain.width, vulkan.swapchain.height, attachments
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
970 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
971
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
972 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
973 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: 1280
diff changeset
974 withNextFrame(framebuffer, commandbuffer):
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
975 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
976 offscreenRP,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
977 offscreenFB,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
978 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
979 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
980 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
981 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
982 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
983 withPipeline(commandbuffer, drawPipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
984 render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
985
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
986 withRenderPass(
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
987 presentRP,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
988 framebuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
989 commandbuffer,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
990 vulkan.swapchain.width,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
991 vulkan.swapchain.height,
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
992 vec4(0, 0, 0, 0),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
993 ):
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
994 withPipeline(commandbuffer, presentPipeline):
1322
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
995 bindDescriptorSet(commandbuffer, uniforms1, 0, presentPipeline)
4a1c2b1128bc did: improve handling of descriptor sets
sam <sam@basx.dev>
parents: 1299
diff changeset
996 render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
997
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
998 # cleanup
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
999 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1000 destroyPipeline(presentPipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1001 destroyPipeline(drawPipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1002 destroyRenderData(renderdata)
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1003 if depthImage.Valid:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1004 vkDestroyImageView(vulkan.device, depthImageView, nil)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1005 vkDestroyImage(vulkan.device, depthImage, nil)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1006 vkFreeMemory(vulkan.device, depthMemory, nil)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1007 if msaaImage.Valid:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1008 vkDestroyImageView(vulkan.device, msaaImageView, nil)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1009 vkDestroyImage(vulkan.device, msaaImage, nil)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1010 vkFreeMemory(vulkan.device, msaaMemory, nil)
1285
6d16003406fb add: missing destroy-function for renderpass
sam <sam@basx.dev>
parents: 1283
diff changeset
1011 destroyRenderPass(offscreenRP)
6d16003406fb add: missing destroy-function for renderpass
sam <sam@basx.dev>
parents: 1283
diff changeset
1012 destroyRenderPass(presentRP)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
1013 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1014 clearSwapchain()
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
1015
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
1016 when isMainModule:
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
1017 var time = 1'f32
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1018 initVulkan()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
1019
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1020 var mainRenderpass: RenderPass
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1021 var renderPasses = [
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
1022 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
1023 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
1024 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT),
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
1025 (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT),
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
1026 ]
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
1027
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
1028 # test normal
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
1029 for i, (depthBuffer, samples) in renderPasses:
1332
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
1030 var renderpass =
df3c075e5dea did: formatting
sam <sam@basx.dev>
parents: 1322
diff changeset
1031 createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1032 setupSwapchain(renderpass = renderpass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
1033
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
1034 # tests a simple triangle with minimalistic shader and vertex format
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1035 # test_01_triangle(time)
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
1036
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
1037 # tests instanced triangles and quads, mixing meshes and instances
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1038 # test_02_triangle_quad_instanced(time)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
1039
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
1040 # teste descriptor sets
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1041 # test_03_simple_descriptorset(time)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
1042
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
1043 # tests multiple descriptor sets and arrays
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1044 # test_04_multiple_descriptorsets(time)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
1045
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
1046 # rotating cube
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1047 # test_05_cube(time)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
1048
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
1049 # different draw modes (lines, points, and topologies)
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1050 # test_06_different_draw_modes(time)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
1051
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1052 # test_07_png_texture(time)
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1053
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1054 test_08_texture_array(time)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
1055
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
1056 checkVkResult vkDeviceWaitIdle(vulkan.device)
1285
6d16003406fb add: missing destroy-function for renderpass
sam <sam@basx.dev>
parents: 1283
diff changeset
1057 destroyRenderPass(renderpass)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1058 clearSwapchain()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
1059
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
1060 # test multiple render passes
1334
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1061 # for i, (depthBuffer, samples) in renderPasses:
d5a6f69dc855 add: support for multi-layer images
sam <sam@basx.dev>
parents: 1332
diff changeset
1062 # test_09_triangle_2pass(time, depthBuffer, samples)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
1063
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
1064 destroyVulkan()