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 --- 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 ----------- 10 files changed, 16 insertions(+), 233 deletions(-) delete mode 100644 src/scene/scene_memory.c delete mode 100644 src/scene/scene_memory.h (limited to 'src/scene') 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