annotate tests/test_rendering.nim @ 1232:32a977c71ba5

fix: make matrices working correctly
author sam <sam@basx.dev>
date Fri, 19 Jul 2024 20:00:15 +0700
parents 70f6c1cfe005
children 1cf698973dca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
1 import std/sequtils
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
2 import std/monotimes
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
3 import std/times
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
4 import std/options
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
5 import std/random
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
6
1223
55896320c8bf fix: references to new package not updated
sam <sam@basx.dev>
parents: 1214
diff changeset
7 import ../semiconginev2
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
8
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
9 proc test_01_triangle(nFrames: int, swapchain: var Swapchain) =
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
10 var renderdata = InitRenderData()
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
11
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
12 type
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
13 TrianglShader = object
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
14 position {.VertexAttribute.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
15 color {.VertexAttribute.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
16 fragmentColor {.Pass.}: Vec3f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
17 outColor {.ShaderOutput.}: Vec4f
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
18 # code
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
19 vertexCode: string = """void main() {
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
20 fragmentColor = color;
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
21 gl_Position = vec4(position, 1);}"""
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
22 fragmentCode: string = """void main() {
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
23 outColor = vec4(fragmentColor, 1);}"""
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
24 TriangleMesh = object
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
25 position: GPUArray[Vec3f, VertexBuffer]
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
26 color: GPUArray[Vec3f, VertexBuffer]
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
27 var mesh = TriangleMesh(
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
28 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
29 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
30 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
31 AssignBuffers(renderdata, mesh)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
32 renderdata.FlushAllMemory()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
33
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
34 var
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
35 pipeline = CreatePipeline[TrianglShader](renderPass = swapchain.renderPass)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
36
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
37 var c = 0
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
38 while UpdateInputs() and c < nFrames:
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
39 WithNextFrame(swapchain, framebuffer, commandbuffer):
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
40 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
41 WithPipeline(commandbuffer, pipeline):
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
42 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
43 inc c
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
44
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
45 # cleanup
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
46 checkVkResult vkDeviceWaitIdle(vulkan.device)
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
47 DestroyPipeline(pipeline)
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
48 DestroyRenderData(renderdata)
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
49
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
50
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
51 proc test_02_triangle_quad_instanced(nFrames: int, swapchain: var Swapchain) =
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
52 var renderdata = InitRenderData()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
53
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
54 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
55 SomeShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
56 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
57 color {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
58 pos {.InstanceAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
59 scale {.InstanceAttribute.}: float32
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
60 fragmentColor {.Pass.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
61 outColor {.ShaderOutput.}: Vec4f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
62 # code
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
63 vertexCode: string = """void main() {
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
64 fragmentColor = color;
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
65 gl_Position = vec4((position * scale) + pos, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
66 fragmentCode: string = """void main() {
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
67 outColor = vec4(fragmentColor, 1);}"""
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
68 TriangleMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
69 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
70 color: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
71 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
72 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
73 color: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
74 indices: GPUArray[uint16, IndexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
75 Instances = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
76 pos: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
77 scale: GPUArray[float32, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
78 var tri = TriangleMesh(
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
79 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
80 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
81 )
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
82 var quad = QuadMesh(
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
83 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
84 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
85 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
86 )
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 var instancesA: Instances
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
89 for n in 1..100:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
90 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
91 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
92 var instancesB: Instances
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
93 for n in 1..100:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
94 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
95 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
96
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
97 AssignBuffers(renderdata, tri)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
98 AssignBuffers(renderdata, quad)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
99 AssignBuffers(renderdata, instancesA)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
100 AssignBuffers(renderdata, instancesB)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
101 renderdata.FlushAllMemory()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
102
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
103 var pipeline = CreatePipeline[SomeShader](renderPass = swapchain.renderPass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
104
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
105 var c = 0
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
106 while UpdateInputs() and c < nFrames:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
107 WithNextFrame(swapchain, framebuffer, commandbuffer):
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
108 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
109 WithPipeline(commandbuffer, pipeline):
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
110 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
114 inc c
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
115
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
116 # cleanup
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
117 checkVkResult vkDeviceWaitIdle(vulkan.device)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
118 DestroyPipeline(pipeline)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
119 DestroyRenderData(renderdata)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
120
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
121 proc test_03_simple_descriptorset(nFrames: int, swapchain: var Swapchain) =
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
122 var renderdata = InitRenderData()
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
123
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
124 type
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
125 Material = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
126 baseColor: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
127
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
128 Uniforms = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
129 material: GPUValue[Material, UniformBuffer]
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
130 texture1: Image[TVec4[uint8]]
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
131
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
132 QuadShader = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
133 position {.VertexAttribute.}: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
134 fragmentColor {.Pass.}: Vec3f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
135 uv {.Pass.}: Vec2f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
136 outColor {.ShaderOutput.}: Vec4f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
137 descriptorSets {.DescriptorSets.}: (Uniforms, )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
138 # code
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
139 vertexCode: string = """void main() {
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
140 fragmentColor = material.baseColor;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
141 gl_Position = vec4(position, 1);
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
142 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
143 uv = position.xy + 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
144 }"""
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
145 fragmentCode: string = """void main() {
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
146 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}"""
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
147 QuadMesh = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
148 position: GPUArray[Vec3f, VertexBuffer]
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
149 indices: GPUArray[uint16, IndexBuffer]
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
150
1212
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1211
diff changeset
151 let R = TVec4[uint8]([255'u8, 0'u8, 0'u8, 255'u8])
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1211
diff changeset
152 let G = TVec4[uint8]([0'u8, 255'u8, 0'u8, 255'u8])
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1211
diff changeset
153 let B = TVec4[uint8]([0'u8, 0'u8, 255'u8, 255'u8])
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1211
diff changeset
154 let W = TVec4[uint8]([255'u8, 255'u8, 255'u8, 255'u8])
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
155 var
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
156 quad = QuadMesh(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
157 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
158 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
159 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
160 uniforms1 = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
161 Uniforms(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
162 material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer),
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
163 texture1: Image[TVec4[uint8]](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], interpolation: VK_FILTER_NEAREST),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
164 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
165 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
166 uniforms2 = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
167 Uniforms(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
168 material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer),
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
169 texture1: Image[TVec4[uint8]](width: 2, height: 2, data: @[R, G, B, W]),
1212
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1211
diff changeset
170 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
171 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
172
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
173 AssignBuffers(renderdata, quad)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
174 AssignBuffers(renderdata, uniforms1)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
175 AssignBuffers(renderdata, uniforms2)
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
176 UploadImages(renderdata, uniforms1)
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
177 UploadImages(renderdata, uniforms2)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
178 renderdata.FlushAllMemory()
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
179
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
180 var pipeline = CreatePipeline[QuadShader](renderPass = swapchain.renderPass)
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 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
183 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
184
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
185 var c = 0
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
186 while UpdateInputs() and c < nFrames:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
187 WithNextFrame(swapchain, framebuffer, commandbuffer):
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
188 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
189 WithPipeline(commandbuffer, pipeline):
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
190 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF):
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
191 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
192 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF):
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
193 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
194 inc c
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
195
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
196 # cleanup
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
197 checkVkResult vkDeviceWaitIdle(vulkan.device)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
198 DestroyPipeline(pipeline)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
199 DestroyRenderData(renderdata)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
200
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
201 proc test_04_multiple_descriptorsets(nFrames: int, swapchain: var Swapchain) =
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
202 var renderdata = InitRenderData()
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
203
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
204 type
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
205 RenderSettings = object
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
206 brigthness: float32
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
207 Material = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
208 baseColor: Vec3f
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
209 ObjectSettings = object
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
210 scale: float32
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
211 materialIndex: uint32
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
212 Constants = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
213 offset: Vec2f
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
214
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
215 ConstSet = object
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
216 constants: GPUValue[Constants, UniformBuffer]
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
217 MainSet = object
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
218 renderSettings: GPUValue[RenderSettings, UniformBufferMapped]
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
219 material: array[2, GPUValue[Material, UniformBuffer]]
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
220 texture1: array[2, Image[TVec1[uint8]]]
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
221 OtherSet = object
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
222 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped]
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
223
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
224 QuadShader = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
225 position {.VertexAttribute.}: Vec3f
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
226 fragmentColor {.Pass.}: Vec3f
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
227 uv {.Pass.}: Vec2f
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
228 outColor {.ShaderOutput.}: Vec4f
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
229 descriptorSets {.DescriptorSets.}: (ConstSet, MainSet, OtherSet)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
230 # code
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
231 vertexCode: string = """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
232 fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
233 gl_Position = vec4(position * objectSettings.scale, 1);
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
234 gl_Position.xy += constants.offset.xy;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
235 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
236 uv = position.xy + 0.5;
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
237 }"""
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
238 fragmentCode: string = """void main() {
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
239 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
240 }"""
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
241 QuadMesh = object
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
242 position: GPUArray[Vec3f, VertexBuffer]
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
243 indices: GPUArray[uint16, IndexBuffer]
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
244
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
245 var quad = QuadMesh(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
246 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
247 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
248 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
249 var constset = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
250 ConstSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
251 constants: asGPUValue(Constants(offset: NewVec2f(-0.3, 0.2)), UniformBuffer),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
252 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
253 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
254 let G = TVec1[uint8]([50'u8])
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
255 let W = TVec1[uint8]([255'u8])
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
256 var mainset = asDescriptorSet(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
257 MainSet(
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
258 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped),
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
259 material: [
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
260 asGPUValue(Material(baseColor: NewVec3f(1, 1, 0)), UniformBuffer),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
261 asGPUValue(Material(baseColor: NewVec3f(1, 0, 1)), UniformBuffer),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
262 ],
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
263 texture1: [
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
264 Image[TVec1[uint8]](width: 2, height: 2, data: @[W, G, G, W], interpolation: VK_FILTER_NEAREST),
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
265 Image[TVec1[uint8]](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], interpolation: VK_FILTER_NEAREST),
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 ),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
268 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
269 var otherset1 = asDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
270 OtherSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
271 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped),
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
272 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
273 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
274 var otherset2 = asDescriptorSet(
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
275 OtherSet(
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
276 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped),
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
277 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
278 )
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1208
diff changeset
279
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
280 AssignBuffers(renderdata, quad)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
281 AssignBuffers(renderdata, constset)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
282 AssignBuffers(renderdata, mainset)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
283 AssignBuffers(renderdata, otherset1)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
284 AssignBuffers(renderdata, otherset2)
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
285 UploadImages(renderdata, mainset)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
286 renderdata.FlushAllMemory()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
287
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
288 var pipeline = CreatePipeline[QuadShader](renderPass = swapchain.renderPass)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
289
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
290 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
291 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
292 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
293 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
294
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
295 var c = 0
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
296 while UpdateInputs() and c < nFrames:
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
297 TimeAndLog:
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
298 WithNextFrame(swapchain, framebuffer, commandbuffer):
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
299 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
300 WithPipeline(commandbuffer, pipeline):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
301 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
302 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
303 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
304 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
305 mainset.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
306 otherset1.data.objectSettings.data.scale = 0.5 + (c.float32 / nFrames.float32)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
307 UpdateGPUBuffer(mainset.data.renderSettings)
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
308 UpdateGPUBuffer(otherset1.data.objectSettings)
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
309 renderdata.FlushAllMemory()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
310 inc c
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
311
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
312 # cleanup
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
313 checkVkResult vkDeviceWaitIdle(vulkan.device)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
314 DestroyPipeline(pipeline)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
315 DestroyRenderData(renderdata)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
316
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
317 proc test_05_cube(nFrames: int, swapchain: var Swapchain) =
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
318 type
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
319
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
320 UniformData = object
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
321 mvp: Mat4
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
322 Uniforms = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
323 data: GPUValue[UniformData, UniformBufferMapped]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
324 CubeShader = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
325 position {.VertexAttribute.}: Vec3f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
326 color {.VertexAttribute.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
327 fragmentColor {.Pass.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
328 outColor {.ShaderOutput.}: Vec4f
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
329 descriptorSets {.DescriptorSets.}: (Uniforms, )
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
330 # code
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
331 vertexCode = """void main() {
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
332 fragmentColor = color;
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
333 gl_Position = vec4(position, 1) * data.mvp;
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
334 }"""
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
335 fragmentCode = """void main() {
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
336 outColor = fragmentColor;
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 Mesh = object
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
339 position: GPUArray[Vec3f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
340 normals: GPUArray[Vec3f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
341 color: GPUArray[Vec4f, VertexBuffer]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
342
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
343 let quad = @[
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
344 NewVec3f(-0.5, -0.5), NewVec3f(-0.5, +0.5), NewVec3f(+0.5, +0.5),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
345 NewVec3f(+0.5, +0.5), NewVec3f(+0.5, -0.5), NewVec3f(-0.5, -0.5),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
346 ]
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
347 proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] =
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
348 for v in data:
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
349 result.add mat * v
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
350
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
351 var
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
352 vertices: seq[Vec3f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
353 colors: seq[Vec4f]
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
354 normals: seq[Vec3f]
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 # front, red
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
357 vertices.add quad.transf(Translate(0, 0, -0.5))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
358 colors.add newSeqWith(6, NewVec4f(1, 0, 0, 1))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
359 normals.add newSeqWith(6, NewVec3f(0, 0, -1))
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 # back, cyan
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
362 vertices.add quad.transf(Rotate(PI, Y) * Translate(0, 0, -0.5))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
363 colors.add newSeqWith(6, NewVec4f(0, 1, 1, 1))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
364 normals.add newSeqWith(6, NewVec3f(0, 0, 1))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
365
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
366 # right, green
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
367 vertices.add quad.transf(Rotate(PI / 2, Y) * Translate(0, 0, -0.5))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
368 colors.add newSeqWith(6, NewVec4f(0, 1, 0, 1))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
369 normals.add newSeqWith(6, NewVec3f(-1, 0, 0))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
370
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
371 # left, magenta
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
372 vertices.add quad.transf(Rotate(-PI / 2, Y) * Translate(0, 0, -0.5))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
373 colors.add newSeqWith(6, NewVec4f(1, 0, 1, 1))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
374 normals.add newSeqWith(6, NewVec3f(1, 0, 0))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
375
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
376 # bottom, blue
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
377 vertices.add quad.transf(Rotate(PI / 2, X) * Translate(0, 0, -0.5))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
378 colors.add newSeqWith(6, NewVec4f(0, 0, 1, 1))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
379 normals.add newSeqWith(6, NewVec3f(0, -1, 0))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
380
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
381 # top, yellow
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
382 vertices.add quad.transf(Rotate(-PI / 2, X) * Translate(0, 0, -0.5))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
383 colors.add newSeqWith(6, NewVec4f(1, 1, 0, 1))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
384 normals.add newSeqWith(6, NewVec3f(0, 1, 0))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
385
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
386 var renderdata = InitRenderData()
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
387
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
388 var mesh = Mesh(
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
389 position: asGPUArray(vertices, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
390 color: asGPUArray(colors, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
391 normals: asGPUArray(normals, VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
392 )
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
393 AssignBuffers(renderdata, mesh)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
394
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
395 var floor = Mesh(
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
396 position: asGPUArray(quad.transf(Scale(10, 10, 10) * Rotate(-PI / 2, X) * Translate(0, 0, 0.05)), VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
397 color: asGPUArray(newSeqWith(6, NewVec4f(0.1, 0.1, 0.1, 1)), VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
398 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
399 )
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
400 AssignBuffers(renderdata, floor)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
401
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
402 var uniforms1 = asDescriptorSet(
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
403 Uniforms(
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
404 data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped)
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 )
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
407 AssignBuffers(renderdata, uniforms1)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
408
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
409 renderdata.FlushAllMemory()
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
410
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
411 var pipeline = CreatePipeline[CubeShader](renderPass = swapchain.renderPass)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
412 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
413
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
414 var c = 0
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
415 var tStart = getMonoTime()
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
416 var t = tStart
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
417 while UpdateInputs() and c < nFrames:
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
418
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
419 let t = getMonoTime() - tStart
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
420
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
421 uniforms1.data.data.data.mvp = (
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
422 Perspective(-PI / 2, GetAspectRatio(swapchain), 0.01, 100) *
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
423 Translate(0, 0, 2) *
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
424 Rotate(PI / 4, X) *
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
425 Rotate(PI * 0.5 * (t.inMicroseconds() / 1_000_000), Y)
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
426 )
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
427 UpdateGPUBuffer(uniforms1.data.data, flush = true)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
428 WithNextFrame(swapchain, framebuffer, commandbuffer):
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
429 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
430 WithPipeline(commandbuffer, pipeline):
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
431 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF):
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
432 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
433 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
434 inc c
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
435
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
436 # cleanup
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
437 checkVkResult vkDeviceWaitIdle(vulkan.device)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
438 DestroyPipeline(pipeline)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
439 DestroyRenderData(renderdata)
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
440
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
441 proc test_06_triangle_2pass(nFrames: int, depthBuffer: bool, samples: VkSampleCountFlagBits) =
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
442 var
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
443 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
444 swapchain = InitSwapchain(renderpass = presentRP).get()
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
445
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
446 var renderdata = InitRenderData()
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
447
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
448 type
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
449 Uniforms = object
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
450 frameTexture: Image[TVec4[uint8]]
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
451 TriangleShader = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
452 position {.VertexAttribute.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
453 color {.VertexAttribute.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
454 fragmentColor {.Pass.}: Vec3f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
455 outColor {.ShaderOutput.}: Vec4f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
456 # code
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
457 vertexCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
458 fragmentColor = color;
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
459 gl_Position = vec4(position, 1);}"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
460 fragmentCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
461 outColor = vec4(fragmentColor, 1);}"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
462 PresentShader = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
463 position {.VertexAttribute.}: Vec2f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
464 uv {.Pass.}: Vec2f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
465 outColor {.ShaderOutput.}: Vec4f
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
466 descriptorSets {.DescriptorSets.}: (Uniforms, )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
467 # code
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
468 vertexCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
469 uv = ((position + 1) * 0.5) * vec2(1, -1);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
470 gl_Position = vec4(position, 0, 1);}"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
471 fragmentCode: string = """void main() {
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
472 vec2 uv1 = uv + vec2(0.001, 0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
473 vec2 uv2 = uv + vec2(0.001, -0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
474 vec2 uv3 = uv + vec2(-0.001, 0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
475 vec2 uv4 = uv + vec2(-0.001, -0.001);
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
476 outColor = (
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
477 texture(frameTexture, uv1) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
478 texture(frameTexture, uv2) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
479 texture(frameTexture, uv3) +
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
480 texture(frameTexture, uv4)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
481 ) / 4;
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
482 }"""
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
483 TriangleMesh = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
484 position: GPUArray[Vec3f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
485 color: GPUArray[Vec3f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
486 QuadMesh = object
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
487 position: GPUArray[Vec2f, VertexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
488 indices: GPUArray[uint16, IndexBuffer]
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
489 var mesh = TriangleMesh(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
490 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
491 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
492 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
493 var quad = QuadMesh(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
494 position: asGPUArray([NewVec2f(-1, -1), NewVec2f(-1, 1), NewVec2f(1, 1), NewVec2f(1, -1)], VertexBuffer),
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
495 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
496 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
497 var uniforms1 = asDescriptorSet(
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
498 Uniforms(
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
499 frameTexture: Image[TVec4[uint8]](width: swapchain.width, height: swapchain.height, isRenderTarget: true),
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
500 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
501 )
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
502 AssignBuffers(renderdata, mesh)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
503 AssignBuffers(renderdata, quad)
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1223
diff changeset
504 UploadImages(renderdata, uniforms1)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
505 renderdata.FlushAllMemory()
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
506
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
507 var
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
508 drawPipeline = CreatePipeline[TriangleShader](renderPass = offscreenRP)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
509 presentPipeline = CreatePipeline[PresentShader](renderPass = presentRP)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
510
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
511 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
512
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
513 # 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
514 var
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
515 depthImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
516 depthImageView: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
517 depthMemory: VkDeviceMemory
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
518 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
519 depthImage = svkCreate2DImage(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
520 width = swapchain.width,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
521 height = swapchain.height,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
522 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
523 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
524 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
525 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
526 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
527 depthMemory = svkAllocateMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
528 requirements.size,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
529 BestMemory(mappable = false, filter = requirements.memoryTypes)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
530 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
531 checkVkResult vkBindImageMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
532 vulkan.device,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
533 depthImage,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
534 depthMemory,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
535 0,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
536 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
537 depthImageView = svkCreate2DImageView(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
538 image = depthImage,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
539 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
540 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
541 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
542
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
543 # 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
544 var
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
545 msaaImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
546 msaaImageView: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
547 msaaMemory: VkDeviceMemory
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
548 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
549 msaaImage = svkCreate2DImage(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
550 width = swapchain.width,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
551 height = swapchain.height,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
552 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
553 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
554 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
555 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
556 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
557 msaaMemory = svkAllocateMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
558 requirements.size,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
559 BestMemory(mappable = false, filter = requirements.memoryTypes)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
560 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
561 checkVkResult vkBindImageMemory(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
562 vulkan.device,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
563 msaaImage,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
564 msaaMemory,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
565 0,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
566 )
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
567 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
568
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
569 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
570 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
571 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
572 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
573 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
574 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
575 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
576 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
577 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
578 else:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
579 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
580 echo attachments
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
581 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
582 offscreenRP.vk,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
583 swapchain.width,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
584 swapchain.height,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
585 attachments
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
586 )
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
587
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
588 var c = 0
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
589 while UpdateInputs() and c < nFrames:
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
590
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
591 TimeAndLog:
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
592 WithNextFrame(swapchain, framebuffer, commandbuffer):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
593
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
594 WithRenderPass(offscreenRP, offscreenFB, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
595 WithPipeline(commandbuffer, drawPipeline):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
596 Render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
597
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
598 WithRenderPass(presentRP, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
599 WithPipeline(commandbuffer, presentPipeline):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
600 WithBind(commandbuffer, (uniforms1, ), presentPipeline, swapchain.currentFiF):
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
601 Render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
602 inc c
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
603
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
604 # cleanup
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
605 checkVkResult vkDeviceWaitIdle(vulkan.device)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
606 DestroyPipeline(presentPipeline)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
607 DestroyPipeline(drawPipeline)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
608 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
609 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
610 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
611 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
612 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
613 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
614 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
615 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
616 vkFreeMemory(vulkan.device, msaaMemory, nil)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
617 vkDestroyRenderPass(vulkan.device, offscreenRP.vk, nil)
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
618 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
619 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
620 DestroySwapchain(swapchain)
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
621
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
622 when isMainModule:
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
623 var nFrames = 3000
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
624 InitVulkan()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
625
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
626 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
627 var renderPasses = [
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
628 # (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT),
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
629 # (depthBuffer: false, 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
630 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT),
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
631 # (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
632 ]
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
633
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
634 # test normal
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
635 for i, (depthBuffer, samples) in renderPasses:
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
636 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples)
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
637 var swapchain = InitSwapchain(renderpass = renderpass).get()
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
638
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
639 #[
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
640 # tests a simple triangle with minimalistic shader and vertex format
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
641 test_01_triangle(nFrames, swapchain)
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
642
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
643 # tests instanced triangles and quads, mixing meshes and instances
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
644 test_02_triangle_quad_instanced(nFrames, swapchain)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
645
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
646 # teste descriptor sets
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
647 test_03_simple_descriptorset(nFrames, swapchain)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
648
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
649 # tests multiple descriptor sets and arrays
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
650 test_04_multiple_descriptorsets(nFrames, swapchain)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
651 ]#
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
652
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
653 # rotating cube
1232
32a977c71ba5 fix: make matrices working correctly
sam <sam@basx.dev>
parents: 1231
diff changeset
654 test_05_cube(999999999, swapchain)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
655
1230
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
656 checkVkResult vkDeviceWaitIdle(vulkan.device)
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
657 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
51221494caeb fix: uncomment all tests
sam <sam@basx.dev>
parents: 1229
diff changeset
658 DestroySwapchain(swapchain)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
659
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
660 # test multiple render passes
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
661 # for i, (depthBuffer, samples) in renderPasses:
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1230
diff changeset
662 # test_06_triangle_2pass(nFrames, depthBuffer, samples)
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1213
diff changeset
663
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents:
diff changeset
664 DestroyVulkan()