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