comparison 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
comparison
equal deleted inserted replaced
1231:70f6c1cfe005 1232:32a977c71ba5
1 import std/sequtils 1 import std/sequtils
2 import std/monotimes
3 import std/times
2 import std/options 4 import std/options
3 import std/random 5 import std/random
4 6
5 import ../semiconginev2 7 import ../semiconginev2
6 8
314 316
315 proc test_05_cube(nFrames: int, swapchain: var Swapchain) = 317 proc test_05_cube(nFrames: int, swapchain: var Swapchain) =
316 type 318 type
317 319
318 UniformData = object 320 UniformData = object
319 m: Mat4 321 mvp: Mat4
320 Uniforms = object 322 Uniforms = object
321 data: GPUValue[UniformData, UniformBufferMapped] 323 data: GPUValue[UniformData, UniformBufferMapped]
322 CubeShader = object 324 CubeShader = object
323 position {.VertexAttribute.}: Vec3f 325 position {.VertexAttribute.}: Vec3f
324 color {.VertexAttribute.}: Vec4f 326 color {.VertexAttribute.}: Vec4f
326 outColor {.ShaderOutput.}: Vec4f 328 outColor {.ShaderOutput.}: Vec4f
327 descriptorSets {.DescriptorSets.}: (Uniforms, ) 329 descriptorSets {.DescriptorSets.}: (Uniforms, )
328 # code 330 # code
329 vertexCode = """void main() { 331 vertexCode = """void main() {
330 fragmentColor = color; 332 fragmentColor = color;
331 gl_Position = data.m * vec4(position, 1); 333 gl_Position = vec4(position, 1) * data.mvp;
332 }""" 334 }"""
333 fragmentCode = """void main() { 335 fragmentCode = """void main() {
334 outColor = fragmentColor; 336 outColor = fragmentColor;
335 }""" 337 }"""
336 Mesh = object 338 Mesh = object
340 342
341 let quad = @[ 343 let quad = @[
342 NewVec3f(-0.5, -0.5), NewVec3f(-0.5, +0.5), NewVec3f(+0.5, +0.5), 344 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), 345 NewVec3f(+0.5, +0.5), NewVec3f(+0.5, -0.5), NewVec3f(-0.5, -0.5),
344 ] 346 ]
345 proc transf(data: seq[Vec3f], m: Mat4): seq[Vec3f] = 347 proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] =
346 for v in data: 348 for v in data:
347 result.add m * v 349 result.add mat * v
348 350
349 var 351 var
350 vertices: seq[Vec3f] 352 vertices: seq[Vec3f]
351 colors: seq[Vec4f] 353 colors: seq[Vec4f]
352 normals: seq[Vec3f] 354 normals: seq[Vec3f]
397 ) 399 )
398 AssignBuffers(renderdata, floor) 400 AssignBuffers(renderdata, floor)
399 401
400 var uniforms1 = asDescriptorSet( 402 var uniforms1 = asDescriptorSet(
401 Uniforms( 403 Uniforms(
402 data: asGPUValue(UniformData(m: Unit4), UniformBufferMapped) 404 data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped)
403 ) 405 )
404 ) 406 )
405 AssignBuffers(renderdata, uniforms1) 407 AssignBuffers(renderdata, uniforms1)
406 408
407 renderdata.FlushAllMemory() 409 renderdata.FlushAllMemory()
408 410
409 var pipeline = CreatePipeline[CubeShader](renderPass = swapchain.renderPass) 411 var pipeline = CreatePipeline[CubeShader](renderPass = swapchain.renderPass)
410 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) 412 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
411 413
412 var c = 0 414 var c = 0
415 var tStart = getMonoTime()
416 var t = tStart
413 while UpdateInputs() and c < nFrames: 417 while UpdateInputs() and c < nFrames:
414 418
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) 419 let t = getMonoTime() - tStart
420
421 uniforms1.data.data.data.mvp = (
422 Perspective(-PI / 2, GetAspectRatio(swapchain), 0.01, 100) *
423 Translate(0, 0, 2) *
424 Rotate(PI / 4, X) *
425 Rotate(PI * 0.5 * (t.inMicroseconds() / 1_000_000), Y)
426 )
416 UpdateGPUBuffer(uniforms1.data.data, flush = true) 427 UpdateGPUBuffer(uniforms1.data.data, flush = true)
417 WithNextFrame(swapchain, framebuffer, commandbuffer): 428 WithNextFrame(swapchain, framebuffer, commandbuffer):
418 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): 429 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
419 WithPipeline(commandbuffer, pipeline): 430 WithPipeline(commandbuffer, pipeline):
420 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): 431 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF):
638 # tests multiple descriptor sets and arrays 649 # tests multiple descriptor sets and arrays
639 test_04_multiple_descriptorsets(nFrames, swapchain) 650 test_04_multiple_descriptorsets(nFrames, swapchain)
640 ]# 651 ]#
641 652
642 # rotating cube 653 # rotating cube
643 while true: 654 test_05_cube(999999999, swapchain)
644 test_05_cube(nFrames, swapchain)
645 655
646 checkVkResult vkDeviceWaitIdle(vulkan.device) 656 checkVkResult vkDeviceWaitIdle(vulkan.device)
647 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) 657 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
648 DestroySwapchain(swapchain) 658 DestroySwapchain(swapchain)
649 659