diff options
| author | 3gg <3gg@shellblade.net> | 2023-06-02 12:03:07 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2023-06-02 12:03:07 -0700 |
| commit | 6ba8b628bf72c14372b70247e2f12df2bcd1c34e (patch) | |
| tree | eb101a1fecf000b3c96d2aa614424866a6a6d8ef | |
| parent | 6887ef315e355e1a89b8b5d7a9d3345d2444aacd (diff) | |
scene_memory refactor to make adding new types easier.
| -rw-r--r-- | gfx/include/gfx/gfx.h | 12 | ||||
| -rw-r--r-- | gfx/include/gfx/scene/scene.h | 10 | ||||
| -rw-r--r-- | gfx/src/gfx.c | 24 | ||||
| -rw-r--r-- | gfx/src/scene/scene.c | 15 | ||||
| -rw-r--r-- | gfx/src/scene/scene_impl.h | 6 | ||||
| -rw-r--r-- | gfx/src/scene/scene_memory.c | 221 | ||||
| -rw-r--r-- | gfx/src/scene/scene_memory.h | 193 | ||||
| -rw-r--r-- | gltfview/src/game.c | 2 |
8 files changed, 99 insertions, 384 deletions
diff --git a/gfx/include/gfx/gfx.h b/gfx/include/gfx/gfx.h index 303ddcd..b8f2595 100644 --- a/gfx/include/gfx/gfx.h +++ b/gfx/include/gfx/gfx.h | |||
| @@ -9,7 +9,7 @@ typedef struct SceneCamera SceneCamera; | |||
| 9 | typedef struct Gfx Gfx; | 9 | typedef struct Gfx Gfx; |
| 10 | 10 | ||
| 11 | /// Create a new graphics system, | 11 | /// Create a new graphics system, |
| 12 | Gfx* gfx_init(); | 12 | Gfx* gfx_init(void); |
| 13 | 13 | ||
| 14 | /// Destroy the graphics system. | 14 | /// Destroy the graphics system. |
| 15 | void gfx_destroy(Gfx**); | 15 | void gfx_destroy(Gfx**); |
| @@ -23,16 +23,6 @@ Renderer* gfx_get_renderer(Gfx*); | |||
| 23 | /// Get the immediate mode renderer. | 23 | /// Get the immediate mode renderer. |
| 24 | ImmRenderer* gfx_get_imm_renderer(Gfx*); | 24 | ImmRenderer* gfx_get_imm_renderer(Gfx*); |
| 25 | 25 | ||
| 26 | /// Create a new scene. | ||
| 27 | Scene* gfx_make_scene(Gfx*); | ||
| 28 | |||
| 29 | /// Destroy the scene. | ||
| 30 | /// | ||
| 31 | /// This function destroys the scene and all objects that it owns (scene | ||
| 32 | /// objects, cameras, lights, etc), but not objects that could be shared with | ||
| 33 | /// other scenes (meshes, materials, etc). | ||
| 34 | void gfx_destroy_scene(Gfx*, Scene**); | ||
| 35 | |||
| 36 | /// Remove unused resources from the scene (meshes, materials). | 26 | /// Remove unused resources from the scene (meshes, materials). |
| 37 | /// TODO: need to think about the interface for scene_purge(). Maybe this | 27 | /// TODO: need to think about the interface for scene_purge(). Maybe this |
| 38 | /// should be gfx_purge() and take a list of Scenes? | 28 | /// should be gfx_purge() and take a list of Scenes? |
diff --git a/gfx/include/gfx/scene/scene.h b/gfx/include/gfx/scene/scene.h index 5a1d823..f5c9ddf 100644 --- a/gfx/include/gfx/scene/scene.h +++ b/gfx/include/gfx/scene/scene.h | |||
| @@ -7,6 +7,16 @@ typedef struct SceneNode SceneNode; | |||
| 7 | 7 | ||
| 8 | typedef struct Scene Scene; | 8 | typedef struct Scene Scene; |
| 9 | 9 | ||
| 10 | /// Create a new scene. | ||
| 11 | Scene* gfx_make_scene(void); | ||
| 12 | |||
| 13 | /// Destroy the scene. | ||
| 14 | /// | ||
| 15 | /// This function destroys the scene and all objects that it owns (scene | ||
| 16 | /// objects, cameras, lights, etc), but not objects that could be shared with | ||
| 17 | /// other scenes (meshes, materials, etc). | ||
| 18 | void gfx_destroy_scene(Scene**); | ||
| 19 | |||
| 10 | /// Get the scene's root node. | 20 | /// Get the scene's root node. |
| 11 | SceneNode* gfx_get_scene_root(Scene*); | 21 | SceneNode* gfx_get_scene_root(Scene*); |
| 12 | 22 | ||
diff --git a/gfx/src/gfx.c b/gfx/src/gfx.c index 47f2187..fc720ed 100644 --- a/gfx/src/gfx.c +++ b/gfx/src/gfx.c | |||
| @@ -14,13 +14,12 @@ | |||
| 14 | #include <stdlib.h> | 14 | #include <stdlib.h> |
| 15 | 15 | ||
| 16 | typedef struct Gfx { | 16 | typedef struct Gfx { |
| 17 | scene_idx scene; // First child scene. | ||
| 18 | RenderBackend render_backend; | 17 | RenderBackend render_backend; |
| 19 | Renderer renderer; | 18 | Renderer renderer; |
| 20 | ImmRenderer imm_renderer; | 19 | ImmRenderer imm_renderer; |
| 21 | } Gfx; | 20 | } Gfx; |
| 22 | 21 | ||
| 23 | Gfx* gfx_init() { | 22 | Gfx* gfx_init(void) { |
| 24 | if (!gladLoadGL()) { | 23 | if (!gladLoadGL()) { |
| 25 | LOGE("Failed loading glad!"); | 24 | LOGE("Failed loading glad!"); |
| 26 | return 0; | 25 | return 0; |
| @@ -72,24 +71,3 @@ ImmRenderer* gfx_get_imm_renderer(Gfx* gfx) { | |||
| 72 | assert(gfx); | 71 | assert(gfx); |
| 73 | return &gfx->imm_renderer; | 72 | return &gfx->imm_renderer; |
| 74 | } | 73 | } |
| 75 | |||
| 76 | Scene* gfx_make_scene(Gfx* gfx) { | ||
| 77 | Scene* scene = mem_alloc_scene(); | ||
| 78 | if (!scene) { | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | scene_make(scene); | ||
| 82 | LIST_PREPEND(gfx->scene, scene); | ||
| 83 | return scene; | ||
| 84 | } | ||
| 85 | |||
| 86 | void gfx_destroy_scene(Gfx* gfx, Scene** scene) { | ||
| 87 | assert(scene); | ||
| 88 | scene_destroy(*scene); | ||
| 89 | const scene_idx scene_index = mem_get_scene_index(*scene); | ||
| 90 | if (gfx->scene.val == scene_index.val) { | ||
| 91 | gfx->scene.val = 0; | ||
| 92 | } | ||
| 93 | LIST_REMOVE(*scene); | ||
| 94 | mem_free_scene(scene); | ||
| 95 | } | ||
diff --git a/gfx/src/scene/scene.c b/gfx/src/scene/scene.c index 19b9379..c3dae9c 100644 --- a/gfx/src/scene/scene.c +++ b/gfx/src/scene/scene.c | |||
| @@ -1,18 +1,25 @@ | |||
| 1 | #include "scene_impl.h" | 1 | #include "scene_impl.h" |
| 2 | 2 | ||
| 3 | #include "node_impl.h" | 3 | #include "node_impl.h" |
| 4 | #include "scene_memory.h" | ||
| 4 | 5 | ||
| 5 | #include <assert.h> | 6 | #include <assert.h> |
| 6 | 7 | ||
| 7 | void scene_make(Scene* scene) { | 8 | Scene* gfx_make_scene(void) { |
| 8 | assert(scene); | 9 | Scene* scene = mem_alloc_scene(); |
| 10 | if (!scene) { | ||
| 11 | return 0; | ||
| 12 | } | ||
| 9 | scene->root = gfx_make_node(); | 13 | scene->root = gfx_make_node(); |
| 10 | assert(scene->root); | 14 | assert(scene->root); |
| 15 | return scene; | ||
| 11 | } | 16 | } |
| 12 | 17 | ||
| 13 | void scene_destroy(Scene* scene) { | 18 | void gfx_destroy_scene(Scene** scene) { |
| 14 | assert(scene); | 19 | assert(scene); |
| 15 | gfx_destroy_node(&scene->root); | 20 | assert(*scene); |
| 21 | gfx_destroy_node(&(*scene)->root); | ||
| 22 | mem_free_scene(scene); | ||
| 16 | } | 23 | } |
| 17 | 24 | ||
| 18 | SceneNode* gfx_get_scene_root(Scene* scene) { | 25 | SceneNode* gfx_get_scene_root(Scene* scene) { |
diff --git a/gfx/src/scene/scene_impl.h b/gfx/src/scene/scene_impl.h index 25ec8da..992f620 100644 --- a/gfx/src/scene/scene_impl.h +++ b/gfx/src/scene/scene_impl.h | |||
| @@ -11,9 +11,3 @@ typedef struct Scene { | |||
| 11 | scene_idx next; | 11 | scene_idx next; |
| 12 | scene_idx prev; | 12 | scene_idx prev; |
| 13 | } Scene; | 13 | } Scene; |
| 14 | |||
| 15 | /// Create a new scene. | ||
| 16 | void scene_make(Scene*); | ||
| 17 | |||
| 18 | /// Destroy the scene. | ||
| 19 | void scene_destroy(Scene*); | ||
diff --git a/gfx/src/scene/scene_memory.c b/gfx/src/scene/scene_memory.c index 217c719..ecd9434 100644 --- a/gfx/src/scene/scene_memory.c +++ b/gfx/src/scene/scene_memory.c | |||
| @@ -10,7 +10,6 @@ | |||
| 10 | #include "node_impl.h" | 10 | #include "node_impl.h" |
| 11 | #include "object_impl.h" | 11 | #include "object_impl.h" |
| 12 | #include "scene_impl.h" | 12 | #include "scene_impl.h" |
| 13 | #include "types.h" | ||
| 14 | 13 | ||
| 15 | #include <mempool.h> | 14 | #include <mempool.h> |
| 16 | 15 | ||
| @@ -37,7 +36,7 @@ typedef struct SceneMemory { | |||
| 37 | joint_pool joints; | 36 | joint_pool joints; |
| 38 | light_pool lights; | 37 | light_pool lights; |
| 39 | material_pool materials; | 38 | material_pool materials; |
| 40 | mesh_pool meshes; | 39 | mesh_pool meshs; // Purposeful typo to make the PLURAL() macro work. |
| 41 | mesh_link_pool mesh_links; | 40 | mesh_link_pool mesh_links; |
| 42 | node_pool nodes; | 41 | node_pool nodes; |
| 43 | object_pool objects; | 42 | object_pool objects; |
| @@ -50,6 +49,9 @@ static SceneMemory mem; | |||
| 50 | #define ALLOC_DUMMY(POOL) \ | 49 | #define ALLOC_DUMMY(POOL) \ |
| 51 | assert(mempool_get_block_index(POOL, mempool_alloc(POOL)) == 0) | 50 | assert(mempool_get_block_index(POOL, mempool_alloc(POOL)) == 0) |
| 52 | 51 | ||
| 52 | #define PLURAL(name) name##s | ||
| 53 | #define MEM_FIELD(name) mem.PLURAL(name) | ||
| 54 | |||
| 53 | void scene_mem_init() { | 55 | void scene_mem_init() { |
| 54 | mempool_make(&mem.animas); | 56 | mempool_make(&mem.animas); |
| 55 | mempool_make(&mem.animations); | 57 | mempool_make(&mem.animations); |
| @@ -57,7 +59,7 @@ void scene_mem_init() { | |||
| 57 | mempool_make(&mem.joints); | 59 | mempool_make(&mem.joints); |
| 58 | mempool_make(&mem.lights); | 60 | mempool_make(&mem.lights); |
| 59 | mempool_make(&mem.materials); | 61 | mempool_make(&mem.materials); |
| 60 | mempool_make(&mem.meshes); | 62 | mempool_make(&mem.meshs); |
| 61 | mempool_make(&mem.mesh_links); | 63 | mempool_make(&mem.mesh_links); |
| 62 | mempool_make(&mem.nodes); | 64 | mempool_make(&mem.nodes); |
| 63 | mempool_make(&mem.objects); | 65 | mempool_make(&mem.objects); |
| @@ -72,7 +74,7 @@ void scene_mem_init() { | |||
| 72 | ALLOC_DUMMY(&mem.joints); | 74 | ALLOC_DUMMY(&mem.joints); |
| 73 | ALLOC_DUMMY(&mem.lights); | 75 | ALLOC_DUMMY(&mem.lights); |
| 74 | ALLOC_DUMMY(&mem.materials); | 76 | ALLOC_DUMMY(&mem.materials); |
| 75 | ALLOC_DUMMY(&mem.meshes); | 77 | ALLOC_DUMMY(&mem.meshs); |
| 76 | ALLOC_DUMMY(&mem.mesh_links); | 78 | ALLOC_DUMMY(&mem.mesh_links); |
| 77 | ALLOC_DUMMY(&mem.nodes); | 79 | ALLOC_DUMMY(&mem.nodes); |
| 78 | ALLOC_DUMMY(&mem.objects); | 80 | ALLOC_DUMMY(&mem.objects); |
| @@ -84,182 +86,59 @@ void scene_mem_destroy() { | |||
| 84 | // NOTE: the dummy objects are not constructed, so the destruction code below | 86 | // NOTE: the dummy objects are not constructed, so the destruction code below |
| 85 | // always skips index 0. (I don't really like the conditional inside the loop, | 87 | // always skips index 0. (I don't really like the conditional inside the loop, |
| 86 | // but this gets the job done without having to specialize the loop macro.) | 88 | // but this gets the job done without having to specialize the loop macro.) |
| 87 | // | 89 | #define DESTROY(name) \ |
| 90 | mempool_foreach(&MEM_FIELD(name), obj, { \ | ||
| 91 | if (i > 0) { \ | ||
| 92 | gfx_destroy_##name(&obj); \ | ||
| 93 | } \ | ||
| 94 | }) | ||
| 95 | |||
| 88 | // First destroy the scenes. This will recursively destroy the scene's nodes | 96 | // First destroy the scenes. This will recursively destroy the scene's nodes |
| 89 | // and their objects and avoid a double-free when we then destroy any stray | 97 | // and their objects and avoid a double-free when we then destroy any stray |
| 90 | // scene elements. | 98 | // scene elements. |
| 91 | mempool_foreach(&mem.scenes, scene, { | 99 | DESTROY(scene); |
| 92 | if (i > 0) { | ||
| 93 | scene_destroy(scene); | ||
| 94 | } | ||
| 95 | }); | ||
| 96 | // Then delete stray nodes. This will delete their children nodes and | 100 | // Then delete stray nodes. This will delete their children nodes and |
| 97 | // resource. | 101 | // resource. |
| 98 | mempool_foreach(&mem.nodes, node, { | 102 | DESTROY(node); |
| 99 | if (i > 0) { | ||
| 100 | gfx_destroy_node(&node); | ||
| 101 | } | ||
| 102 | }); | ||
| 103 | // Destroy remaining scene elements. | 103 | // Destroy remaining scene elements. |
| 104 | mempool_foreach(&mem.animas, anima, { | 104 | DESTROY(anima); |
| 105 | if (i > 0) { | ||
| 106 | gfx_destroy_anima(&anima); | ||
| 107 | } | ||
| 108 | }); | ||
| 109 | // Animations are owned by animas and do not have a destructor. | 105 | // Animations are owned by animas and do not have a destructor. |
| 110 | mempool_foreach(&mem.cameras, camera, { | 106 | DESTROY(camera); |
| 111 | if (i > 0) { | 107 | DESTROY(joint); |
| 112 | gfx_destroy_camera(&camera); | 108 | DESTROY(light); |
| 113 | } | 109 | DESTROY(material); |
| 114 | }); | 110 | DESTROY(mesh); |
| 115 | mempool_foreach(&mem.joints, joint, { | ||
| 116 | if (i > 0) { | ||
| 117 | gfx_destroy_joint(&joint); | ||
| 118 | } | ||
| 119 | }); | ||
| 120 | mempool_foreach(&mem.lights, light, { | ||
| 121 | if (i > 0) { | ||
| 122 | gfx_destroy_light(&light); | ||
| 123 | } | ||
| 124 | }); | ||
| 125 | mempool_foreach(&mem.materials, material, { | ||
| 126 | if (i > 0) { | ||
| 127 | gfx_destroy_material(&material); | ||
| 128 | } | ||
| 129 | }); | ||
| 130 | mempool_foreach(&mem.meshes, mesh, { | ||
| 131 | if (i > 0) { | ||
| 132 | gfx_destroy_mesh(&mesh); | ||
| 133 | } | ||
| 134 | }); | ||
| 135 | // Mesh links don't have a destructor. | 111 | // Mesh links don't have a destructor. |
| 136 | mempool_foreach(&mem.objects, object, { | 112 | DESTROY(object); |
| 137 | if (i > 0) { | ||
| 138 | gfx_destroy_object(&object); | ||
| 139 | } | ||
| 140 | }); | ||
| 141 | // Skeletons are owned by animas and do not have a destructor. | 113 | // Skeletons are owned by animas and do not have a destructor. |
| 142 | } | 114 | } |
| 143 | 115 | ||
| 144 | // Memory allocation. | 116 | #define DEF_MEMORY(name, type) \ |
| 145 | Anima* mem_alloc_anima() { return mempool_alloc(&mem.animas); } | 117 | /* xyz* mem_alloc_xyz(); */ \ |
| 146 | Animation* mem_alloc_animation() { return mempool_alloc(&mem.animations); } | 118 | type* mem_alloc_##name() { return mempool_alloc(&MEM_FIELD(name)); } \ |
| 147 | SceneCamera* mem_alloc_camera() { return mempool_alloc(&mem.cameras); } | 119 | /* void mem_free_xyz(xyz**); */ \ |
| 148 | Joint* mem_alloc_joint() { return mempool_alloc(&mem.joints); } | 120 | void mem_free_##name(type** obj) { mempool_free(&MEM_FIELD(name), obj); } \ |
| 149 | Light* mem_alloc_light() { return mempool_alloc(&mem.lights); } | 121 | /* xyz* mem_get_xyz(xyz_idx); */ \ |
| 150 | Material* mem_alloc_material() { return mempool_alloc(&mem.materials); } | 122 | /* TODO: Check for 0 index and return nullptr? Otherwise this can \ |
| 151 | Mesh* mem_alloc_mesh() { return mempool_alloc(&mem.meshes); } | 123 | * accidentally return a pointer to the dummy objects. */ \ |
| 152 | MeshLink* mem_alloc_mesh_link() { return mempool_alloc(&mem.mesh_links); } | 124 | type* mem_get_##name(NAMED_INDEX(name) index) { \ |
| 153 | SceneNode* mem_alloc_node() { return mempool_alloc(&mem.nodes); } | 125 | return mempool_get_block(&MEM_FIELD(name), index.val); \ |
| 154 | SceneObject* mem_alloc_object() { return mempool_alloc(&mem.objects); } | 126 | } \ |
| 155 | Scene* mem_alloc_scene() { return mempool_alloc(&mem.scenes); } | 127 | /* xyz_idx mem_get_xyz_index(const xyz*); */ \ |
| 156 | Skeleton* mem_alloc_skeleton() { return mempool_alloc(&mem.skeletons); } | 128 | NAMED_INDEX(name) mem_get_##name##_index(const type* obj) { \ |
| 157 | 129 | return (NAMED_INDEX(name)){ \ | |
| 158 | // Memory free. | 130 | .val = mempool_get_block_index(&MEM_FIELD(name), obj)}; \ |
| 159 | void mem_free_anima(Anima** anima) { mempool_free(&mem.animas, anima); } | 131 | } |
| 160 | void mem_free_animation(Animation** animation) { | ||
| 161 | mempool_free(&mem.animations, animation); | ||
| 162 | } | ||
| 163 | void mem_free_camera(SceneCamera** camera) { | ||
| 164 | mempool_free(&mem.cameras, camera); | ||
| 165 | } | ||
| 166 | void mem_free_joint(Joint** joint) { mempool_free(&mem.joints, joint); } | ||
| 167 | void mem_free_light(Light** light) { mempool_free(&mem.lights, light); } | ||
| 168 | void mem_free_material(Material** material) { | ||
| 169 | mempool_free(&mem.materials, material); | ||
| 170 | } | ||
| 171 | void mem_free_mesh(Mesh** mesh) { mempool_free(&mem.meshes, mesh); } | ||
| 172 | void mem_free_mesh_link(MeshLink** mesh_link) { | ||
| 173 | mempool_free(&mem.mesh_links, mesh_link); | ||
| 174 | } | ||
| 175 | void mem_free_node(SceneNode** node) { mempool_free(&mem.nodes, node); } | ||
| 176 | void mem_free_object(SceneObject** object) { | ||
| 177 | mempool_free(&mem.objects, object); | ||
| 178 | } | ||
| 179 | void mem_free_scene(Scene** scene) { mempool_free(&mem.scenes, scene); } | ||
| 180 | void mem_free_skeleton(Skeleton** skeleton) { | ||
| 181 | mempool_free(&mem.skeletons, skeleton); | ||
| 182 | } | ||
| 183 | |||
| 184 | // Query by index. | ||
| 185 | // | ||
| 186 | // TODO: Check for 0 index and return nullptr? Otherwise this can accidentally | ||
| 187 | // return a pointer to the dummy objects. | ||
| 188 | Anima* mem_get_anima(anima_idx index) { | ||
| 189 | return mempool_get_block(&mem.animas, index.val); | ||
| 190 | } | ||
| 191 | Animation* mem_get_animation(animation_idx index) { | ||
| 192 | return mempool_get_block(&mem.animations, index.val); | ||
| 193 | } | ||
| 194 | SceneCamera* mem_get_camera(camera_idx index) { | ||
| 195 | return mempool_get_block(&mem.cameras, index.val); | ||
| 196 | } | ||
| 197 | Joint* mem_get_joint(joint_idx index) { | ||
| 198 | return mempool_get_block(&mem.joints, index.val); | ||
| 199 | } | ||
| 200 | Light* mem_get_light(light_idx index) { | ||
| 201 | return mempool_get_block(&mem.lights, index.val); | ||
| 202 | } | ||
| 203 | Material* mem_get_material(material_idx index) { | ||
| 204 | return mempool_get_block(&mem.materials, index.val); | ||
| 205 | } | ||
| 206 | Mesh* mem_get_mesh(mesh_idx index) { | ||
| 207 | return mempool_get_block(&mem.meshes, index.val); | ||
| 208 | } | ||
| 209 | MeshLink* mem_get_mesh_link(mesh_link_idx index) { | ||
| 210 | return mempool_get_block(&mem.mesh_links, index.val); | ||
| 211 | } | ||
| 212 | SceneNode* mem_get_node(node_idx index) { | ||
| 213 | return mempool_get_block(&mem.nodes, index.val); | ||
| 214 | } | ||
| 215 | SceneObject* mem_get_object(object_idx index) { | ||
| 216 | return mempool_get_block(&mem.objects, index.val); | ||
| 217 | } | ||
| 218 | Scene* mem_get_scene(scene_idx index) { | ||
| 219 | return mempool_get_block(&mem.scenes, index.val); | ||
| 220 | } | ||
| 221 | Skeleton* mem_get_skeleton(skeleton_idx index) { | ||
| 222 | return mempool_get_block(&mem.skeletons, index.val); | ||
| 223 | } | ||
| 224 | 132 | ||
| 225 | // Map object to index. | 133 | DEF_MEMORY(anima, Anima) |
| 226 | anima_idx mem_get_anima_index(const Anima* anima) { | 134 | DEF_MEMORY(animation, Animation) |
| 227 | return (anima_idx){.val = mempool_get_block_index(&mem.animas, anima)}; | 135 | DEF_MEMORY(camera, SceneCamera) |
| 228 | } | 136 | DEF_MEMORY(joint, Joint) |
| 229 | animation_idx mem_get_animation_index(const Animation* animation) { | 137 | DEF_MEMORY(light, Light) |
| 230 | return (animation_idx){ | 138 | DEF_MEMORY(material, Material) |
| 231 | .val = mempool_get_block_index(&mem.animations, animation)}; | 139 | DEF_MEMORY(mesh, Mesh) |
| 232 | } | 140 | DEF_MEMORY(mesh_link, MeshLink) |
| 233 | camera_idx mem_get_camera_index(const SceneCamera* camera) { | 141 | DEF_MEMORY(node, SceneNode) |
| 234 | return (camera_idx){.val = mempool_get_block_index(&mem.cameras, camera)}; | 142 | DEF_MEMORY(object, SceneObject) |
| 235 | } | 143 | DEF_MEMORY(scene, Scene) |
| 236 | joint_idx mem_get_joint_index(const Joint* joint) { | 144 | DEF_MEMORY(skeleton, Skeleton) |
| 237 | return (joint_idx){.val = mempool_get_block_index(&mem.joints, joint)}; | ||
| 238 | } | ||
| 239 | light_idx mem_get_light_index(const Light* light) { | ||
| 240 | return (light_idx){.val = mempool_get_block_index(&mem.lights, light)}; | ||
| 241 | } | ||
| 242 | material_idx mem_get_material_index(const Material* material) { | ||
| 243 | return (material_idx){ | ||
| 244 | .val = mempool_get_block_index(&mem.materials, material)}; | ||
| 245 | } | ||
| 246 | mesh_idx mem_get_mesh_index(const Mesh* mesh) { | ||
| 247 | return (mesh_idx){.val = mempool_get_block_index(&mem.meshes, mesh)}; | ||
| 248 | } | ||
| 249 | mesh_link_idx mem_get_mesh_link_index(const MeshLink* mesh_link) { | ||
| 250 | return (mesh_link_idx){ | ||
| 251 | .val = mempool_get_block_index(&mem.mesh_links, mesh_link)}; | ||
| 252 | } | ||
| 253 | node_idx mem_get_node_index(const SceneNode* node) { | ||
| 254 | return (node_idx){.val = mempool_get_block_index(&mem.nodes, node)}; | ||
| 255 | } | ||
| 256 | object_idx mem_get_object_index(const SceneObject* object) { | ||
| 257 | return (object_idx){.val = mempool_get_block_index(&mem.objects, object)}; | ||
| 258 | } | ||
| 259 | scene_idx mem_get_scene_index(const Scene* scene) { | ||
| 260 | return (scene_idx){.val = mempool_get_block_index(&mem.scenes, scene)}; | ||
| 261 | } | ||
| 262 | skeleton_idx mem_get_skeleton_index(const Skeleton* skeleton) { | ||
| 263 | return (skeleton_idx){ | ||
| 264 | .val = mempool_get_block_index(&mem.skeletons, skeleton)}; | ||
| 265 | } | ||
diff --git a/gfx/src/scene/scene_memory.h b/gfx/src/scene/scene_memory.h index 197e4ca..9486ee6 100644 --- a/gfx/src/scene/scene_memory.h +++ b/gfx/src/scene/scene_memory.h | |||
| @@ -4,19 +4,6 @@ | |||
| 4 | #include "animation_impl.h" | 4 | #include "animation_impl.h" |
| 5 | #include "types.h" | 5 | #include "types.h" |
| 6 | 6 | ||
| 7 | typedef struct Anima Anima; | ||
| 8 | typedef struct Animation Animation; | ||
| 9 | typedef struct Joint Joint; | ||
| 10 | typedef struct Light Light; | ||
| 11 | typedef struct Material Material; | ||
| 12 | typedef struct Mesh Mesh; | ||
| 13 | typedef struct MeshLink MeshLink; | ||
| 14 | typedef struct SceneCamera SceneCamera; | ||
| 15 | typedef struct SceneNode SceneNode; | ||
| 16 | typedef struct SceneObject SceneObject; | ||
| 17 | typedef struct Scene Scene; | ||
| 18 | typedef struct Skeleton Skeleton; | ||
| 19 | |||
| 20 | /// Initialize scene memory. | 7 | /// Initialize scene memory. |
| 21 | /// | 8 | /// |
| 22 | /// The scene memory guarantees that every object maps to an index different | 9 | /// The scene memory guarantees that every object maps to an index different |
| @@ -26,158 +13,28 @@ void scene_mem_init(); | |||
| 26 | /// Destroy the scene memory and all allocated objects. | 13 | /// Destroy the scene memory and all allocated objects. |
| 27 | void scene_mem_destroy(); | 14 | void scene_mem_destroy(); |
| 28 | 15 | ||
| 29 | /// ---------------------------------------------------------------------------- | 16 | #define NAMED_INDEX(name) name##_idx |
| 30 | /// Memory allocation. | 17 | |
| 31 | /// ---------------------------------------------------------------------------- | 18 | #define DECL_MEMORY(name, type) \ |
| 32 | 19 | typedef struct type type; \ | |
| 33 | /// Allocate space for an anima. | 20 | /* xyz* mem_alloc_xyz() */ \ |
| 34 | Anima* mem_alloc_anima(); | 21 | type* mem_alloc_##name(); \ |
| 35 | 22 | /* mem_free_xyz(xyz**) */ \ | |
| 36 | /// Allocate space for an animation. | 23 | void mem_free_##name(type**); \ |
| 37 | Animation* mem_alloc_animation(); | 24 | /* xyz* mem_get_xyz(xyz_idx); */ \ |
| 38 | 25 | type* mem_get_##name(NAMED_INDEX(name)); \ | |
| 39 | /// Allocate space for a camera. | 26 | /* xyz_idx mem_get_xyz_index(const xyz*); */ \ |
| 40 | SceneCamera* mem_alloc_camera(); | 27 | NAMED_INDEX(name) mem_get_##name##_index(const type*); |
| 41 | 28 | ||
| 42 | /// Allocate space for a joint. | 29 | DECL_MEMORY(anima, Anima) |
| 43 | Joint* mem_alloc_joint(); | 30 | DECL_MEMORY(animation, Animation) |
| 44 | 31 | DECL_MEMORY(camera, SceneCamera) | |
| 45 | /// Allocate space for a light. | 32 | DECL_MEMORY(joint, Joint) |
| 46 | Light* mem_alloc_light(); | 33 | DECL_MEMORY(light, Light) |
| 47 | 34 | DECL_MEMORY(material, Material) | |
| 48 | /// Allocate space for a material. | 35 | DECL_MEMORY(mesh, Mesh) |
| 49 | Material* mem_alloc_material(); | 36 | DECL_MEMORY(mesh_link, MeshLink) |
| 50 | 37 | DECL_MEMORY(node, SceneNode) | |
| 51 | /// Allocate space for a mesh. | 38 | DECL_MEMORY(object, SceneObject) |
| 52 | Mesh* mem_alloc_mesh(); | 39 | DECL_MEMORY(scene, Scene) |
| 53 | 40 | DECL_MEMORY(skeleton, Skeleton) | |
| 54 | /// Allocate space for a mesh link. | ||
| 55 | MeshLink* mem_alloc_mesh_link(); | ||
| 56 | |||
| 57 | /// Allocate space for a scene node. | ||
| 58 | SceneNode* mem_alloc_node(); | ||
| 59 | |||
| 60 | /// Allocate space for a scene object. | ||
| 61 | SceneObject* mem_alloc_object(); | ||
| 62 | |||
| 63 | /// Allocate space for a scene. | ||
| 64 | Scene* mem_alloc_scene(); | ||
| 65 | |||
| 66 | /// Allocate space for a skeleton. | ||
| 67 | Skeleton* mem_alloc_skeleton(); | ||
| 68 | |||
| 69 | /// Free the anima. | ||
| 70 | void mem_free_anima(Anima**); | ||
| 71 | |||
| 72 | /// Free the animation. | ||
| 73 | void mem_free_animation(Animation**); | ||
| 74 | |||
| 75 | /// Free the camera. | ||
| 76 | void mem_free_camera(SceneCamera**); | ||
| 77 | |||
| 78 | /// Free the joint. | ||
| 79 | void mem_free_joint(Joint**); | ||
| 80 | |||
| 81 | /// Free the light. | ||
| 82 | void mem_free_light(Light**); | ||
| 83 | |||
| 84 | /// Free the material. | ||
| 85 | void mem_free_material(Material**); | ||
| 86 | |||
| 87 | /// Free the mesh. | ||
| 88 | void mem_free_mesh(Mesh**); | ||
| 89 | |||
| 90 | /// Free the mesh link. | ||
| 91 | void mem_free_mesh_link(MeshLink**); | ||
| 92 | |||
| 93 | /// Free the scene node. | ||
| 94 | void mem_free_node(SceneNode**); | ||
| 95 | |||
| 96 | /// Free the scene object. | ||
| 97 | void mem_free_object(SceneObject**); | ||
| 98 | |||
| 99 | /// Free the scene. | ||
| 100 | void mem_free_scene(Scene**); | ||
| 101 | |||
| 102 | /// Free the skeleton. | ||
| 103 | void mem_free_skeleton(Skeleton**); | ||
| 104 | |||
| 105 | /// ---------------------------------------------------------------------------- | ||
| 106 | /// Query objects by index. | ||
| 107 | /// ---------------------------------------------------------------------------- | ||
| 108 | |||
| 109 | /// Get an anima by index. | ||
| 110 | Anima* mem_get_anima(anima_idx); | ||
| 111 | |||
| 112 | /// Get an animation by index. | ||
| 113 | Animation* mem_get_animation(animation_idx); | ||
| 114 | |||
| 115 | /// Get a camera by index. | ||
| 116 | SceneCamera* mem_get_camera(camera_idx); | ||
| 117 | |||
| 118 | /// Get a joint by index. | ||
| 119 | Joint* mem_get_joint(joint_idx); | ||
| 120 | |||
| 121 | /// Get a light by index. | ||
| 122 | Light* mem_get_light(light_idx); | ||
| 123 | |||
| 124 | /// Get a material by index. | ||
| 125 | Material* mem_get_material(material_idx); | ||
| 126 | |||
| 127 | /// Get a mesh by index. | ||
| 128 | Mesh* mem_get_mesh(mesh_idx); | ||
| 129 | |||
| 130 | /// Get a mesh link by index. | ||
| 131 | MeshLink* mem_get_mesh_link(mesh_link_idx); | ||
| 132 | |||
| 133 | /// Get a node by index. | ||
| 134 | SceneNode* mem_get_node(node_idx); | ||
| 135 | |||
| 136 | /// Get an object by index. | ||
| 137 | SceneObject* mem_get_object(object_idx); | ||
| 138 | |||
| 139 | /// Get a scene by index. | ||
| 140 | Scene* mem_get_scene(scene_idx); | ||
| 141 | |||
| 142 | /// Get a skeleton by index. | ||
| 143 | Skeleton* mem_get_skeleton(skeleton_idx); | ||
| 144 | |||
| 145 | /// ---------------------------------------------------------------------------- | ||
| 146 | /// Map objects to indices. | ||
| 147 | /// ---------------------------------------------------------------------------- | ||
| 148 | |||
| 149 | /// Get the anima's index. | ||
| 150 | anima_idx mem_get_anima_index(const Anima*); | ||
| 151 | |||
| 152 | /// Get the animation's index. | ||
| 153 | animation_idx mem_get_animation_index(const Animation*); | ||
| 154 | |||
| 155 | /// Get the camera's index. | ||
| 156 | camera_idx mem_get_camera_index(const SceneCamera*); | ||
| 157 | |||
| 158 | /// Get the joint's index. | ||
| 159 | joint_idx mem_get_joint_index(const Joint*); | ||
| 160 | |||
| 161 | /// Get the light's index. | ||
| 162 | light_idx mem_get_light_index(const Light*); | ||
| 163 | |||
| 164 | /// Get the material's index. | ||
| 165 | material_idx mem_get_material_index(const Material*); | ||
| 166 | |||
| 167 | /// Get the mesh's index. | ||
| 168 | mesh_idx mem_get_mesh_index(const Mesh*); | ||
| 169 | |||
| 170 | /// Get the mesh link's index. | ||
| 171 | mesh_link_idx mem_get_mesh_link_index(const MeshLink*); | ||
| 172 | |||
| 173 | /// Get the node's index. | ||
| 174 | node_idx mem_get_node_index(const SceneNode*); | ||
| 175 | |||
| 176 | /// Get the object's index. | ||
| 177 | object_idx mem_get_object_index(const SceneObject*); | ||
| 178 | |||
| 179 | /// Get the scene's index. | ||
| 180 | scene_idx mem_get_scene_index(const Scene*); | ||
| 181 | |||
| 182 | /// Get the skeleton's index. | ||
| 183 | skeleton_idx mem_get_skeleton_index(const Skeleton*); | ||
diff --git a/gltfview/src/game.c b/gltfview/src/game.c index 20f33bb..9709ea5 100644 --- a/gltfview/src/game.c +++ b/gltfview/src/game.c | |||
| @@ -194,7 +194,7 @@ bool game_new(Game* game, int argc, const char** argv) { | |||
| 194 | game->render_backend = gfx_get_render_backend(game->gfx); | 194 | game->render_backend = gfx_get_render_backend(game->gfx); |
| 195 | game->renderer = gfx_get_renderer(game->gfx); | 195 | game->renderer = gfx_get_renderer(game->gfx); |
| 196 | 196 | ||
| 197 | game->scene = gfx_make_scene(game->gfx); | 197 | game->scene = gfx_make_scene(); |
| 198 | if (!game->scene) { | 198 | if (!game->scene) { |
| 199 | goto cleanup; | 199 | goto cleanup; |
| 200 | } | 200 | } |
