annotate tests/test_rendering.nim @ 1306:7be3628298f5

did: reworked locked mouse
author sam <sam@basx.dev>
date Wed, 07 Aug 2024 20:58:11 +0700
parents 6d0162bfe48a
children 4a1c2b1128bc
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
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
17 Shader = object
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
18 position {.VertexAttribute.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
19 color {.VertexAttribute.}: Vec3f
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
20 pushConstant {.PushConstantAttribute.}: PushConstant
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
21 fragmentColor {.Pass.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
22 outColor {.ShaderOutput.}: Vec4f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
23 # code
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
24 vertexCode: string = """void main() {
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
25 fragmentColor = color;
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
26 gl_Position = vec4(position * pushConstant.scale, 1);}"""
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
27 fragmentCode: string = """void main() {
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
28 outColor = vec4(fragmentColor, 1);}"""
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
29 TriangleMesh = object
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
30 position: GPUArray[Vec3f, VertexBuffer]
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
31 color: GPUArray[Vec3f, VertexBuffer]
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
32 var mesh = TriangleMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
33 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
34 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
35 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
36 assignBuffers(renderdata, mesh)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
37 renderdata.flushAllMemory()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
38
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
39 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
40
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
41 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
42 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
43
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
44 withNextFrame(framebuffer, commandbuffer):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
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 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
47
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
48 withPipeline(commandbuffer, pipeline):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
49
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
50 renderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh, pushConstant = PushConstant(scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000)))
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
51
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
52 # cleanup
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
53 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
54 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
55 destroyRenderData(renderdata)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
56
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
57
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
58 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
59 var renderdata = initRenderData()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
60
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
61 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
62 SomeShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
63 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
64 color {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
65 pos {.InstanceAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
66 scale {.InstanceAttribute.}: float32
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
67 fragmentColor {.Pass.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
68 outColor {.ShaderOutput.}: Vec4f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
69 # code
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
70 vertexCode: string = """void main() {
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
71 fragmentColor = color;
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
72 gl_Position = vec4((position * scale) + pos, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
73 fragmentCode: string = """void main() {
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
74 outColor = vec4(fragmentColor, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
75 TriangleMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
76 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
77 color: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
78 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
79 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
80 color: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
81 indices: GPUArray[uint16, IndexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
82 Instances = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
83 pos: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
84 scale: GPUArray[float32, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
85 var tri = TriangleMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
86 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
87 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
88 )
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
89 var quad = QuadMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
90 position: asGPUArray([vec3(-0.3, -0.3, 0), vec3(-0.3, 0.3, 0), vec3(0.3, 0.3, 0), vec3(0.3, -0.3, 0)], VertexBuffer),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
91 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
92 color: asGPUArray([vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1)], VertexBuffer),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
93 )
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
94
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
95 var instancesA: Instances
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
96 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
97 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
98 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
99 var instancesB: Instances
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
100 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
101 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
102 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
103
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
104 assignBuffers(renderdata, tri)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
105 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
106 assignBuffers(renderdata, instancesA)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
107 assignBuffers(renderdata, instancesB)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
108 renderdata.flushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
109
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
110 var pipeline = createPipeline[SomeShader](renderPass = vulkan.swapchain.renderPass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
111
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
112 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
113 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
114
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
115 withNextFrame(framebuffer, commandbuffer):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
116
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
117 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
118
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
119 withPipeline(commandbuffer, pipeline):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
120
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
121 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
122 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
123 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
124 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
125
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
126 # cleanup
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
127 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
128 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
129 destroyRenderData(renderdata)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
130
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
131 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
132 var renderdata = initRenderData()
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
133
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
134 type
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
135 Material = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
136 baseColor: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
137
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
138 Uniforms = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
139 material: GPUValue[Material, UniformBuffer]
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
140 texture1: Image[BGRA]
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
141
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
142 QuadShader = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
143 position {.VertexAttribute.}: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
144 fragmentColor {.Pass.}: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
145 uv {.Pass.}: Vec2f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
146 outColor {.ShaderOutput.}: Vec4f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
147 descriptorSets {.DescriptorSets.}: (Uniforms, )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
148 # code
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
149 vertexCode: string = """void main() {
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
150 fragmentColor = material.baseColor;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
151 gl_Position = vec4(position, 1);
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
152 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
153 uv = position.xy + 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
154 }"""
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
155 fragmentCode: string = """void main() {
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
156 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}"""
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
157 QuadMesh = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
158 position: GPUArray[Vec3f, VertexBuffer]
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
159 indices: GPUArray[uint16, IndexBuffer]
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
160
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
161 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
162 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
163 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
164 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
165 var
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
166 quad = QuadMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
167 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(-0.5, 0.5, 0), vec3(0.5, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
168 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
169 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
170 uniforms1 = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
171 Uniforms(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
172 material: asGPUValue(Material(baseColor: vec3(1, 1, 1)), UniformBuffer),
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
173 texture1: Image[BGRA](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
174 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
175 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
176 uniforms2 = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
177 Uniforms(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
178 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
179 texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]),
1212
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1211
diff changeset
180 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
181 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
182
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
183 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
184 assignBuffers(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
185 assignBuffers(renderdata, uniforms2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
186 uploadImages(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
187 uploadImages(renderdata, uniforms2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
188 renderdata.flushAllMemory()
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
189
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
190 var pipeline = createPipeline[QuadShader](renderPass = vulkan.swapchain.renderPass)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
191
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
192 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
193 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
194
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
195 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
196 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
197
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
198 withNextFrame(framebuffer, commandbuffer):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
199
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
200 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
201
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
202 withPipeline(commandbuffer, pipeline):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
203
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
204 withBind(commandbuffer, (uniforms1, ), pipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
205 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
206
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
207 withBind(commandbuffer, (uniforms2, ), pipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
208 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
209
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
210 # cleanup
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
211 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
212 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
213 destroyRenderData(renderdata)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
214
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
215 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
216 var renderdata = initRenderData()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
217
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
218 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
219 RenderSettings = object
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
220 brigthness: float32
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
221 Material = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
222 baseColor: Vec3f
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
223 ObjectSettings = object
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
224 scale: float32
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
225 materialIndex: uint32
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
226 Constants = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
227 offset: Vec2f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
228
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
229 ConstSet = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
230 constants: GPUValue[Constants, UniformBuffer]
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
231 MainSet = object
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
232 renderSettings: GPUValue[RenderSettings, UniformBufferMapped]
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
233 material: array[2, GPUValue[Material, UniformBuffer]]
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
234 texture1: array[2, Image[Gray]]
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
235 OtherSet = object
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
236 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped]
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
237
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
238 QuadShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
239 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
240 fragmentColor {.Pass.}: Vec3f
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
241 uv {.Pass.}: Vec2f
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
242 outColor {.ShaderOutput.}: Vec4f
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
243 descriptorSets {.DescriptorSets.}: (ConstSet, MainSet, OtherSet)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
244 # code
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
245 vertexCode: string = """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
246 fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
247 gl_Position = vec4(position * objectSettings.scale, 1);
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
248 gl_Position.xy += constants.offset.xy;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
249 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
250 uv = position.xy + 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
251 }"""
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
252 fragmentCode: string = """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
253 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
254 }"""
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
255 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
256 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
257 indices: GPUArray[uint16, IndexBuffer]
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
258
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
259 var quad = QuadMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
260 position: asGPUArray([vec3(-0.5, -0.5), vec3(-0.5, 0.5), vec3(0.5, 0.5), vec3(0.5, -0.5)], VertexBuffer),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
261 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
262 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
263 var constset = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
264 ConstSet(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
265 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
266 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
267 )
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
268 let G = Gray([50'u8])
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
269 let W = Gray([255'u8])
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
270 var mainset = asDescriptorSet(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
271 MainSet(
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
272 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
273 material: [
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
274 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
275 asGPUValue(Material(baseColor: vec3(1, 0, 1)), UniformBuffer),
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 texture1: [
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
278 Image[Gray](width: 2, height: 2, data: @[W, G, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST),
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
279 Image[Gray](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
280 ],
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
281 ),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
282 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
283 var otherset1 = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
284 OtherSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
285 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped),
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
286 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
287 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
288 var otherset2 = asDescriptorSet(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
289 OtherSet(
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
290 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped),
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
291 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
292 )
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
293
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
294 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
295 assignBuffers(renderdata, constset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
296 assignBuffers(renderdata, mainset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
297 assignBuffers(renderdata, otherset1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
298 assignBuffers(renderdata, otherset2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
299 uploadImages(renderdata, mainset)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
300 renderdata.flushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
301
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
302 var pipeline = createPipeline[QuadShader](renderPass = vulkan.swapchain.renderPass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
303
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
304 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
305 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
306 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
307 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
308
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
309 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
310 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
311
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
312 withNextFrame(framebuffer, commandbuffer):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
313
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
314 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
315
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
316 withPipeline(commandbuffer, pipeline):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
317
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
318 withBind(commandbuffer, (constset, mainset, otherset1), pipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
319 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
320
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
321 withBind(commandbuffer, (constset, mainset, otherset2), pipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
322 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
323
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
324 mainset.data.renderSettings.data.brigthness = ((getMonoTime() - start).inMilliseconds().int / 1000) / time
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
325 otherset1.data.objectSettings.data.scale = 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
326 updateGPUBuffer(mainset.data.renderSettings)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
327 updateGPUBuffer(otherset1.data.objectSettings)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
328 renderdata.flushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
329
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
330 # cleanup
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
331 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
332 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
333 destroyRenderData(renderdata)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
334
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
335 proc test_05_cube(time: float32) =
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
336 type
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
337
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
338 UniformData = object
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
339 mvp: Mat4
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
340 Uniforms = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
341 data: GPUValue[UniformData, UniformBufferMapped]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
342 CubeShader = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
343 position {.VertexAttribute.}: Vec3f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
344 color {.VertexAttribute.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
345 fragmentColor {.Pass.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
346 outColor {.ShaderOutput.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
347 descriptorSets {.DescriptorSets.}: (Uniforms, )
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
348 # code
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
349 vertexCode = """void main() {
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
350 fragmentColor = color;
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
351 gl_Position = vec4(position, 1) * data.mvp;
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
352 }"""
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
353 fragmentCode = """void main() {
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
354 outColor = fragmentColor;
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
355 }"""
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
356 Mesh = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
357 position: GPUArray[Vec3f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
358 normals: GPUArray[Vec3f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
359 color: GPUArray[Vec4f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
360
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
361 let quad = @[
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
362 vec3(-0.5, -0.5), vec3(-0.5, +0.5), vec3(+0.5, +0.5),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
363 vec3(+0.5, +0.5), vec3(+0.5, -0.5), vec3(-0.5, -0.5),
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
364 ]
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
365 proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] =
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
366 for v in data:
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
367 result.add mat * v
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
368
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
369 var
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
370 vertices: seq[Vec3f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
371 colors: seq[Vec4f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
372 normals: seq[Vec3f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
373
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
374 # front, red
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
375 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
376 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
377 normals.add newSeqWith(6, vec3(0, 0, -1))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
378
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
379 # back, cyan
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
380 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
381 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
382 normals.add newSeqWith(6, vec3(0, 0, 1))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
383
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
384 # right, green
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
385 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
386 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
387 normals.add newSeqWith(6, vec3(-1, 0, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
388
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
389 # left, magenta
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
390 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
391 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
392 normals.add newSeqWith(6, vec3(1, 0, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
393
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
394 # bottom, blue
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
395 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
396 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
397 normals.add newSeqWith(6, vec3(0, -1, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
398
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
399 # top, yellow
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
400 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
401 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
402 normals.add newSeqWith(6, vec3(0, 1, 0))
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
403
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
404 var renderdata = initRenderData()
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
405
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
406 var mesh = Mesh(
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
407 position: asGPUArray(vertices, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
408 color: asGPUArray(colors, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
409 normals: asGPUArray(normals, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
410 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
411 assignBuffers(renderdata, mesh)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
412
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
413 var floor = Mesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
414 position: asGPUArray(quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), VertexBuffer),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
415 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
416 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
417 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
418 assignBuffers(renderdata, floor)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
419
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
420 var uniforms1 = asDescriptorSet(
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
421 Uniforms(
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
422 data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
423 )
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
424 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
425 assignBuffers(renderdata, uniforms1)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
426
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
427 renderdata.flushAllMemory()
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
428
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
429 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
430 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
431
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
432 var tStart = getMonoTime()
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
433 var t = tStart
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
434
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
435 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
436 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
437 let tStartLoop = getMonoTime() - tStart
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
438
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
439 uniforms1.data.data.data.mvp = (
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
440 projection(-PI / 2, getAspectRatio(), 0.01, 100) *
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
441 translate(0, 0, 2) *
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
442 rotate(PI / 4, X) *
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
443 rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y)
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
444 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
445 updateGPUBuffer(uniforms1.data.data, flush = true)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
446
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
447 withNextFrame(framebuffer, commandbuffer):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
448
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
449 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
450 withPipeline(commandbuffer, pipeline):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
451
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
452 withBind(commandbuffer, (uniforms1, ), pipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
453 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
454 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
455
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
456 let tEndLoop = getMonoTime() - tStart
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
457 let looptime = tEndLoop - tStartLoop
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
458 let waitTime = 16_666 - looptime.inMicroseconds
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
459 if waitTime > 0:
1cf698973dca did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents: 1232
diff changeset
460 sleep((waitTime / 1000).int)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
461
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
462 # cleanup
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
463 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
464 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
465 destroyRenderData(renderdata)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
466
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
467 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
468 var renderdata = initRenderData()
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
469
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
470 type
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
471 Shader = object
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
472 position {.VertexAttribute.}: Vec3f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
473 color {.VertexAttribute.}: Vec3f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
474 fragmentColor {.Pass.}: Vec3f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
475 outColor {.ShaderOutput.}: Vec4f
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
476 # code
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
477 vertexCode: string = """void main() {
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
478 gl_PointSize = 100;
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
479 fragmentColor = color;
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
480 gl_Position = vec4(position, 1);}"""
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
481 fragmentCode: string = """void main() {
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
482 outColor = vec4(fragmentColor, 1);}"""
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
483 TriangleMesh = object
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
484 position: GPUArray[Vec3f, VertexBuffer]
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
485 color: GPUArray[Vec3f, VertexBuffer]
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
486 var triangle = TriangleMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
487 position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
488 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
489 )
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
490 var lines = TriangleMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
491 position: asGPUArray([vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], VertexBuffer),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
492 color: asGPUArray([vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer),
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
493 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
494 assignBuffers(renderdata, triangle)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
495 assignBuffers(renderdata, lines)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
496 renderdata.flushAllMemory()
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
497
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
498 var pipeline1 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_LINE, lineWidth = 20'f32)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
499 var pipeline2 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
500 var pipeline3 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, lineWidth = 5)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
501 var pipeline4 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
502
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
503 var start = getMonoTime()
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
504 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
505 withNextFrame(framebuffer, commandbuffer):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
506 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
507 withPipeline(commandbuffer, pipeline1):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
508 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
509 withPipeline(commandbuffer, pipeline2):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
510 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
511 withPipeline(commandbuffer, pipeline3):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
512 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
513 withPipeline(commandbuffer, pipeline4):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
514 render(commandbuffer = commandbuffer, pipeline = pipeline4, mesh = lines)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
515
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
516 # cleanup
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
517 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
518 destroyPipeline(pipeline1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
519 destroyPipeline(pipeline2)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
520 destroyPipeline(pipeline3)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
521 destroyPipeline(pipeline4)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
522 destroyRenderData(renderdata)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
523
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
524 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
525 var renderdata = initRenderData()
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
526
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
527 type
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
528 Uniforms = object
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
529 texture1: Image[BGRA]
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
530 Shader = object
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
531 position {.VertexAttribute.}: Vec3f
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
532 uv {.VertexAttribute.}: Vec2f
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
533 fragmentUv {.Pass.}: Vec2f
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
534 outColor {.ShaderOutput.}: Vec4f
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
535 descriptorSets {.DescriptorSets.}: (Uniforms, )
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
536 # code
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
537 vertexCode: string = """
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
538 void main() {
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
539 fragmentUv = uv;
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
540 gl_Position = vec4(position, 1);
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
541 }"""
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
542 fragmentCode: string = """
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
543 void main() {
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
544 outColor = texture(texture1, fragmentUv);
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
545 }"""
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
546 Quad = object
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
547 position: GPUArray[Vec3f, VertexBuffer]
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
548 uv: GPUArray[Vec2f, VertexBuffer]
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
549 var mesh = Quad(
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
550 position: asGPUArray([
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
551 vec3(-0.8, -0.5), vec3(-0.8, 0.5), vec3(0.8, 0.5),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
552 vec3(0.8, 0.5), vec3(0.8, -0.5), vec3(-0.8, -0.5),
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
553 ], VertexBuffer),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
554 uv: asGPUArray([
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
555 vec2(0, 1), vec2(0, 0), vec2(1, 0),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
556 vec2(1, 0), vec2(1, 1), vec2(0, 1),
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
557 ], VertexBuffer),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
558 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
559 assignBuffers(renderdata, mesh)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
560 renderdata.flushAllMemory()
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
561
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
562 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
563 var uniforms1 = asDescriptorSet(
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
564 Uniforms(
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
565 texture1: loadImage[BGRA]("art.png"),
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
566 )
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
567 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
568 uploadImages(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
569 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
570
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
571 var start = getMonoTime()
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
572 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
573
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
574 withNextFrame(framebuffer, commandbuffer):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
575
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
576 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
577
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
578 withPipeline(commandbuffer, pipeline):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
579
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
580 withBind(commandbuffer, (uniforms1, ), pipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
581 render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
582
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
583 # cleanup
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
584 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
585 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
586 destroyRenderData(renderdata)
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
587
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
588 proc test_08_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
589 var (offscreenRP, presentRP) = 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
590
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
591 setupSwapchain(renderpass = presentRP)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
592
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
593 var renderdata = initRenderData()
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
594
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
595 type
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
596 Uniforms = object
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
597 frameTexture: Image[BGRA]
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
598 TriangleShader = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
599 position {.VertexAttribute.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
600 color {.VertexAttribute.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
601 fragmentColor {.Pass.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
602 outColor {.ShaderOutput.}: Vec4f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
603 # code
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
604 vertexCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
605 fragmentColor = color;
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
606 gl_Position = vec4(position, 1);}"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
607 fragmentCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
608 outColor = vec4(fragmentColor, 1);}"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
609 PresentShader = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
610 position {.VertexAttribute.}: Vec2f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
611 uv {.Pass.}: Vec2f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
612 outColor {.ShaderOutput.}: Vec4f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
613 descriptorSets {.DescriptorSets.}: (Uniforms, )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
614 # code
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
615 vertexCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
616 uv = ((position + 1) * 0.5) * vec2(1, -1);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
617 gl_Position = vec4(position, 0, 1);}"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
618 fragmentCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
619 vec2 uv1 = uv + vec2(0.001, 0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
620 vec2 uv2 = uv + vec2(0.001, -0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
621 vec2 uv3 = uv + vec2(-0.001, 0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
622 vec2 uv4 = uv + vec2(-0.001, -0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
623 outColor = (
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
624 texture(frameTexture, uv1) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
625 texture(frameTexture, uv2) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
626 texture(frameTexture, uv3) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
627 texture(frameTexture, uv4)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
628 ) / 4;
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
629 }"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
630 TriangleMesh = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
631 position: GPUArray[Vec3f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
632 color: GPUArray[Vec3f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
633 QuadMesh = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
634 position: GPUArray[Vec2f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
635 indices: GPUArray[uint16, IndexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
636 var mesh = TriangleMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
637 position: asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer),
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
638 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
639 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
640 var quad = QuadMesh(
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
641 position: 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
642 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
643 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
644 var uniforms1 = asDescriptorSet(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
645 Uniforms(
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1242
diff changeset
646 frameTexture: Image[BGRA](width: vulkan.swapchain.width, height: vulkan.swapchain.height, isRenderTarget: true),
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
647 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
648 )
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
649 assignBuffers(renderdata, mesh)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
650 assignBuffers(renderdata, quad)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
651 uploadImages(renderdata, uniforms1)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
652 renderdata.flushAllMemory()
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
653
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
654 var
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
655 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
656 presentPipeline = createPipeline[PresentShader](renderPass = presentRP)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
657
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
658 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
659
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
660 # 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
661 var
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
662 depthImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
663 depthImageView: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
664 depthMemory: VkDeviceMemory
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
665 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
666 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
667 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
668 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
669 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
670 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
671 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
672 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
673 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
674 depthMemory = svkAllocateMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
675 requirements.size,
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
676 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
677 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
678 checkVkResult vkBindImageMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
679 vulkan.device,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
680 depthImage,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
681 depthMemory,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
682 0,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
683 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
684 depthImageView = svkCreate2DImageView(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
685 image = depthImage,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
686 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
687 aspect = VK_IMAGE_ASPECT_DEPTH_BIT
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
688 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
689
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
690 # 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
691 var
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
692 msaaImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
693 msaaImageView: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
694 msaaMemory: VkDeviceMemory
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
695 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
696 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
697 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
698 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
699 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
700 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
701 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
702 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
703 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
704 msaaMemory = svkAllocateMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
705 requirements.size,
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
706 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
707 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
708 checkVkResult vkBindImageMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
709 vulkan.device,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
710 msaaImage,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
711 msaaMemory,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
712 0,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
713 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
714 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
715
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
716 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
717 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
718 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
719 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
720 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
721 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
722 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
723 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
724 attachments = @[msaaImageView, depthImageView, 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
725 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
726 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
727 var offscreenFB = svkCreateFramebuffer(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
728 offscreenRP.vk,
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
729 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
730 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
731 attachments
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
732 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
733
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
734 var start = getMonoTime()
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
735 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
736
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
737 withNextFrame(framebuffer, commandbuffer):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
738
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
739 withRenderPass(offscreenRP, offscreenFB, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
740 withPipeline(commandbuffer, drawPipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
741 render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
742
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
743 withRenderPass(presentRP, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
744
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
745 withPipeline(commandbuffer, presentPipeline):
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
746
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
747 withBind(commandbuffer, (uniforms1, ), presentPipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
748 render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
749
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
750 # cleanup
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
751 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
752 destroyPipeline(presentPipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
753 destroyPipeline(drawPipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
754 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
755 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
756 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
757 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
758 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
759 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
760 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
761 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
762 vkFreeMemory(vulkan.device, msaaMemory, nil)
1285
6d16003406fb add: missing destroy-function for renderpass
sam <sam@basx.dev>
parents: 1283
diff changeset
763 destroyRenderPass(offscreenRP)
6d16003406fb add: missing destroy-function for renderpass
sam <sam@basx.dev>
parents: 1283
diff changeset
764 destroyRenderPass(presentRP)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
765 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
766 clearSwapchain()
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
767
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
768 when isMainModule:
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
769 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
770 initVulkan()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
771
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
772 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
773 var renderPasses = [
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
774 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT),
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
775 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT),
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
776 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT),
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
777 (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
778 ]
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
779
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
780
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
781 # test normal
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
782 for i, (depthBuffer, samples) in renderPasses:
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
783 var renderpass = createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
784 setupSwapchain(renderpass = renderpass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
785
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
786 # tests a simple triangle with minimalistic shader and vertex format
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
787 test_01_triangle(time)
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
788
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
789 # tests instanced triangles and quads, mixing meshes and instances
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
790 test_02_triangle_quad_instanced(time)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
791
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
792 # teste descriptor sets
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
793 test_03_simple_descriptorset(time)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
794
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
795 # tests multiple descriptor sets and arrays
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
796 test_04_multiple_descriptorsets(time)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
797
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
798 # rotating cube
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1235
diff changeset
799 test_05_cube(time)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
800
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
801 # different draw modes (lines, points, and topologies)
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
802 test_06_different_draw_modes(time)
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
803
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
804 # load PNG texture
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
805 test_07_png_texture(time)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
806
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
807 checkVkResult vkDeviceWaitIdle(vulkan.device)
1285
6d16003406fb add: missing destroy-function for renderpass
sam <sam@basx.dev>
parents: 1283
diff changeset
808 destroyRenderPass(renderpass)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1280
diff changeset
809 clearSwapchain()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
810
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
811 # test multiple render passes
1235
c70fee6568f6 did: improv render tests to run without user input
sam <sam@basx.dev>
parents: 1233
diff changeset
812 for i, (depthBuffer, samples) in renderPasses:
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents: 1240
diff changeset
813 test_08_triangle_2pass(time, depthBuffer, samples)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
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 destroyVulkan()