diff options
Diffstat (limited to 'src/scene')
-rw-r--r-- | src/scene/animation.c | 18 | ||||
-rw-r--r-- | src/scene/camera.c | 2 | ||||
-rw-r--r-- | src/scene/model.c | 2 | ||||
-rw-r--r-- | src/scene/model_impl.h | 2 | ||||
-rw-r--r-- | src/scene/node.c | 2 | ||||
-rw-r--r-- | src/scene/object.c | 2 | ||||
-rw-r--r-- | src/scene/scene.c | 2 | ||||
-rw-r--r-- | src/scene/scene_graph.h | 2 | ||||
-rw-r--r-- | src/scene/scene_memory.c | 178 | ||||
-rw-r--r-- | src/scene/scene_memory.h | 39 |
10 files changed, 16 insertions, 233 deletions
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 @@ | |||
1 | #include "animation_impl.h" | 1 | #include "animation_impl.h" |
2 | 2 | ||
3 | #include "memory.h" | ||
3 | #include "node_impl.h" | 4 | #include "node_impl.h" |
4 | #include "scene_memory.h" | ||
5 | 5 | ||
6 | #include <string.h> | 6 | #include <string.h> |
7 | 7 | ||
@@ -504,21 +504,21 @@ Box gfx_get_joint_box( | |||
504 | return (Box){ | 504 | return (Box){ |
505 | .vertices = { | 505 | .vertices = { |
506 | mat4_mul_vec3( | 506 | mat4_mul_vec3( |
507 | joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmax.z), 1), | 507 | joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmax.z), 1), |
508 | mat4_mul_vec3( | 508 | mat4_mul_vec3( |
509 | joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmax.z), 1), | 509 | joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmax.z), 1), |
510 | mat4_mul_vec3( | 510 | mat4_mul_vec3( |
511 | joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmax.z), 1), | 511 | joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmax.z), 1), |
512 | mat4_mul_vec3( | 512 | mat4_mul_vec3( |
513 | joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmax.z), 1), | 513 | joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmax.z), 1), |
514 | mat4_mul_vec3( | 514 | mat4_mul_vec3( |
515 | joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmin.z), 1), | 515 | joint->joint_matrix, vec3_make(pmin.x, pmin.y, pmin.z), 1), |
516 | mat4_mul_vec3( | 516 | mat4_mul_vec3( |
517 | joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmin.z), 1), | 517 | joint->joint_matrix, vec3_make(pmax.x, pmin.y, pmin.z), 1), |
518 | mat4_mul_vec3( | 518 | mat4_mul_vec3( |
519 | joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmin.z), 1), | 519 | joint->joint_matrix, vec3_make(pmax.x, pmax.y, pmin.z), 1), |
520 | mat4_mul_vec3( | 520 | mat4_mul_vec3( |
521 | joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmin.z), 1), | 521 | joint->joint_matrix, vec3_make(pmin.x, pmax.y, pmin.z), 1), |
522 | } | 522 | } |
523 | }; | 523 | }; |
524 | } | 524 | } |
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 @@ | |||
1 | #include "camera_impl.h" | 1 | #include "camera_impl.h" |
2 | 2 | ||
3 | #include "memory.h" | ||
3 | #include "node_impl.h" | 4 | #include "node_impl.h" |
4 | #include "scene_memory.h" | ||
5 | 5 | ||
6 | #include <assert.h> | 6 | #include <assert.h> |
7 | 7 | ||
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 @@ | |||
2 | 2 | ||
3 | #include <gfx/scene/node.h> | 3 | #include <gfx/scene/node.h> |
4 | 4 | ||
5 | #include "scene_memory.h" | 5 | #include "memory.h" |
6 | 6 | ||
7 | #include <assert.h> | 7 | #include <assert.h> |
8 | 8 | ||
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 @@ | |||
2 | 2 | ||
3 | #include <gfx/scene/model.h> | 3 | #include <gfx/scene/model.h> |
4 | 4 | ||
5 | #include "scene_memory.h" | 5 | #include "memory.h" |
6 | 6 | ||
7 | /// Model. | 7 | /// Model. |
8 | typedef struct Model { | 8 | 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 @@ | |||
3 | #include "animation_impl.h" | 3 | #include "animation_impl.h" |
4 | #include "camera_impl.h" | 4 | #include "camera_impl.h" |
5 | #include "llr/light_impl.h" | 5 | #include "llr/light_impl.h" |
6 | #include "memory.h" | ||
6 | #include "model_impl.h" | 7 | #include "model_impl.h" |
7 | #include "object_impl.h" | 8 | #include "object_impl.h" |
8 | #include "scene_graph.h" | 9 | #include "scene_graph.h" |
9 | #include "scene_memory.h" | ||
10 | 10 | ||
11 | #include "gfx_assert.h" | 11 | #include "gfx_assert.h" |
12 | 12 | ||
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 @@ | |||
3 | #include <gfx/core.h> | 3 | #include <gfx/core.h> |
4 | 4 | ||
5 | #include "llr/mesh_impl.h" | 5 | #include "llr/mesh_impl.h" |
6 | #include "memory.h" | ||
6 | #include "node_impl.h" | 7 | #include "node_impl.h" |
7 | #include "scene_memory.h" | ||
8 | 8 | ||
9 | #include <assert.h> | 9 | #include <assert.h> |
10 | 10 | ||
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 @@ | |||
1 | #include "scene_impl.h" | 1 | #include "scene_impl.h" |
2 | 2 | ||
3 | #include "memory.h" | ||
3 | #include "node_impl.h" | 4 | #include "node_impl.h" |
4 | #include "scene_memory.h" | ||
5 | 5 | ||
6 | #include <assert.h> | 6 | #include <assert.h> |
7 | 7 | ||
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 @@ | |||
1 | /// Functions for list manipulation. | 1 | /// Functions for list manipulation. |
2 | #pragma once | 2 | #pragma once |
3 | 3 | ||
4 | #include "scene_memory.h" | 4 | #include "memory.h" |
5 | 5 | ||
6 | // NOTE: SceneMemory guarantees that index 0 can be regarded as an invalid | 6 | // NOTE: SceneMemory guarantees that index 0 can be regarded as an invalid |
7 | // index. | 7 | // 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 @@ | |||
1 | #include "scene_memory.h" | ||
2 | |||
3 | #include <gfx/sizes.h> | ||
4 | |||
5 | #include "animation_impl.h" | ||
6 | #include "camera_impl.h" | ||
7 | #include "llr/light_impl.h" | ||
8 | #include "llr/material_impl.h" | ||
9 | #include "llr/mesh_impl.h" | ||
10 | #include "model_impl.h" | ||
11 | #include "node_impl.h" | ||
12 | #include "object_impl.h" | ||
13 | #include "scene_impl.h" | ||
14 | |||
15 | #include <log/log.h> | ||
16 | #include <mempool.h> | ||
17 | |||
18 | DEF_MEMPOOL(anima_pool, Anima, GFX_MAX_NUM_ANIMAS) | ||
19 | DEF_MEMPOOL(animation_pool, Animation, GFX_MAX_NUM_ANIMATIONS) | ||
20 | DEF_MEMPOOL(camera_pool, SceneCamera, GFX_MAX_NUM_CAMERAS) | ||
21 | DEF_MEMPOOL(light_pool, Light, GFX_MAX_NUM_LIGHTS) | ||
22 | DEF_MEMPOOL(material_pool, Material, GFX_MAX_NUM_MATERIALS) | ||
23 | DEF_MEMPOOL(mesh_pool, Mesh, GFX_MAX_NUM_MESHES) | ||
24 | DEF_MEMPOOL(mesh_link_pool, MeshLink, GFX_MAX_NUM_MESH_LINKS) | ||
25 | DEF_MEMPOOL(model_pool, Model, GFX_MAX_NUM_MODELS) | ||
26 | DEF_MEMPOOL(node_pool, SceneNode, GFX_MAX_NUM_NODES) | ||
27 | DEF_MEMPOOL(object_pool, SceneObject, GFX_MAX_NUM_OBJECTS) | ||
28 | DEF_MEMPOOL(scene_pool, Scene, GFX_MAX_NUM_SCENES) | ||
29 | DEF_MEMPOOL(skeleton_pool, Skeleton, GFX_MAX_NUM_SKELETONS) | ||
30 | |||
31 | /// Scene memory. | ||
32 | /// | ||
33 | /// Holds memory pools for every type of scene object. | ||
34 | typedef struct SceneMemory { | ||
35 | anima_pool animas; | ||
36 | animation_pool animations; | ||
37 | camera_pool cameras; | ||
38 | light_pool lights; | ||
39 | material_pool materials; | ||
40 | mesh_pool meshs; // Purposeful typo to make the PLURAL() macro work. | ||
41 | mesh_link_pool mesh_links; | ||
42 | model_pool models; | ||
43 | node_pool nodes; | ||
44 | object_pool objects; | ||
45 | scene_pool scenes; | ||
46 | skeleton_pool skeletons; | ||
47 | } SceneMemory; | ||
48 | |||
49 | static SceneMemory mem; | ||
50 | |||
51 | #define ALLOC_DUMMY(POOL) \ | ||
52 | { \ | ||
53 | const void* object = mempool_alloc(POOL); \ | ||
54 | (void)object; /* Silence warning in release builds. */ \ | ||
55 | assert(mempool_get_block_index(POOL, object) == 0); \ | ||
56 | } | ||
57 | |||
58 | #define PLURAL(name) name##s | ||
59 | #define MEM_FIELD(name) mem.PLURAL(name) | ||
60 | |||
61 | void scene_mem_init() { | ||
62 | mempool_make(&mem.animas); | ||
63 | mempool_make(&mem.animations); | ||
64 | mempool_make(&mem.cameras); | ||
65 | mempool_make(&mem.lights); | ||
66 | mempool_make(&mem.materials); | ||
67 | mempool_make(&mem.meshs); | ||
68 | mempool_make(&mem.mesh_links); | ||
69 | mempool_make(&mem.models); | ||
70 | mempool_make(&mem.nodes); | ||
71 | mempool_make(&mem.objects); | ||
72 | mempool_make(&mem.scenes); | ||
73 | mempool_make(&mem.skeletons); | ||
74 | |||
75 | // Allocate dummy objects at index 0 to guarantee that no objects allocated by | ||
76 | // the caller map to index 0. This allows 0 to be used as a sentinel. | ||
77 | ALLOC_DUMMY(&mem.animas); | ||
78 | ALLOC_DUMMY(&mem.animations); | ||
79 | ALLOC_DUMMY(&mem.cameras); | ||
80 | ALLOC_DUMMY(&mem.lights); | ||
81 | ALLOC_DUMMY(&mem.materials); | ||
82 | ALLOC_DUMMY(&mem.meshs); | ||
83 | ALLOC_DUMMY(&mem.mesh_links); | ||
84 | ALLOC_DUMMY(&mem.models); | ||
85 | ALLOC_DUMMY(&mem.nodes); | ||
86 | ALLOC_DUMMY(&mem.objects); | ||
87 | ALLOC_DUMMY(&mem.scenes); | ||
88 | ALLOC_DUMMY(&mem.skeletons); | ||
89 | } | ||
90 | |||
91 | void scene_mem_destroy() { | ||
92 | // NOTE: the dummy objects are not constructed, so the destruction code below | ||
93 | // always skips index 0. (I don't really like the conditional inside the loop, | ||
94 | // but this gets the job done without having to specialize the loop macro.) | ||
95 | #define DESTROY(NAME) \ | ||
96 | mempool_foreach(&MEM_FIELD(NAME), obj, { \ | ||
97 | if (i > 0) { \ | ||
98 | gfx_destroy_##NAME(&obj); \ | ||
99 | } \ | ||
100 | }) | ||
101 | |||
102 | // Print memory diagnostics. | ||
103 | #define PRINT_POOL(POOL_NAME, POOL) \ | ||
104 | { \ | ||
105 | const size_t capacity = mempool_capacity(POOL); \ | ||
106 | const size_t size = mempool_size(POOL); \ | ||
107 | const size_t block_size_bytes = mempool_block_size_bytes(POOL); \ | ||
108 | const size_t size_bytes = size * block_size_bytes; \ | ||
109 | const size_t capacity_bytes = capacity * block_size_bytes; \ | ||
110 | LOGI( \ | ||
111 | "%s pool: %lu/%lu (%lu/%lu bytes)", POOL_NAME, size, capacity, \ | ||
112 | size_bytes, capacity_bytes); \ | ||
113 | } | ||
114 | |||
115 | LOGI("Pool diagnostics:"); | ||
116 | PRINT_POOL("Animas", &mem.animas); | ||
117 | PRINT_POOL("Animations", &mem.animations); | ||
118 | PRINT_POOL("Cameras", &mem.cameras); | ||
119 | PRINT_POOL("Lights", &mem.lights); | ||
120 | PRINT_POOL("Materials", &mem.materials); | ||
121 | PRINT_POOL("Meshes", &mem.meshs); | ||
122 | PRINT_POOL("Mesh links", &mem.mesh_links); | ||
123 | PRINT_POOL("Models", &mem.models); | ||
124 | PRINT_POOL("Nodes", &mem.nodes); | ||
125 | PRINT_POOL("Objects", &mem.objects); | ||
126 | PRINT_POOL("Scenes", &mem.scenes); | ||
127 | PRINT_POOL("Skeletons", &mem.skeletons); | ||
128 | |||
129 | // Models contain scene elements. Destruction is handled by the remainder of | ||
130 | // scene destructionb elow. | ||
131 | // | ||
132 | // First destroy the scenes. This will recursively destroy the scene's nodes | ||
133 | // and their objects and avoid a double-free when we then destroy any stray | ||
134 | // scene elements. | ||
135 | DESTROY(scene); | ||
136 | // Then delete stray nodes. This will delete their children nodes and | ||
137 | // resource. | ||
138 | DESTROY(node); | ||
139 | // Destroy remaining scene elements. | ||
140 | DESTROY(anima); | ||
141 | // Animations are owned by animas and do not have a destructor. | ||
142 | DESTROY(camera); | ||
143 | DESTROY(light); | ||
144 | DESTROY(material); | ||
145 | DESTROY(mesh); | ||
146 | // Mesh links don't have a destructor. | ||
147 | DESTROY(object); | ||
148 | // Skeletons are owned by animas and do not have a destructor. | ||
149 | } | ||
150 | |||
151 | #define DEF_MEMORY(NAME, TYPE) \ | ||
152 | /* xyz* mem_alloc_xyz(); */ \ | ||
153 | TYPE* mem_alloc_##NAME() { return mempool_alloc(&MEM_FIELD(NAME)); } \ | ||
154 | /* void mem_free_xyz(xyz**); */ \ | ||
155 | void mem_free_##NAME(TYPE** obj) { mempool_free(&MEM_FIELD(NAME), obj); } \ | ||
156 | /* xyz* mem_get_xyz(xyz_idx); */ \ | ||
157 | TYPE* mem_get_##NAME(NAMED_INDEX(NAME) index) { \ | ||
158 | assert(index.val != 0); /* 0 is the dummy allocation. */ \ | ||
159 | return mempool_get_block(&MEM_FIELD(NAME), index.val); \ | ||
160 | } \ | ||
161 | /* xyz_idx mem_get_xyz_index(const xyz*); */ \ | ||
162 | NAMED_INDEX(NAME) mem_get_##NAME##_index(const TYPE* obj) { \ | ||
163 | return (NAMED_INDEX(NAME)){ \ | ||
164 | .val = mempool_get_block_index(&MEM_FIELD(NAME), obj)}; \ | ||
165 | } | ||
166 | |||
167 | DEF_MEMORY(anima, Anima) | ||
168 | DEF_MEMORY(animation, Animation) | ||
169 | DEF_MEMORY(camera, SceneCamera) | ||
170 | DEF_MEMORY(light, Light) | ||
171 | DEF_MEMORY(material, Material) | ||
172 | DEF_MEMORY(mesh, Mesh) | ||
173 | DEF_MEMORY(mesh_link, MeshLink) | ||
174 | DEF_MEMORY(model, Model) | ||
175 | DEF_MEMORY(node, SceneNode) | ||
176 | DEF_MEMORY(object, SceneObject) | ||
177 | DEF_MEMORY(scene, Scene) | ||
178 | 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 @@ | |||
1 | /// Memory management of scene objects. | ||
2 | #pragma once | ||
3 | |||
4 | #include "types.h" | ||
5 | |||
6 | /// Initialize scene memory. | ||
7 | /// | ||
8 | /// The scene memory guarantees that every object maps to an index different | ||
9 | /// than 0. This way, 0 can be used as a special index to denote "no value". | ||
10 | void scene_mem_init(); | ||
11 | |||
12 | /// Destroy the scene memory and all allocated objects. | ||
13 | void scene_mem_destroy(); | ||
14 | |||
15 | #define NAMED_INDEX(name) name##_idx | ||
16 | |||
17 | #define DECL_MEMORY(name, type) \ | ||
18 | typedef struct type type; \ | ||
19 | /* xyz* mem_alloc_xyz() */ \ | ||
20 | type* mem_alloc_##name(); \ | ||
21 | /* mem_free_xyz(xyz**) */ \ | ||
22 | void mem_free_##name(type**); \ | ||
23 | /* xyz* mem_get_xyz(xyz_idx); */ \ | ||
24 | type* mem_get_##name(NAMED_INDEX(name)); \ | ||
25 | /* xyz_idx mem_get_xyz_index(const xyz*); */ \ | ||
26 | NAMED_INDEX(name) mem_get_##name##_index(const type*); | ||
27 | |||
28 | DECL_MEMORY(anima, Anima) | ||
29 | DECL_MEMORY(animation, Animation) | ||
30 | DECL_MEMORY(camera, SceneCamera) | ||
31 | DECL_MEMORY(light, Light) | ||
32 | DECL_MEMORY(material, Material) | ||
33 | DECL_MEMORY(mesh, Mesh) | ||
34 | DECL_MEMORY(mesh_link, MeshLink) | ||
35 | DECL_MEMORY(model, Model) | ||
36 | DECL_MEMORY(node, SceneNode) | ||
37 | DECL_MEMORY(object, SceneObject) | ||
38 | DECL_MEMORY(scene, Scene) | ||
39 | DECL_MEMORY(skeleton, Skeleton) | ||