# HG changeset patch # User Sam # Date 1695572108 -25200 # Node ID 84c56d1aef392091c95f7c538189e7c81e543f39 # Parent 6d258ab35f702c86b01b53692ffb2306d3908cc3 add: better scene handling, add API to unload scene (not tested yet though) diff -r 6d258ab35f70 -r 84c56d1aef39 src/semicongine/engine.nim --- 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 diff -r 6d258ab35f70 -r 84c56d1aef39 src/semicongine/renderer.nim --- 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: diff -r 6d258ab35f70 -r 84c56d1aef39 src/semicongine/scene.nim --- 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