Mercurial > games > semicongine
comparison tests/test_rendering.nim @ 1231:70f6c1cfe005
add: incomplete cube demo
author | sam <sam@basx.dev> |
---|---|
date | Thu, 18 Jul 2024 23:48:57 +0700 |
parents | 51221494caeb |
children | 32a977c71ba5 |
comparison
equal
deleted
inserted
replaced
1230:51221494caeb | 1231:70f6c1cfe005 |
---|---|
1 import std/sequtils | |
1 import std/options | 2 import std/options |
2 import std/random | 3 import std/random |
3 | 4 |
4 import ../semiconginev2 | 5 import ../semiconginev2 |
5 | 6 |
309 # cleanup | 310 # cleanup |
310 checkVkResult vkDeviceWaitIdle(vulkan.device) | 311 checkVkResult vkDeviceWaitIdle(vulkan.device) |
311 DestroyPipeline(pipeline) | 312 DestroyPipeline(pipeline) |
312 DestroyRenderData(renderdata) | 313 DestroyRenderData(renderdata) |
313 | 314 |
314 proc test_05_triangle_2pass(nFrames: int, depthBuffer: bool, samples: VkSampleCountFlagBits) = | 315 proc test_05_cube(nFrames: int, swapchain: var Swapchain) = |
316 type | |
317 | |
318 UniformData = object | |
319 m: Mat4 | |
320 Uniforms = object | |
321 data: GPUValue[UniformData, UniformBufferMapped] | |
322 CubeShader = object | |
323 position {.VertexAttribute.}: Vec3f | |
324 color {.VertexAttribute.}: Vec4f | |
325 fragmentColor {.Pass.}: Vec4f | |
326 outColor {.ShaderOutput.}: Vec4f | |
327 descriptorSets {.DescriptorSets.}: (Uniforms, ) | |
328 # code | |
329 vertexCode = """void main() { | |
330 fragmentColor = color; | |
331 gl_Position = data.m * vec4(position, 1); | |
332 }""" | |
333 fragmentCode = """void main() { | |
334 outColor = fragmentColor; | |
335 }""" | |
336 Mesh = object | |
337 position: GPUArray[Vec3f, VertexBuffer] | |
338 normals: GPUArray[Vec3f, VertexBuffer] | |
339 color: GPUArray[Vec4f, VertexBuffer] | |
340 | |
341 let quad = @[ | |
342 NewVec3f(-0.5, -0.5), NewVec3f(-0.5, +0.5), NewVec3f(+0.5, +0.5), | |
343 NewVec3f(+0.5, +0.5), NewVec3f(+0.5, -0.5), NewVec3f(-0.5, -0.5), | |
344 ] | |
345 proc transf(data: seq[Vec3f], m: Mat4): seq[Vec3f] = | |
346 for v in data: | |
347 result.add m * v | |
348 | |
349 var | |
350 vertices: seq[Vec3f] | |
351 colors: seq[Vec4f] | |
352 normals: seq[Vec3f] | |
353 | |
354 # front, red | |
355 vertices.add quad.transf(Translate(0, 0, -0.5)) | |
356 colors.add newSeqWith(6, NewVec4f(1, 0, 0, 1)) | |
357 normals.add newSeqWith(6, NewVec3f(0, 0, -1)) | |
358 | |
359 # back, cyan | |
360 vertices.add quad.transf(Rotate(PI, Y) * Translate(0, 0, -0.5)) | |
361 colors.add newSeqWith(6, NewVec4f(0, 1, 1, 1)) | |
362 normals.add newSeqWith(6, NewVec3f(0, 0, 1)) | |
363 | |
364 # right, green | |
365 vertices.add quad.transf(Rotate(PI / 2, Y) * Translate(0, 0, -0.5)) | |
366 colors.add newSeqWith(6, NewVec4f(0, 1, 0, 1)) | |
367 normals.add newSeqWith(6, NewVec3f(-1, 0, 0)) | |
368 | |
369 # left, magenta | |
370 vertices.add quad.transf(Rotate(-PI / 2, Y) * Translate(0, 0, -0.5)) | |
371 colors.add newSeqWith(6, NewVec4f(1, 0, 1, 1)) | |
372 normals.add newSeqWith(6, NewVec3f(1, 0, 0)) | |
373 | |
374 # bottom, blue | |
375 vertices.add quad.transf(Rotate(PI / 2, X) * Translate(0, 0, -0.5)) | |
376 colors.add newSeqWith(6, NewVec4f(0, 0, 1, 1)) | |
377 normals.add newSeqWith(6, NewVec3f(0, -1, 0)) | |
378 | |
379 # top, yellow | |
380 vertices.add quad.transf(Rotate(-PI / 2, X) * Translate(0, 0, -0.5)) | |
381 colors.add newSeqWith(6, NewVec4f(1, 1, 0, 1)) | |
382 normals.add newSeqWith(6, NewVec3f(0, 1, 0)) | |
383 | |
384 var renderdata = InitRenderData() | |
385 | |
386 var mesh = Mesh( | |
387 position: asGPUArray(vertices, VertexBuffer), | |
388 color: asGPUArray(colors, VertexBuffer), | |
389 normals: asGPUArray(normals, VertexBuffer), | |
390 ) | |
391 AssignBuffers(renderdata, mesh) | |
392 | |
393 var floor = Mesh( | |
394 position: asGPUArray(quad.transf(Scale(10, 10, 10) * Rotate(-PI / 2, X) * Translate(0, 0, 0.05)), VertexBuffer), | |
395 color: asGPUArray(newSeqWith(6, NewVec4f(0.1, 0.1, 0.1, 1)), VertexBuffer), | |
396 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), | |
397 ) | |
398 AssignBuffers(renderdata, floor) | |
399 | |
400 var uniforms1 = asDescriptorSet( | |
401 Uniforms( | |
402 data: asGPUValue(UniformData(m: Unit4), UniformBufferMapped) | |
403 ) | |
404 ) | |
405 AssignBuffers(renderdata, uniforms1) | |
406 | |
407 renderdata.FlushAllMemory() | |
408 | |
409 var pipeline = CreatePipeline[CubeShader](renderPass = swapchain.renderPass) | |
410 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | |
411 | |
412 var c = 0 | |
413 while UpdateInputs() and c < nFrames: | |
414 | |
415 uniforms1.data.data.data.m = Translate(0, 0, -2) * Rotate(PI * 2 * c.float32 / nFrames.float32, Y) * Rotate(-PI / 4, X) * Perspective(-PI / 2, GetAspectRatio(swapchain), 0.01, 100) | |
416 UpdateGPUBuffer(uniforms1.data.data, flush = true) | |
417 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
418 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | |
419 WithPipeline(commandbuffer, pipeline): | |
420 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): | |
421 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) | |
422 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor) | |
423 inc c | |
424 | |
425 # cleanup | |
426 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
427 DestroyPipeline(pipeline) | |
428 DestroyRenderData(renderdata) | |
429 | |
430 proc test_06_triangle_2pass(nFrames: int, depthBuffer: bool, samples: VkSampleCountFlagBits) = | |
315 var | 431 var |
316 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 432 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
317 swapchain = InitSwapchain(renderpass = presentRP).get() | 433 swapchain = InitSwapchain(renderpass = presentRP).get() |
318 | 434 |
319 var renderdata = InitRenderData() | 435 var renderdata = InitRenderData() |
491 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil) | 607 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil) |
492 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) | 608 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) |
493 DestroySwapchain(swapchain) | 609 DestroySwapchain(swapchain) |
494 | 610 |
495 when isMainModule: | 611 when isMainModule: |
496 var nFrames = 1000 | 612 var nFrames = 3000 |
497 InitVulkan() | 613 InitVulkan() |
498 | 614 |
499 var mainRenderpass: RenderPass | 615 var mainRenderpass: RenderPass |
500 var renderPasses = [ | 616 var renderPasses = [ |
501 (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), | 617 # (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), |
502 (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), | 618 # (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), |
503 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), | 619 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), |
504 (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), | 620 # (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), |
505 ] | 621 ] |
506 | 622 |
507 # test normal | 623 # test normal |
508 for i, (depthBuffer, samples) in renderPasses: | 624 for i, (depthBuffer, samples) in renderPasses: |
509 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | 625 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
510 var swapchain = InitSwapchain(renderpass = renderpass).get() | 626 var swapchain = InitSwapchain(renderpass = renderpass).get() |
511 | 627 |
628 #[ | |
512 # tests a simple triangle with minimalistic shader and vertex format | 629 # tests a simple triangle with minimalistic shader and vertex format |
513 test_01_triangle(nFrames, swapchain) | 630 test_01_triangle(nFrames, swapchain) |
514 | 631 |
515 # tests instanced triangles and quads, mixing meshes and instances | 632 # tests instanced triangles and quads, mixing meshes and instances |
516 test_02_triangle_quad_instanced(nFrames, swapchain) | 633 test_02_triangle_quad_instanced(nFrames, swapchain) |
518 # teste descriptor sets | 635 # teste descriptor sets |
519 test_03_simple_descriptorset(nFrames, swapchain) | 636 test_03_simple_descriptorset(nFrames, swapchain) |
520 | 637 |
521 # tests multiple descriptor sets and arrays | 638 # tests multiple descriptor sets and arrays |
522 test_04_multiple_descriptorsets(nFrames, swapchain) | 639 test_04_multiple_descriptorsets(nFrames, swapchain) |
640 ]# | |
641 | |
642 # rotating cube | |
643 while true: | |
644 test_05_cube(nFrames, swapchain) | |
523 | 645 |
524 checkVkResult vkDeviceWaitIdle(vulkan.device) | 646 checkVkResult vkDeviceWaitIdle(vulkan.device) |
525 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | 647 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
526 DestroySwapchain(swapchain) | 648 DestroySwapchain(swapchain) |
527 | 649 |
528 # test multiple render passes | 650 # test multiple render passes |
529 for i, (depthBuffer, samples) in renderPasses: | 651 # for i, (depthBuffer, samples) in renderPasses: |
530 test_05_triangle_2pass(nFrames, depthBuffer, samples) | 652 # test_06_triangle_2pass(nFrames, depthBuffer, samples) |
531 | 653 |
532 DestroyVulkan() | 654 DestroyVulkan() |