comparison semicongine/renderer.nim @ 1139:114f395b9144

did: finish refactoring and updated all tests accordingly
author sam <sam@basx.dev>
date Sat, 08 Jun 2024 14:58:25 +0700
parents 02e1d2658ff5
children dd757eb5ca86
comparison
equal deleted inserted replaced
1138:02e1d2658ff5 1139:114f395b9144
61 clearColor = NewVec4f(0, 0, 0, 0), 61 clearColor = NewVec4f(0, 0, 0, 0),
62 backFaceCulling = true, 62 backFaceCulling = true,
63 vSync = false, 63 vSync = false,
64 inFlightFrames = 2, 64 inFlightFrames = 2,
65 ): Renderer = 65 ): Renderer =
66 assert device.vk.valid 66 assert device.vk.Valid
67 67
68 result.device = device 68 result.device = device
69 result.renderPass = device.CreateRenderPass(shaders, clearColor = clearColor, backFaceCulling = backFaceCulling) 69 result.renderPass = device.CreateRenderPass(shaders, clearColor = clearColor, backFaceCulling = backFaceCulling)
70 let swapchain = device.CreateSwapchain( 70 let swapchain = device.CreateSwapchain(
71 result.renderPass.vk, 71 result.renderPass.vk,
332 if renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers.len > 0: 332 if renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers.len > 0:
333 var dirtyMaterialAttribs: seq[string] 333 var dirtyMaterialAttribs: seq[string]
334 for material in renderer.scenedata[scene].materials[materialType].mitems: 334 for material in renderer.scenedata[scene].materials[materialType].mitems:
335 dirtyMaterialAttribs.add material.DirtyAttributes 335 dirtyMaterialAttribs.add material.DirtyAttributes
336 material.ClearDirtyAttributes() 336 material.ClearDirtyAttributes()
337 assert renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers[renderer.swapchain.currentInFlight].vk.valid 337 assert renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers[renderer.swapchain.currentInFlight].vk.Valid
338 if forceAll: 338 if forceAll:
339 for buffer in renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers: 339 for buffer in renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers:
340 assert buffer.vk.valid 340 assert buffer.vk.Valid
341 341
342 var offset = 0'u64 342 var offset = 0'u64
343 # loop over all uniforms of the shader-shaderPipeline 343 # loop over all uniforms of the shader-shaderPipeline
344 for uniform in shaderPipeline.Uniforms: 344 for uniform in shaderPipeline.Uniforms:
345 if dirty.contains(uniform.name) or dirtyMaterialAttribs.contains(uniform.name) or forceAll: # only update uniforms if necessary 345 if dirty.contains(uniform.name) or dirtyMaterialAttribs.contains(uniform.name) or forceAll: # only update uniforms if necessary
372 proc StartNewFrame*(renderer: var Renderer) = 372 proc StartNewFrame*(renderer: var Renderer) =
373 # TODO: chance for an infinity-loop? 373 # TODO: chance for an infinity-loop?
374 while not renderer.swapchain.AcquireNextFrame(): 374 while not renderer.swapchain.AcquireNextFrame():
375 checkVkResult renderer.device.vk.vkDeviceWaitIdle() 375 checkVkResult renderer.device.vk.vkDeviceWaitIdle()
376 let res = renderer.swapchain.Recreate() 376 let res = renderer.swapchain.Recreate()
377 if not res.isSome: 377 if res.isSome:
378 raise newException(Exception, "Unable to recreate swapchain") 378 var oldSwapchain = renderer.swapchain
379 var oldSwapchain = renderer.swapchain 379 renderer.swapchain = res.get()
380 renderer.swapchain = res.get() 380 checkVkResult renderer.device.vk.vkDeviceWaitIdle()
381 checkVkResult renderer.device.vk.vkDeviceWaitIdle() 381 oldSwapchain.Destroy()
382 oldSwapchain.Destroy()
383 renderer.nextFrameReady = true 382 renderer.nextFrameReady = true
384 383
385 proc Render*(renderer: var Renderer, scene: Scene) = 384 proc Render*(renderer: var Renderer, scene: Scene) =
386 assert scene in renderer.scenedata 385 assert scene in renderer.scenedata
387 assert renderer.nextFrameReady, "startNewFrame() must be called before calling render()" 386 assert renderer.nextFrameReady, "startNewFrame() must be called before calling render()"
425 oldSwapchain.Destroy() 424 oldSwapchain.Destroy()
426 renderer.swapchain.currentInFlight = (renderer.swapchain.currentInFlight + 1) mod renderer.swapchain.inFlightFrames 425 renderer.swapchain.currentInFlight = (renderer.swapchain.currentInFlight + 1) mod renderer.swapchain.inFlightFrames
427 renderer.nextFrameReady = false 426 renderer.nextFrameReady = false
428 427
429 func Valid*(renderer: Renderer): bool = 428 func Valid*(renderer: Renderer): bool =
430 renderer.device.vk.valid 429 renderer.device.vk.Valid
431 430
432 proc Destroy*(renderer: var Renderer, scene: Scene) = 431 proc Destroy*(renderer: var Renderer, scene: Scene) =
433 checkVkResult renderer.device.vk.vkDeviceWaitIdle() 432 checkVkResult renderer.device.vk.vkDeviceWaitIdle()
434 var scenedata = renderer.scenedata[scene] 433 var scenedata = renderer.scenedata[scene]
435 434
436 for buffer in scenedata.vertexBuffers.mvalues: 435 for buffer in scenedata.vertexBuffers.mvalues:
437 assert buffer.vk.valid 436 assert buffer.vk.Valid
438 buffer.Destroy() 437 buffer.Destroy()
439 438
440 if scenedata.indexBuffer.vk.valid: 439 if scenedata.indexBuffer.vk.Valid:
441 assert scenedata.indexBuffer.vk.valid 440 assert scenedata.indexBuffer.vk.Valid
442 scenedata.indexBuffer.Destroy() 441 scenedata.indexBuffer.Destroy()
443 442
444 var destroyedTextures: seq[VkImage] 443 var destroyedTextures: seq[VkImage]
445 444
446 for (vkPipeline, shaderData) in scenedata.shaderData.mpairs: 445 for (vkPipeline, shaderData) in scenedata.shaderData.mpairs:
447 446
448 for buffer in shaderData.uniformBuffers.mitems: 447 for buffer in shaderData.uniformBuffers.mitems:
449 assert buffer.vk.valid 448 assert buffer.vk.Valid
450 buffer.Destroy() 449 buffer.Destroy()
451 450
452 for textures in shaderData.textures.mvalues: 451 for textures in shaderData.textures.mvalues:
453 for texture in textures.mitems: 452 for texture in textures.mitems:
454 if not destroyedTextures.contains(texture.image.vk): 453 if not destroyedTextures.contains(texture.image.vk):