comparison tests/test_rendering.nim @ 1214:04e446a7eb2b compiletime-tests

add: multipass renderer, finish tets for now
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 20:11:55 +0700
parents f9919ea98e5b
children 55896320c8bf
comparison
equal deleted inserted replaced
1213:f9919ea98e5b 1214:04e446a7eb2b
5 5
6 var 6 var
7 mainRenderpass: VkRenderPass 7 mainRenderpass: VkRenderPass
8 swapchain: Swapchain 8 swapchain: Swapchain
9 9
10 proc test_01_triangle(nFrames: int) = 10 proc test_01_triangle(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) =
11 var renderdata = InitRenderData() 11 var renderdata = InitRenderData()
12 12
13 type 13 type
14 TrianglShader = object 14 TrianglShader = object
15 position {.VertexAttribute.}: Vec3f 15 position {.VertexAttribute.}: Vec3f
31 ) 31 )
32 AssignBuffers(renderdata, mesh) 32 AssignBuffers(renderdata, mesh)
33 renderdata.FlushAllMemory() 33 renderdata.FlushAllMemory()
34 34
35 var 35 var
36 pipeline = CreatePipeline[TrianglShader](renderPass = mainRenderpass, samples = swapchain.samples) 36 pipeline = CreatePipeline[TrianglShader](renderPass = renderPass, samples = samples)
37 37
38 var c = 0 38 var c = 0
39 while UpdateInputs() and c < nFrames: 39 while UpdateInputs() and c < nFrames:
40 WithNextFrame(swapchain, framebuffer, commandbuffer): 40 WithNextFrame(swapchain, framebuffer, commandbuffer):
41 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): 41 WithRenderPass(renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
42 WithPipeline(commandbuffer, pipeline): 42 WithPipeline(commandbuffer, pipeline):
43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) 43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
44 inc c 44 inc c
45 45
46 # cleanup 46 # cleanup
47 checkVkResult vkDeviceWaitIdle(vulkan.device) 47 checkVkResult vkDeviceWaitIdle(vulkan.device)
48 DestroyPipeline(pipeline) 48 DestroyPipeline(pipeline)
49 DestroyRenderData(renderdata) 49 DestroyRenderData(renderdata)
50 50
51 51
52 proc test_02_triangle_quad_instanced(nFrames: int) = 52 proc test_02_triangle_quad_instanced(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) =
53 var renderdata = InitRenderData() 53 var renderdata = InitRenderData()
54 54
55 type 55 type
56 SomeShader = object 56 SomeShader = object
57 position {.VertexAttribute.}: Vec3f 57 position {.VertexAttribute.}: Vec3f
99 AssignBuffers(renderdata, quad) 99 AssignBuffers(renderdata, quad)
100 AssignBuffers(renderdata, instancesA) 100 AssignBuffers(renderdata, instancesA)
101 AssignBuffers(renderdata, instancesB) 101 AssignBuffers(renderdata, instancesB)
102 renderdata.FlushAllMemory() 102 renderdata.FlushAllMemory()
103 103
104 var pipeline = CreatePipeline[SomeShader](renderPass = mainRenderpass, samples = swapchain.samples) 104 var pipeline = CreatePipeline[SomeShader](renderPass = renderPass, samples = samples)
105 105
106 var c = 0 106 var c = 0
107 while UpdateInputs() and c < nFrames: 107 while UpdateInputs() and c < nFrames:
108 WithNextFrame(swapchain, framebuffer, commandbuffer): 108 WithNextFrame(swapchain, framebuffer, commandbuffer):
109 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): 109 WithRenderPass(renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
110 WithPipeline(commandbuffer, pipeline): 110 WithPipeline(commandbuffer, pipeline):
111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) 111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA)
112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) 112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB)
113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) 113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA)
114 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) 114 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB)
117 # cleanup 117 # cleanup
118 checkVkResult vkDeviceWaitIdle(vulkan.device) 118 checkVkResult vkDeviceWaitIdle(vulkan.device)
119 DestroyPipeline(pipeline) 119 DestroyPipeline(pipeline)
120 DestroyRenderData(renderdata) 120 DestroyRenderData(renderdata)
121 121
122 proc test_03_simple_descriptorset(nFrames: int) = 122 proc test_03_simple_descriptorset(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) =
123 var renderdata = InitRenderData() 123 var renderdata = InitRenderData()
124 124
125 type 125 type
126 Material = object 126 Material = object
127 baseColor: Vec3f 127 baseColor: Vec3f
176 AssignBuffers(renderdata, uniforms2) 176 AssignBuffers(renderdata, uniforms2)
177 UploadTextures(renderdata, uniforms1) 177 UploadTextures(renderdata, uniforms1)
178 UploadTextures(renderdata, uniforms2) 178 UploadTextures(renderdata, uniforms2)
179 renderdata.FlushAllMemory() 179 renderdata.FlushAllMemory()
180 180
181 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples) 181 var pipeline = CreatePipeline[QuadShader](renderPass = renderPass, samples = samples)
182 182
183 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) 183 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
184 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) 184 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2)
185 185
186 var c = 0 186 var c = 0
187 while UpdateInputs() and c < nFrames: 187 while UpdateInputs() and c < nFrames:
188 WithNextFrame(swapchain, framebuffer, commandbuffer): 188 WithNextFrame(swapchain, framebuffer, commandbuffer):
189 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): 189 WithRenderPass(renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
190 WithPipeline(commandbuffer, pipeline): 190 WithPipeline(commandbuffer, pipeline):
191 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): 191 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF):
192 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) 192 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
193 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF): 193 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF):
194 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) 194 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
197 # cleanup 197 # cleanup
198 checkVkResult vkDeviceWaitIdle(vulkan.device) 198 checkVkResult vkDeviceWaitIdle(vulkan.device)
199 DestroyPipeline(pipeline) 199 DestroyPipeline(pipeline)
200 DestroyRenderData(renderdata) 200 DestroyRenderData(renderdata)
201 201
202 proc test_04_multiple_descriptorsets(nFrames: int) = 202 proc test_04_multiple_descriptorsets(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) =
203 var renderdata = InitRenderData() 203 var renderdata = InitRenderData()
204 204
205 type 205 type
206 RenderSettings = object 206 RenderSettings = object
207 brigthness: float32 207 brigthness: float32
284 AssignBuffers(renderdata, otherset1) 284 AssignBuffers(renderdata, otherset1)
285 AssignBuffers(renderdata, otherset2) 285 AssignBuffers(renderdata, otherset2)
286 UploadTextures(renderdata, mainset) 286 UploadTextures(renderdata, mainset)
287 renderdata.FlushAllMemory() 287 renderdata.FlushAllMemory()
288 288
289 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples) 289 var pipeline = CreatePipeline[QuadShader](renderPass = renderPass, samples = samples)
290 290
291 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset) 291 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset)
292 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset) 292 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset)
293 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) 293 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1)
294 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) 294 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2)
295 295
296 var c = 0 296 var c = 0
297 while UpdateInputs() and c < nFrames: 297 while UpdateInputs() and c < nFrames:
298 WithNextFrame(swapchain, framebuffer, commandbuffer): 298 TimeAndLog:
299 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): 299 WithNextFrame(swapchain, framebuffer, commandbuffer):
300 WithPipeline(commandbuffer, pipeline): 300 WithRenderPass(renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
301 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF): 301 WithPipeline(commandbuffer, pipeline):
302 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) 302 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF):
303 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF): 303 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
304 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) 304 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF):
305 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
305 mainset.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32) 306 mainset.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32)
306 otherset1.data.objectSettings.data.scale = 0.5 + (c.float32 / nFrames.float32) 307 otherset1.data.objectSettings.data.scale = 0.5 + (c.float32 / nFrames.float32)
307 UpdateGPUBuffer(mainset.data.renderSettings) 308 UpdateGPUBuffer(mainset.data.renderSettings)
308 UpdateGPUBuffer(otherset1.data.objectSettings) 309 UpdateGPUBuffer(otherset1.data.objectSettings)
309 renderdata.FlushAllMemory() 310 renderdata.FlushAllMemory()
312 # cleanup 313 # cleanup
313 checkVkResult vkDeviceWaitIdle(vulkan.device) 314 checkVkResult vkDeviceWaitIdle(vulkan.device)
314 DestroyPipeline(pipeline) 315 DestroyPipeline(pipeline)
315 DestroyRenderData(renderdata) 316 DestroyRenderData(renderdata)
316 317
318 proc test_05_triangle_2pass(nFrames: int, samples = VK_SAMPLE_COUNT_1_BIT) =
319 var
320 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass()
321 swapchain = InitSwapchain(renderpass = presentRP).get()
322
323 var renderdata = InitRenderData()
324
325 type
326 Uniforms = object
327 frameTexture: Texture[TVec4[uint8]]
328 TriangleShader = object
329 position {.VertexAttribute.}: Vec3f
330 color {.VertexAttribute.}: Vec3f
331 fragmentColor {.Pass.}: Vec3f
332 outColor {.ShaderOutput.}: Vec4f
333 # code
334 vertexCode: string = """void main() {
335 fragmentColor = color;
336 gl_Position = vec4(position, 1);}"""
337 fragmentCode: string = """void main() {
338 outColor = vec4(fragmentColor, 1);}"""
339 PresentShader = object
340 position {.VertexAttribute.}: Vec2f
341 uv {.Pass.}: Vec2f
342 outColor {.ShaderOutput.}: Vec4f
343 descriptorSets {.DescriptorSets.}: (Uniforms, )
344 # code
345 vertexCode: string = """void main() {
346 uv = ((position + 1) * 0.5) * vec2(1, -1);
347 gl_Position = vec4(position, 0, 1);}"""
348 fragmentCode: string = """void main() {
349 vec2 uv1 = uv + vec2(0.001, 0.001);
350 vec2 uv2 = uv + vec2(0.001, -0.001);
351 vec2 uv3 = uv + vec2(-0.001, 0.001);
352 vec2 uv4 = uv + vec2(-0.001, -0.001);
353 outColor = (
354 texture(frameTexture, uv1) +
355 texture(frameTexture, uv2) +
356 texture(frameTexture, uv3) +
357 texture(frameTexture, uv4)
358 ) / 4;
359 }"""
360 TriangleMesh = object
361 position: GPUArray[Vec3f, VertexBuffer]
362 color: GPUArray[Vec3f, VertexBuffer]
363 QuadMesh = object
364 position: GPUArray[Vec2f, VertexBuffer]
365 indices: GPUArray[uint16, IndexBuffer]
366 var mesh = TriangleMesh(
367 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
368 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer),
369 )
370 var quad = QuadMesh(
371 position: asGPUArray([NewVec2f(-1, -1), NewVec2f(-1, 1), NewVec2f(1, 1), NewVec2f(1, -1)], VertexBuffer),
372 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
373 )
374 var uniforms1 = asDescriptorSet(
375 Uniforms(
376 frameTexture: Texture[TVec4[uint8]](width: swapchain.width, height: swapchain.height, isRenderTarget: true),
377 )
378 )
379 var uniforms2 = asDescriptorSet(
380 Uniforms(
381 frameTexture: Texture[TVec4[uint8]](width: swapchain.width, height: swapchain.height, isRenderTarget: true),
382 )
383 )
384 AssignBuffers(renderdata, mesh)
385 AssignBuffers(renderdata, quad)
386 UploadTextures(renderdata, uniforms1)
387 UploadTextures(renderdata, uniforms2)
388 renderdata.FlushAllMemory()
389
390 var
391 drawPipeline = CreatePipeline[TriangleShader](renderPass = offscreenRP, samples = samples)
392 presentPipeline = CreatePipeline[PresentShader](renderPass = presentRP, samples = samples)
393
394 InitDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms1)
395 InitDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms2)
396
397 var offscreenFB = svkCreateFramebuffer(offscreenRP, swapchain.width, swapchain.height, [uniforms1.data.frameTexture.imageview])
398
399 var c = 0
400 while UpdateInputs() and c < nFrames:
401
402 TimeAndLog:
403 WithNextFrame(swapchain, framebuffer, commandbuffer):
404
405 WithRenderPass(offscreenRP, offscreenFB, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
406 WithPipeline(commandbuffer, drawPipeline):
407 Render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh)
408
409 WithRenderPass(presentRP, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
410 WithPipeline(commandbuffer, presentPipeline):
411 WithBind(commandbuffer, (uniforms1, ), presentPipeline, swapchain.currentFiF):
412 Render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad)
413 inc c
414
415 # cleanup
416 checkVkResult vkDeviceWaitIdle(vulkan.device)
417 DestroyPipeline(presentPipeline)
418 DestroyPipeline(drawPipeline)
419 DestroyRenderData(renderdata)
420 vkDestroyRenderPass(vulkan.device, offscreenRP, nil)
421 vkDestroyRenderPass(vulkan.device, presentRP, nil)
422 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil)
423 DestroySwapchain(swapchain)
424
317 when isMainModule: 425 when isMainModule:
318 var nFrames = 2000 426 var nFrames = 2000
319 InitVulkan() 427 InitVulkan()
320 428
429
321 # test normal 430 # test normal
322 block: 431 block:
323 mainRenderpass = CreatePresentationRenderPass() 432 mainRenderpass = CreateDirectPresentationRenderPass()
324 swapchain = InitSwapchain(renderpass = mainRenderpass).get() 433 swapchain = InitSwapchain(renderpass = mainRenderpass).get()
325 434
326 # tests a simple triangle with minimalistic shader and vertex format 435 # tests a simple triangle with minimalistic shader and vertex format
327 test_01_triangle(nFrames) 436 test_01_triangle(nFrames, renderPass = mainRenderpass)
328 437
329 # tests instanced triangles and quads, mixing meshes and instances 438 # tests instanced triangles and quads, mixing meshes and instances
330 test_02_triangle_quad_instanced(nFrames) 439 test_02_triangle_quad_instanced(nFrames, renderPass = mainRenderpass)
331 440
332 # teste descriptor sets 441 # teste descriptor sets
333 test_03_simple_descriptorset(nFrames) 442 test_03_simple_descriptorset(nFrames, renderPass = mainRenderpass)
334 443
335 # tests multiple descriptor sets and arrays 444 # tests multiple descriptor sets and arrays
336 test_04_multiple_descriptorsets(nFrames) 445 test_04_multiple_descriptorsets(nFrames, renderPass = mainRenderpass)
337 446
338 checkVkResult vkDeviceWaitIdle(vulkan.device) 447 checkVkResult vkDeviceWaitIdle(vulkan.device)
339 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) 448 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
340 DestroySwapchain(swapchain) 449 DestroySwapchain(swapchain)
341 450
342 # test MSAA 451 # test MSAA
343 block: 452 block:
344 mainRenderpass = CreatePresentationRenderPass(samples = VK_SAMPLE_COUNT_4_BIT) 453 mainRenderpass = CreateDirectPresentationRenderPass(samples = VK_SAMPLE_COUNT_4_BIT)
345 swapchain = InitSwapchain(renderpass = mainRenderpass, samples = VK_SAMPLE_COUNT_4_BIT).get() 454 swapchain = InitSwapchain(renderpass = mainRenderpass, samples = VK_SAMPLE_COUNT_4_BIT).get()
346 455
347 test_01_triangle(99999999) 456 test_01_triangle(nFrames, renderPass = mainRenderpass, VK_SAMPLE_COUNT_4_BIT)
348 457
349 checkVkResult vkDeviceWaitIdle(vulkan.device) 458 checkVkResult vkDeviceWaitIdle(vulkan.device)
350 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) 459 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
351 DestroySwapchain(swapchain) 460 DestroySwapchain(swapchain)
352 461
462 # test multiple render passes
463 block:
464 test_05_triangle_2pass(999999999)
465
353 DestroyVulkan() 466 DestroyVulkan()