From 9fa21837ae7b54b62ea09939473aa987f07eaf90 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 9 Jul 2025 19:45:49 -0700 Subject: Rename scene_memory -> memory --- CMakeLists.txt | 2 +- include/gfx/llr/llr.h | 3 - include/gfx/scene/scene.h | 3 - src/asset/asset_cache.c | 2 +- src/gfx.c | 2 +- src/llr/light.c | 4 +- src/llr/material.c | 2 +- src/llr/mesh.c | 2 +- src/memory.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++ src/memory.h | 39 ++++++++++ src/renderer/renderer.c | 2 +- src/scene/animation.c | 18 ++--- src/scene/camera.c | 2 +- src/scene/model.c | 2 +- src/scene/model_impl.h | 2 +- src/scene/node.c | 2 +- src/scene/object.c | 2 +- src/scene/scene.c | 2 +- src/scene/scene_graph.h | 2 +- src/scene/scene_memory.c | 178 ---------------------------------------------- src/scene/scene_memory.h | 39 ---------- 21 files changed, 241 insertions(+), 247 deletions(-) create mode 100644 src/memory.c create mode 100644 src/memory.h delete mode 100644 src/scene/scene_memory.c delete mode 100644 src/scene/scene_memory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c480daf..de5f0dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ add_library(gfx SHARED src/llr/light.c src/llr/material.c src/llr/mesh.c + src/memory.c src/renderer/imm_renderer.c src/renderer/renderer.c src/scene/animation.c @@ -61,7 +62,6 @@ add_library(gfx SHARED src/scene/node.c src/scene/object.c src/scene/scene.c - src/scene/scene_memory.c src/gfx.c src/util/geometry.c src/util/ibl.c diff --git a/include/gfx/llr/llr.h b/include/gfx/llr/llr.h index 77df33f..8add1d5 100644 --- a/include/gfx/llr/llr.h +++ b/include/gfx/llr/llr.h @@ -36,9 +36,6 @@ void gfx_llr_clear_skeleton(LLR*); /// Set the camera. void gfx_llr_set_camera(LLR*, const Camera*); -/// Set the view-projection matrix. -// void gfx_llr_set_view_projection_matrix(LLR*, const mat4*); - /// Set the aspect ratio. void gfx_llr_set_aspect(LLR*, float aspect); diff --git a/include/gfx/scene/scene.h b/include/gfx/scene/scene.h index 0d96210..ac1b315 100644 --- a/include/gfx/scene/scene.h +++ b/include/gfx/scene/scene.h @@ -1,8 +1,5 @@ #pragma once -#include -#include - typedef struct SceneNode SceneNode; typedef struct Scene Scene; diff --git a/src/asset/asset_cache.c b/src/asset/asset_cache.c index 727b63f..a364330 100644 --- a/src/asset/asset_cache.c +++ b/src/asset/asset_cache.c @@ -1,10 +1,10 @@ #include "asset_cache.h" +#include "memory.h" #include "model.h" #include "scene/animation_impl.h" #include "scene/model_impl.h" #include "scene/node_impl.h" -#include "scene/scene_memory.h" #include "texture.h" #include diff --git a/src/gfx.c b/src/gfx.c index 4291ae7..39b04cd 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -3,9 +3,9 @@ #include "asset/asset_cache.h" #include "core/core_impl.h" #include "llr/llr_impl.h" +#include "memory.h" #include "renderer/imm_renderer_impl.h" #include "renderer/renderer_impl.h" -#include "scene/scene_memory.h" #include #include diff --git a/src/llr/light.c b/src/llr/light.c index 1d1c40d..0fa1522 100644 --- a/src/llr/light.c +++ b/src/llr/light.c @@ -1,7 +1,7 @@ #include "light_impl.h" -#include "../scene/node_impl.h" -#include "../scene/scene_memory.h" +#include "memory.h" +#include "scene/node_impl.h" #include diff --git a/src/llr/material.c b/src/llr/material.c index 4014482..f09dd3f 100644 --- a/src/llr/material.c +++ b/src/llr/material.c @@ -1,6 +1,6 @@ #include "material_impl.h" -#include "../scene/scene_memory.h" +#include "memory.h" #include diff --git a/src/llr/mesh.c b/src/llr/mesh.c index 3aebb04..5f9e5d0 100644 --- a/src/llr/mesh.c +++ b/src/llr/mesh.c @@ -1,6 +1,6 @@ #include "mesh_impl.h" -#include "../scene/scene_memory.h" +#include "memory.h" #include diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..59bf8ca --- /dev/null +++ b/src/memory.c @@ -0,0 +1,178 @@ +#include "memory.h" + +#include + +#include "llr/light_impl.h" +#include "llr/material_impl.h" +#include "llr/mesh_impl.h" +#include "scene/animation_impl.h" +#include "scene/camera_impl.h" +#include "scene/model_impl.h" +#include "scene/node_impl.h" +#include "scene/object_impl.h" +#include "scene/scene_impl.h" + +#include +#include + +DEF_MEMPOOL(anima_pool, Anima, GFX_MAX_NUM_ANIMAS) +DEF_MEMPOOL(animation_pool, Animation, GFX_MAX_NUM_ANIMATIONS) +DEF_MEMPOOL(camera_pool, SceneCamera, GFX_MAX_NUM_CAMERAS) +DEF_MEMPOOL(light_pool, Light, GFX_MAX_NUM_LIGHTS) +DEF_MEMPOOL(material_pool, Material, GFX_MAX_NUM_MATERIALS) +DEF_MEMPOOL(mesh_pool, Mesh, GFX_MAX_NUM_MESHES) +DEF_MEMPOOL(mesh_link_pool, MeshLink, GFX_MAX_NUM_MESH_LINKS) +DEF_MEMPOOL(model_pool, Model, GFX_MAX_NUM_MODELS) +DEF_MEMPOOL(node_pool, SceneNode, GFX_MAX_NUM_NODES) +DEF_MEMPOOL(object_pool, SceneObject, GFX_MAX_NUM_OBJECTS) +DEF_MEMPOOL(scene_pool, Scene, GFX_MAX_NUM_SCENES) +DEF_MEMPOOL(skeleton_pool, Skeleton, GFX_MAX_NUM_SKELETONS) + +/// Scene memory. +/// +/// Holds memory pools for every type of scene object. +typedef struct SceneMemory { + anima_pool animas; + animation_pool animations; + camera_pool cameras; + light_pool lights; + material_pool materials; + mesh_pool meshs; // Purposeful typo to make the PLURAL() macro work. + mesh_link_pool mesh_links; + model_pool models; + node_pool nodes; + object_pool objects; + scene_pool scenes; + skeleton_pool skeletons; +} SceneMemory; + +static SceneMemory mem; + +#define ALLOC_DUMMY(POOL) \ + { \ + const void* object = mempool_alloc(POOL); \ + (void)object; /* Silence warning in release builds. */ \ + assert(mempool_get_block_index(POOL, object) == 0); \ + } + +#define PLURAL(name) name##s +#define MEM_FIELD(name) mem.PLURAL(name) + +void scene_mem_init() { + mempool_make(&mem.animas); + mempool_make(&mem.animations); + mempool_make(&mem.cameras); + mempool_make(&mem.lights); + mempool_make(&mem.materials); + mempool_make(&mem.meshs); + mempool_make(&mem.mesh_links); + mempool_make(&mem.models); + mempool_make(&mem.nodes); + mempool_make(&mem.objects); + mempool_make(&mem.scenes); + mempool_make(&mem.skeletons); + + // Allocate dummy objects at index 0 to guarantee that no objects allocated by + // the caller map to index 0. This allows 0 to be used as a sentinel. + ALLOC_DUMMY(&mem.animas); + ALLOC_DUMMY(&mem.animations); + ALLOC_DUMMY(&mem.cameras); + ALLOC_DUMMY(&mem.lights); + ALLOC_DUMMY(&mem.materials); + ALLOC_DUMMY(&mem.meshs); + ALLOC_DUMMY(&mem.mesh_links); + ALLOC_DUMMY(&mem.models); + ALLOC_DUMMY(&mem.nodes); + ALLOC_DUMMY(&mem.objects); + ALLOC_DUMMY(&mem.scenes); + ALLOC_DUMMY(&mem.skeletons); +} + +void scene_mem_destroy() { + // NOTE: the dummy objects are not constructed, so the destruction code below + // always skips index 0. (I don't really like the conditional inside the loop, + // but this gets the job done without having to specialize the loop macro.) +#define DESTROY(NAME) \ + mempool_foreach(&MEM_FIELD(NAME), obj, { \ + if (i > 0) { \ + gfx_destroy_##NAME(&obj); \ + } \ + }) + + // Print memory diagnostics. +#define PRINT_POOL(POOL_NAME, POOL) \ + { \ + const size_t capacity = mempool_capacity(POOL); \ + const size_t size = mempool_size(POOL); \ + const size_t block_size_bytes = mempool_block_size_bytes(POOL); \ + const size_t size_bytes = size * block_size_bytes; \ + const size_t capacity_bytes = capacity * block_size_bytes; \ + LOGI( \ + "%s pool: %lu/%lu (%lu/%lu bytes)", POOL_NAME, size, capacity, \ + size_bytes, capacity_bytes); \ + } + + LOGI("Pool diagnostics:"); + PRINT_POOL("Animas", &mem.animas); + PRINT_POOL("Animations", &mem.animations); + PRINT_POOL("Cameras", &mem.cameras); + PRINT_POOL("Lights", &mem.lights); + PRINT_POOL("Materials", &mem.materials); + PRINT_POOL("Meshes", &mem.meshs); + PRINT_POOL("Mesh links", &mem.mesh_links); + PRINT_POOL("Models", &mem.models); + PRINT_POOL("Nodes", &mem.nodes); + PRINT_POOL("Objects", &mem.objects); + PRINT_POOL("Scenes", &mem.scenes); + PRINT_POOL("Skeletons", &mem.skeletons); + + // Models contain scene elements. Destruction is handled by the remainder of + // scene destructionb elow. + // + // First destroy the scenes. This will recursively destroy the scene's nodes + // and their objects and avoid a double-free when we then destroy any stray + // scene elements. + DESTROY(scene); + // Then delete stray nodes. This will delete their children nodes and + // resource. + DESTROY(node); + // Destroy remaining scene elements. + DESTROY(anima); + // Animations are owned by animas and do not have a destructor. + DESTROY(camera); + DESTROY(light); + DESTROY(material); + DESTROY(mesh); + // Mesh links don't have a destructor. + DESTROY(object); + // Skeletons are owned by animas and do not have a destructor. +} + +#define DEF_MEMORY(NAME, TYPE) \ + /* xyz* mem_alloc_xyz(); */ \ + TYPE* mem_alloc_##NAME() { return mempool_alloc(&MEM_FIELD(NAME)); } \ + /* void mem_free_xyz(xyz**); */ \ + void mem_free_##NAME(TYPE** obj) { mempool_free(&MEM_FIELD(NAME), obj); } \ + /* xyz* mem_get_xyz(xyz_idx); */ \ + TYPE* mem_get_##NAME(NAMED_INDEX(NAME) index) { \ + assert(index.val != 0); /* 0 is the dummy allocation. */ \ + return mempool_get_block(&MEM_FIELD(NAME), index.val); \ + } \ + /* xyz_idx mem_get_xyz_index(const xyz*); */ \ + NAMED_INDEX(NAME) mem_get_##NAME##_index(const TYPE* obj) { \ + return (NAMED_INDEX(NAME)){ \ + .val = mempool_get_block_index(&MEM_FIELD(NAME), obj)}; \ + } + +DEF_MEMORY(anima, Anima) +DEF_MEMORY(animation, Animation) +DEF_MEMORY(camera, SceneCamera) +DEF_MEMORY(light, Light) +DEF_MEMORY(material, Material) +DEF_MEMORY(mesh, Mesh) +DEF_MEMORY(mesh_link, MeshLink) +DEF_MEMORY(model, Model) +DEF_MEMORY(node, SceneNode) +DEF_MEMORY(object, SceneObject) +DEF_MEMORY(scene, Scene) +DEF_MEMORY(skeleton, Skeleton) diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..366c6e4 --- /dev/null +++ b/src/memory.h @@ -0,0 +1,39 @@ +/// Memory management of scene objects. +#pragma once + +#include "scene/types.h" + +/// Initialize scene memory. +/// +/// The scene memory guarantees that every object maps to an index different +/// than 0. This way, 0 can be used as a special index to denote "no value". +void scene_mem_init(); + +/// Destroy the scene memory and all allocated objects. +void scene_mem_destroy(); + +#define NAMED_INDEX(name) name##_idx + +#define DECL_MEMORY(name, type) \ + typedef struct type type; \ + /* xyz* mem_alloc_xyz() */ \ + type* mem_alloc_##name(); \ + /* mem_free_xyz(xyz**) */ \ + void mem_free_##name(type**); \ + /* xyz* mem_get_xyz(xyz_idx); */ \ + type* mem_get_##name(NAMED_INDEX(name)); \ + /* xyz_idx mem_get_xyz_index(const xyz*); */ \ + NAMED_INDEX(name) mem_get_##name##_index(const type*); + +DECL_MEMORY(anima, Anima) +DECL_MEMORY(animation, Animation) +DECL_MEMORY(camera, SceneCamera) +DECL_MEMORY(light, Light) +DECL_MEMORY(material, Material) +DECL_MEMORY(mesh, Mesh) +DECL_MEMORY(mesh_link, MeshLink) +DECL_MEMORY(model, Model) +DECL_MEMORY(node, SceneNode) +DECL_MEMORY(object, SceneObject) +DECL_MEMORY(scene, Scene) +DECL_MEMORY(skeleton, Skeleton) diff --git a/src/renderer/renderer.c b/src/renderer/renderer.c index 29a1813..6bcf5cc 100644 --- a/src/renderer/renderer.c +++ b/src/renderer/renderer.c @@ -2,13 +2,13 @@ #include "llr/light_impl.h" #include "llr/mesh_impl.h" +#include "memory.h" #include "scene/animation_impl.h" #include "scene/camera_impl.h" #include "scene/model_impl.h" #include "scene/node_impl.h" #include "scene/object_impl.h" #include "scene/scene_impl.h" -#include "scene/scene_memory.h" #include #include diff --git a/src/scene/animation.c b/src/scene/animation.c index 08d02ce..601c400 100644 --- a/src/scene/animation.c +++ b/src/scene/animation.c @@ -1,7 +1,7 @@ #include "animation_impl.h" +#include "memory.h" #include "node_impl.h" -#include "scene_memory.h" #include @@ -504,21 +504,21 @@ Box gfx_get_joint_box( return (Box){ .vertices = { mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmax.z), 1), + joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmax.z), 1), mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmax.z), 1), + joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmax.z), 1), mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmax.z), 1), + joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmax.z), 1), mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmax.z), 1), + joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmax.z), 1), mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmin.z), 1), + joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmin.z), 1), mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmin.z), 1), + joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmin.z), 1), mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmin.z), 1), + joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmin.z), 1), mat4_mul_vec3( - joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmin.z), 1), + joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmin.z), 1), } }; } diff --git a/src/scene/camera.c b/src/scene/camera.c index be7d806..bb073ba 100644 --- a/src/scene/camera.c +++ b/src/scene/camera.c @@ -1,7 +1,7 @@ #include "camera_impl.h" +#include "memory.h" #include "node_impl.h" -#include "scene_memory.h" #include diff --git a/src/scene/model.c b/src/scene/model.c index cc41a9a..e280a41 100644 --- a/src/scene/model.c +++ b/src/scene/model.c @@ -2,7 +2,7 @@ #include -#include "scene_memory.h" +#include "memory.h" #include diff --git a/src/scene/model_impl.h b/src/scene/model_impl.h index a99d32c..39ac27f 100644 --- a/src/scene/model_impl.h +++ b/src/scene/model_impl.h @@ -2,7 +2,7 @@ #include -#include "scene_memory.h" +#include "memory.h" /// Model. typedef struct Model { diff --git a/src/scene/node.c b/src/scene/node.c index e359f73..9d45aa7 100644 --- a/src/scene/node.c +++ b/src/scene/node.c @@ -3,10 +3,10 @@ #include "animation_impl.h" #include "camera_impl.h" #include "llr/light_impl.h" +#include "memory.h" #include "model_impl.h" #include "object_impl.h" #include "scene_graph.h" -#include "scene_memory.h" #include "gfx_assert.h" diff --git a/src/scene/object.c b/src/scene/object.c index 27ff5db..e985fd5 100644 --- a/src/scene/object.c +++ b/src/scene/object.c @@ -3,8 +3,8 @@ #include #include "llr/mesh_impl.h" +#include "memory.h" #include "node_impl.h" -#include "scene_memory.h" #include diff --git a/src/scene/scene.c b/src/scene/scene.c index 54452dd..8c53810 100644 --- a/src/scene/scene.c +++ b/src/scene/scene.c @@ -1,7 +1,7 @@ #include "scene_impl.h" +#include "memory.h" #include "node_impl.h" -#include "scene_memory.h" #include diff --git a/src/scene/scene_graph.h b/src/scene/scene_graph.h index 0b1f7d0..e7135a4 100644 --- a/src/scene/scene_graph.h +++ b/src/scene/scene_graph.h @@ -1,7 +1,7 @@ /// Functions for list manipulation. #pragma once -#include "scene_memory.h" +#include "memory.h" // NOTE: SceneMemory guarantees that index 0 can be regarded as an invalid // index. diff --git a/src/scene/scene_memory.c b/src/scene/scene_memory.c deleted file mode 100644 index 3a01325..0000000 --- a/src/scene/scene_memory.c +++ /dev/null @@ -1,178 +0,0 @@ -#include "scene_memory.h" - -#include - -#include "animation_impl.h" -#include "camera_impl.h" -#include "llr/light_impl.h" -#include "llr/material_impl.h" -#include "llr/mesh_impl.h" -#include "model_impl.h" -#include "node_impl.h" -#include "object_impl.h" -#include "scene_impl.h" - -#include -#include - -DEF_MEMPOOL(anima_pool, Anima, GFX_MAX_NUM_ANIMAS) -DEF_MEMPOOL(animation_pool, Animation, GFX_MAX_NUM_ANIMATIONS) -DEF_MEMPOOL(camera_pool, SceneCamera, GFX_MAX_NUM_CAMERAS) -DEF_MEMPOOL(light_pool, Light, GFX_MAX_NUM_LIGHTS) -DEF_MEMPOOL(material_pool, Material, GFX_MAX_NUM_MATERIALS) -DEF_MEMPOOL(mesh_pool, Mesh, GFX_MAX_NUM_MESHES) -DEF_MEMPOOL(mesh_link_pool, MeshLink, GFX_MAX_NUM_MESH_LINKS) -DEF_MEMPOOL(model_pool, Model, GFX_MAX_NUM_MODELS) -DEF_MEMPOOL(node_pool, SceneNode, GFX_MAX_NUM_NODES) -DEF_MEMPOOL(object_pool, SceneObject, GFX_MAX_NUM_OBJECTS) -DEF_MEMPOOL(scene_pool, Scene, GFX_MAX_NUM_SCENES) -DEF_MEMPOOL(skeleton_pool, Skeleton, GFX_MAX_NUM_SKELETONS) - -/// Scene memory. -/// -/// Holds memory pools for every type of scene object. -typedef struct SceneMemory { - anima_pool animas; - animation_pool animations; - camera_pool cameras; - light_pool lights; - material_pool materials; - mesh_pool meshs; // Purposeful typo to make the PLURAL() macro work. - mesh_link_pool mesh_links; - model_pool models; - node_pool nodes; - object_pool objects; - scene_pool scenes; - skeleton_pool skeletons; -} SceneMemory; - -static SceneMemory mem; - -#define ALLOC_DUMMY(POOL) \ - { \ - const void* object = mempool_alloc(POOL); \ - (void)object; /* Silence warning in release builds. */ \ - assert(mempool_get_block_index(POOL, object) == 0); \ - } - -#define PLURAL(name) name##s -#define MEM_FIELD(name) mem.PLURAL(name) - -void scene_mem_init() { - mempool_make(&mem.animas); - mempool_make(&mem.animations); - mempool_make(&mem.cameras); - mempool_make(&mem.lights); - mempool_make(&mem.materials); - mempool_make(&mem.meshs); - mempool_make(&mem.mesh_links); - mempool_make(&mem.models); - mempool_make(&mem.nodes); - mempool_make(&mem.objects); - mempool_make(&mem.scenes); - mempool_make(&mem.skeletons); - - // Allocate dummy objects at index 0 to guarantee that no objects allocated by - // the caller map to index 0. This allows 0 to be used as a sentinel. - ALLOC_DUMMY(&mem.animas); - ALLOC_DUMMY(&mem.animations); - ALLOC_DUMMY(&mem.cameras); - ALLOC_DUMMY(&mem.lights); - ALLOC_DUMMY(&mem.materials); - ALLOC_DUMMY(&mem.meshs); - ALLOC_DUMMY(&mem.mesh_links); - ALLOC_DUMMY(&mem.models); - ALLOC_DUMMY(&mem.nodes); - ALLOC_DUMMY(&mem.objects); - ALLOC_DUMMY(&mem.scenes); - ALLOC_DUMMY(&mem.skeletons); -} - -void scene_mem_destroy() { - // NOTE: the dummy objects are not constructed, so the destruction code below - // always skips index 0. (I don't really like the conditional inside the loop, - // but this gets the job done without having to specialize the loop macro.) -#define DESTROY(NAME) \ - mempool_foreach(&MEM_FIELD(NAME), obj, { \ - if (i > 0) { \ - gfx_destroy_##NAME(&obj); \ - } \ - }) - - // Print memory diagnostics. -#define PRINT_POOL(POOL_NAME, POOL) \ - { \ - const size_t capacity = mempool_capacity(POOL); \ - const size_t size = mempool_size(POOL); \ - const size_t block_size_bytes = mempool_block_size_bytes(POOL); \ - const size_t size_bytes = size * block_size_bytes; \ - const size_t capacity_bytes = capacity * block_size_bytes; \ - LOGI( \ - "%s pool: %lu/%lu (%lu/%lu bytes)", POOL_NAME, size, capacity, \ - size_bytes, capacity_bytes); \ - } - - LOGI("Pool diagnostics:"); - PRINT_POOL("Animas", &mem.animas); - PRINT_POOL("Animations", &mem.animations); - PRINT_POOL("Cameras", &mem.cameras); - PRINT_POOL("Lights", &mem.lights); - PRINT_POOL("Materials", &mem.materials); - PRINT_POOL("Meshes", &mem.meshs); - PRINT_POOL("Mesh links", &mem.mesh_links); - PRINT_POOL("Models", &mem.models); - PRINT_POOL("Nodes", &mem.nodes); - PRINT_POOL("Objects", &mem.objects); - PRINT_POOL("Scenes", &mem.scenes); - PRINT_POOL("Skeletons", &mem.skeletons); - - // Models contain scene elements. Destruction is handled by the remainder of - // scene destructionb elow. - // - // First destroy the scenes. This will recursively destroy the scene's nodes - // and their objects and avoid a double-free when we then destroy any stray - // scene elements. - DESTROY(scene); - // Then delete stray nodes. This will delete their children nodes and - // resource. - DESTROY(node); - // Destroy remaining scene elements. - DESTROY(anima); - // Animations are owned by animas and do not have a destructor. - DESTROY(camera); - DESTROY(light); - DESTROY(material); - DESTROY(mesh); - // Mesh links don't have a destructor. - DESTROY(object); - // Skeletons are owned by animas and do not have a destructor. -} - -#define DEF_MEMORY(NAME, TYPE) \ - /* xyz* mem_alloc_xyz(); */ \ - TYPE* mem_alloc_##NAME() { return mempool_alloc(&MEM_FIELD(NAME)); } \ - /* void mem_free_xyz(xyz**); */ \ - void mem_free_##NAME(TYPE** obj) { mempool_free(&MEM_FIELD(NAME), obj); } \ - /* xyz* mem_get_xyz(xyz_idx); */ \ - TYPE* mem_get_##NAME(NAMED_INDEX(NAME) index) { \ - assert(index.val != 0); /* 0 is the dummy allocation. */ \ - return mempool_get_block(&MEM_FIELD(NAME), index.val); \ - } \ - /* xyz_idx mem_get_xyz_index(const xyz*); */ \ - NAMED_INDEX(NAME) mem_get_##NAME##_index(const TYPE* obj) { \ - return (NAMED_INDEX(NAME)){ \ - .val = mempool_get_block_index(&MEM_FIELD(NAME), obj)}; \ - } - -DEF_MEMORY(anima, Anima) -DEF_MEMORY(animation, Animation) -DEF_MEMORY(camera, SceneCamera) -DEF_MEMORY(light, Light) -DEF_MEMORY(material, Material) -DEF_MEMORY(mesh, Mesh) -DEF_MEMORY(mesh_link, MeshLink) -DEF_MEMORY(model, Model) -DEF_MEMORY(node, SceneNode) -DEF_MEMORY(object, SceneObject) -DEF_MEMORY(scene, Scene) -DEF_MEMORY(skeleton, Skeleton) diff --git a/src/scene/scene_memory.h b/src/scene/scene_memory.h deleted file mode 100644 index d175cba..0000000 --- a/src/scene/scene_memory.h +++ /dev/null @@ -1,39 +0,0 @@ -/// Memory management of scene objects. -#pragma once - -#include "types.h" - -/// Initialize scene memory. -/// -/// The scene memory guarantees that every object maps to an index different -/// than 0. This way, 0 can be used as a special index to denote "no value". -void scene_mem_init(); - -/// Destroy the scene memory and all allocated objects. -void scene_mem_destroy(); - -#define NAMED_INDEX(name) name##_idx - -#define DECL_MEMORY(name, type) \ - typedef struct type type; \ - /* xyz* mem_alloc_xyz() */ \ - type* mem_alloc_##name(); \ - /* mem_free_xyz(xyz**) */ \ - void mem_free_##name(type**); \ - /* xyz* mem_get_xyz(xyz_idx); */ \ - type* mem_get_##name(NAMED_INDEX(name)); \ - /* xyz_idx mem_get_xyz_index(const xyz*); */ \ - NAMED_INDEX(name) mem_get_##name##_index(const type*); - -DECL_MEMORY(anima, Anima) -DECL_MEMORY(animation, Animation) -DECL_MEMORY(camera, SceneCamera) -DECL_MEMORY(light, Light) -DECL_MEMORY(material, Material) -DECL_MEMORY(mesh, Mesh) -DECL_MEMORY(mesh_link, MeshLink) -DECL_MEMORY(model, Model) -DECL_MEMORY(node, SceneNode) -DECL_MEMORY(object, SceneObject) -DECL_MEMORY(scene, Scene) -DECL_MEMORY(skeleton, Skeleton) -- cgit v1.2.3