Mercurial > games > semicongine
changeset 361:97e913c0e7fe
add: better scene handling, add API to unload scene (not tested yet though)
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 24 Sep 2023 23:15:08 +0700 |
parents | 6794c07934f9 |
children | 3eccdbf32c53 |
files | src/semicongine/engine.nim src/semicongine/renderer.nim src/semicongine/scene.nim |
diffstat | 3 files changed, 31 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/semicongine/engine.nim Sun Sep 24 21:34:17 2023 +0700 +++ b/src/semicongine/engine.nim Sun Sep 24 23:15:08 2023 +0700 @@ -113,12 +113,16 @@ proc initRenderer*(engine: var Engine, clearColor=Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32])) = engine.initRenderer(@[], clearColor) -proc addScene*(engine: var Engine, scene: var Scene) = +proc loadScene*(engine: var Engine, scene: var Scene) = assert engine.renderer.isSome + assert not scene.loaded engine.renderer.get.setupDrawableBuffers(scene) engine.renderer.get.updateMeshData(scene, forceAll=true) engine.renderer.get.updateUniformData(scene, forceAll=true) +proc unloadScene*(engine: var Engine, scene: Scene) = + engine.renderer.get.destroy(scene) + proc renderScene*(engine: var Engine, scene: var Scene) = assert engine.state == Running assert engine.renderer.isSome
--- a/src/semicongine/renderer.nim Sun Sep 24 21:34:17 2023 +0700 +++ b/src/semicongine/renderer.nim Sun Sep 24 23:15:08 2023 +0700 @@ -452,6 +452,25 @@ func valid*(renderer: Renderer): bool = renderer.device.vk.valid +proc destroy*(renderer: var Renderer, scene: Scene) = + var scenedata = renderer.scenedata[scene] + + for buffer in scenedata.vertexBuffers.mvalues: + assert buffer.vk.valid + buffer.destroy() + if scenedata.indexBuffer.vk.valid: + assert scenedata.indexBuffer.vk.valid + scenedata.indexBuffer.destroy() + for pipelineUniforms in scenedata.uniformBuffers.mvalues: + for buffer in pipelineUniforms.mitems: + assert buffer.vk.valid + buffer.destroy() + for textures in scenedata.textures.mvalues: + for texture in textures.mitems: + texture.destroy() + for descriptorPool in scenedata.descriptorPools.mvalues: + descriptorPool.destroy() + proc destroy*(renderer: var Renderer) = for scenedata in renderer.scenedata.mvalues: for buffer in scenedata.vertexBuffers.mvalues:
--- a/src/semicongine/scene.nim Sun Sep 24 21:34:17 2023 +0700 +++ b/src/semicongine/scene.nim Sun Sep 24 23:15:08 2023 +0700 @@ -1,5 +1,6 @@ import std/tables import std/sequtils +import std/strformat import std/hashes import ./core @@ -11,18 +12,22 @@ shaderGlobals*: Table[string, DataList] meshes*: seq[Mesh] dirtyShaderGlobals: seq[string] + loaded*: bool = false proc add*(scene: var Scene, mesh: MeshObject) = + assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add meshes" var tmp = new Mesh tmp[] = mesh scene.meshes.add tmp proc add*(scene: var Scene, mesh: Mesh) = + assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add meshes" assert not mesh.isNil, "Cannot add a mesh that is 'nil'" scene.meshes.add mesh # generic way to add objects that have a mesh-attribute proc add*[T](scene: var Scene, obj: T) = + assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add meshes" for name, value in obj.fieldPairs: when typeof(value) is Mesh: assert not value.isNil, "Cannot add a mesh that is 'nil': " & name @@ -32,11 +37,13 @@ scene.meshes.add value proc addShaderGlobal*[T](scene: var Scene, name: string, data: T) = + assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add shader values" scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]()) setValues(scene.shaderGlobals[name], @[data]) scene.dirtyShaderGlobals.add name proc addShaderGlobalArray*[T](scene: var Scene, name: string, data: openArray[T]) = + assert not scene.loaded, &"Scene {scene.name} has already been loaded, cannot add shader values" scene.shaderGlobals[name] = newDataList(data) scene.dirtyShaderGlobals.add name @@ -56,11 +63,6 @@ if not scene.dirtyShaderGlobals.contains(name): scene.dirtyShaderGlobals.add name -func appendShaderGlobalArray*[T](scene: var Scene, name: string, value: seq[T]) = - appendValues[T](scene.shaderGlobals[name], value) - if not scene.dirtyShaderGlobals.contains(name): - scene.dirtyShaderGlobals.add name - func dirtyShaderGlobals*(scene: Scene): seq[string] = scene.dirtyShaderGlobals