annotate tests/test_rendering.nim @ 1209:e177336cac3f compiletime-tests

sync to notebook in bedroom
author sam <sam@basx.dev>
date Tue, 16 Jul 2024 20:39:35 +0700
parents 8cf9a05b03eb
children 570ac1620fb6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
1 import std/options
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
2 import std/random
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
3
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
4 import ../semicongine
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
5
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
6 var
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
7 mainRenderpass: VkRenderPass
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
8 swapchain: Swapchain
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
9
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
10 proc test_01_triangle(nFrames: int) =
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
11 var renderdata = InitRenderData()
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
12
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
13 type
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
14 TrianglShader = object
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
15 position {.VertexAttribute.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
16 color {.VertexAttribute.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
17 fragmentColor {.Pass.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
18 outColor {.ShaderOutput.}: Vec4f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
19 # code
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
20 vertexCode: string = """void main() {
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
21 fragmentColor = color;
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
22 gl_Position = vec4(position, 1);}"""
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
23 fragmentCode: string = """void main() {
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
24 outColor = vec4(fragmentColor, 1);}"""
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
25 TriangleMesh = object
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
26 position: GPUArray[Vec3f, VertexBuffer]
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
27 color: GPUArray[Vec3f, VertexBuffer]
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
28 var mesh = TriangleMesh(
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
29 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
30 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer),
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
31 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
32 AssignBuffers(renderdata, mesh)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
33 renderdata.FlushAllMemory()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
34
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
35 var
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
36 pipeline = CreatePipeline[TrianglShader](renderPass = mainRenderpass, samples = swapchain.samples)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
37
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
38 var c = 0
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
39 while UpdateInputs() and c < nFrames:
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
40 WithNextFrame(swapchain, framebuffer, commandbuffer):
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
41 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
42 WithPipeline(commandbuffer, pipeline):
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
44 inc c
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
45
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
46 # cleanup
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
47 checkVkResult vkDeviceWaitIdle(vulkan.device)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
48 DestroyPipeline(pipeline)
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
49 DestroyRenderData(renderdata)
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
50
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
51
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
52 proc test_02_triangle_quad_instanced(nFrames: int) =
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
53 var renderdata = InitRenderData()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
54
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
55 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
56 SomeShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
57 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
58 color {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
59 pos {.InstanceAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
60 scale {.InstanceAttribute.}: float32
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
61 fragmentColor {.Pass.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
62 outColor {.ShaderOutput.}: Vec4f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
63 # code
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
64 vertexCode: string = """void main() {
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
65 fragmentColor = color;
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
66 gl_Position = vec4((position * scale) + pos, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
67 fragmentCode: string = """void main() {
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
68 outColor = vec4(fragmentColor, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
69 TriangleMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
70 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
71 color: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
72 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
73 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
74 color: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
75 indices: GPUArray[uint16, IndexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
76 Instances = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
77 pos: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
78 scale: GPUArray[float32, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
79 var tri = TriangleMesh(
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
80 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
81 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer),
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
82 )
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
83 var quad = QuadMesh(
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
84 position: asGPUArray([NewVec3f(-0.3, -0.3), NewVec3f(-0.3, 0.3), NewVec3f(0.3, 0.3), NewVec3f(0.3, -0.3)], VertexBuffer),
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
85 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
86 color: asGPUArray([NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1)], VertexBuffer),
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
87 )
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 instancesA: Instances
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
90 for n in 1..100:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
91 instancesA.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32))
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
92 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
93 var instancesB: Instances
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
94 for n in 1..100:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
95 instancesB.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32))
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
96 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
97
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
98 AssignBuffers(renderdata, tri)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
99 AssignBuffers(renderdata, quad)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
100 AssignBuffers(renderdata, instancesA)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
101 AssignBuffers(renderdata, instancesB)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
102 renderdata.FlushAllMemory()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
103
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
104 var pipeline = CreatePipeline[SomeShader](renderPass = mainRenderpass, samples = swapchain.samples)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
105
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
106 var c = 0
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
107 while UpdateInputs() and c < nFrames:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
108 WithNextFrame(swapchain, framebuffer, commandbuffer):
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
109 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
110 WithPipeline(commandbuffer, pipeline):
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
114 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
115 inc c
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
116
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
117 # cleanup
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
118 checkVkResult vkDeviceWaitIdle(vulkan.device)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
119 DestroyPipeline(pipeline)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
120 DestroyRenderData(renderdata)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
121
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
122 proc test_03_global_descriptorset(nFrames: int) =
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
123 var renderdata = InitRenderData()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
124
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
125 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
126 RenderSettings = object
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
127 brigthness: float32
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
128 ObjectSettings = object
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
129 baseColor: Vec3f
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
130 MainSet = object
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
131 renderSettings: GPUValue[RenderSettings, UniformBufferMapped]
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
132 materialSettings: GPUValue[ObjectSettings, UniformBuffer]
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
133 OtherSet = object
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
134 # TODO
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
135
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
136 QuadShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
137 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
138 fragmentColor {.Pass.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
139 outColor {.ShaderOutput.}: Vec4f
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
140 descriptorSets {.DescriptorSets.}: (MainSet, OtherSet)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
141 # code
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
142 vertexCode: string = """void main() {
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
143 fragmentColor = materialSettings.baseColor * renderSettings.brigthness;
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
144 gl_Position = vec4(position, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
145 fragmentCode: string = """void main() {
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
146 outColor = vec4(fragmentColor, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
147 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
148 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
149 indices: GPUArray[uint16, IndexBuffer]
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
150
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
151 var quad = QuadMesh(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
152 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
153 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
154 )
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
155 var mainSet = asDescriptorSet(
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
156 MainSet(
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
157 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped),
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
158 materialSettings: asGPUValue(ObjectSettings(baseColor: NewVec3f(1, 1, 0)), UniformBuffer),
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
159 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
160 )
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
161 var settings = asDescriptorSet(
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
162 OtherSet(
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
163 # TODO
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
164 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
165 )
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
166
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
167 AssignBuffers(renderdata, quad)
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
168 AssignBuffers(renderdata, mainSet)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
169 renderdata.FlushAllMemory()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
170
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
171 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
172
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
173 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], mainSet)
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
174
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
175 var c = 0
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
176 while UpdateInputs() and c < nFrames:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
177 WithNextFrame(swapchain, framebuffer, commandbuffer):
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
178 WithBind(commandbuffer, (mainSet, ), pipeline, swapchain.currentFiF):
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
179 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
180 WithPipeline(commandbuffer, pipeline):
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
181 Render(commandbuffer = commandbuffer, pipeline = pipeline, firstSet = mainSet, mesh = quad)
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
182 mainSet.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32)
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
183 UpdateGPUBuffer(mainSet.data.renderSettings)
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
184 renderdata.FlushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
185 inc c
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
186
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
187 # cleanup
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
188 checkVkResult vkDeviceWaitIdle(vulkan.device)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
189 DestroyPipeline(pipeline)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
190 DestroyRenderData(renderdata)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
191
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
192 when isMainModule:
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
193 var nFrames = 5000
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
194 InitVulkan()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
195
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
196 # test normal
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
197 block:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
198 mainRenderpass = CreatePresentationRenderPass()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
199 swapchain = InitSwapchain(renderpass = mainRenderpass).get()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
200
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
201 # tests a simple triangle with minimalistic shader and vertex format
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
202 # test_01_triangle(nFrames)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
203
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
204 # tests instanced triangles and quads, mixing meshes and instances
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
205 # test_02_triangle_quad_instanced(nFrames)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
206
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
207 # tests
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
208 test_03_global_descriptorset(nFrames)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
209
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
210 checkVkResult vkDeviceWaitIdle(vulkan.device)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
211 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
212 DestroySwapchain(swapchain)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
213
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
214 # test MSAA
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
215 block:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
216 mainRenderpass = CreatePresentationRenderPass(samples = VK_SAMPLE_COUNT_4_BIT)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
217 swapchain = InitSwapchain(renderpass = mainRenderpass, samples = VK_SAMPLE_COUNT_4_BIT).get()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
218
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
219 # test_01_triangle(99999999)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
220
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
221 checkVkResult vkDeviceWaitIdle(vulkan.device)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
222 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
223 DestroySwapchain(swapchain)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
224
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
225 DestroyVulkan()