summaryrefslogtreecommitdiff
path: root/gfx/src/asset/asset_cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'gfx/src/asset/asset_cache.c')
-rw-r--r--gfx/src/asset/asset_cache.c47
1 files changed, 17 insertions, 30 deletions
diff --git a/gfx/src/asset/asset_cache.c b/gfx/src/asset/asset_cache.c
index 0c6a8dc..2fa57ad 100644
--- a/gfx/src/asset/asset_cache.c
+++ b/gfx/src/asset/asset_cache.c
@@ -4,13 +4,11 @@
4#include <gfx/gfx.h> 4#include <gfx/gfx.h>
5#include <gfx/util/scene.h> 5#include <gfx/util/scene.h>
6#include <gfx/util/texture.h> 6#include <gfx/util/texture.h>
7#include <gfx_assert.h>
7 8
8#include <cstring.h> 9#include <cstring.h>
9#include <log/log.h> 10#include <log/log.h>
10 11
11#include <assert.h>
12#include <stddef.h>
13
14static Hash calc_scene_hash(const LoadSceneCmd* cmd) { 12static Hash calc_scene_hash(const LoadSceneCmd* cmd) {
15 assert(cmd); 13 assert(cmd);
16 switch (cmd->origin) { 14 switch (cmd->origin) {
@@ -19,7 +17,7 @@ static Hash calc_scene_hash(const LoadSceneCmd* cmd) {
19 case AssetFromMemory: 17 case AssetFromMemory:
20 return (Hash)cmd->data; 18 return (Hash)cmd->data;
21 } 19 }
22 assert(false); 20 FAIL("Unhandled scene asset origin");
23 return 0; 21 return 0;
24} 22}
25 23
@@ -59,7 +57,7 @@ static Hash calc_texture_hash(const LoadTextureCmd* cmd) {
59 } 57 }
60 break; 58 break;
61 } 59 }
62 assert(false); 60 FAIL("Unhandled texture asset origin");
63 return 0; 61 return 0;
64} 62}
65 63
@@ -73,25 +71,16 @@ static Asset* lookup_cache(AssetCache* cache, Hash hash) {
73 return 0; 71 return 0;
74} 72}
75 73
76// TODO: questionable function. Make mempool_alloc() fail when out of memory.
77static void insert_into_cache(AssetCache* cache, const Asset* asset) {
78 assert(cache);
79 assert(asset);
80 Asset* poolAsset = mempool_alloc(&cache->assets);
81 assert(asset);
82 *poolAsset = *asset;
83}
84
85static void log_scene_cache_hit(const LoadSceneCmd* cmd, Hash hash) { 74static void log_scene_cache_hit(const LoadSceneCmd* cmd, Hash hash) {
86 assert(cmd); 75 assert(cmd);
87 switch (cmd->origin) { 76 switch (cmd->origin) {
88 case AssetFromFile: 77 case AssetFromFile:
89 LOGI( 78 LOGD(
90 "Found asset [%s] in cache with hash [%lu]", 79 "Found asset [%s] in cache with hash [%lu]",
91 mstring_cstr(&cmd->filepath), hash); 80 mstring_cstr(&cmd->filepath), hash);
92 break; 81 break;
93 case AssetFromMemory: 82 case AssetFromMemory:
94 LOGI("Found asset [%p] in cache with hash [%lu]", cmd->data, hash); 83 LOGD("Found asset [%p] in cache with hash [%lu]", cmd->data, hash);
95 break; 84 break;
96 } 85 }
97} 86}
@@ -100,10 +89,10 @@ static void log_scene_loaded(const LoadSceneCmd* cmd) {
100 assert(cmd); 89 assert(cmd);
101 switch (cmd->origin) { 90 switch (cmd->origin) {
102 case AssetFromFile: 91 case AssetFromFile:
103 LOGI("Loaded asset from file: [%s]", mstring_cstr(&cmd->filepath)); 92 LOGD("Loaded asset from file: [%s]", mstring_cstr(&cmd->filepath));
104 break; 93 break;
105 case AssetFromMemory: 94 case AssetFromMemory:
106 LOGI("Loaded asset from memory: [%p]", cmd->data); 95 LOGD("Loaded asset from memory: [%p]", cmd->data);
107 break; 96 break;
108 } 97 }
109} 98}
@@ -143,12 +132,11 @@ SceneNode* gfx_load_scene(
143 // Load it, insert it into the cache, and return it. 132 // Load it, insert it into the cache, and return it.
144 SceneNode* node = gfx_scene_load(gfx, root_node, cmd); 133 SceneNode* node = gfx_scene_load(gfx, root_node, cmd);
145 if (node) { 134 if (node) {
146 insert_into_cache( 135 *(Asset*)mempool_alloc(&cache->assets) = (Asset){
147 cache, &(Asset){ 136 .type = SceneAsset,
148 .type = SceneAsset, 137 .hash = hash,
149 .hash = hash, 138 .data = node,
150 .data = node, 139 };
151 });
152 log_scene_loaded(cmd); 140 log_scene_loaded(cmd);
153 } 141 }
154 return node; 142 return node;
@@ -172,12 +160,11 @@ Texture* gfx_load_texture(Gfx* gfx, const LoadTextureCmd* cmd) {
172 RenderBackend* render_backend = gfx_get_render_backend(gfx); 160 RenderBackend* render_backend = gfx_get_render_backend(gfx);
173 Texture* texture = gfx_texture_load(render_backend, cmd); 161 Texture* texture = gfx_texture_load(render_backend, cmd);
174 if (texture) { 162 if (texture) {
175 insert_into_cache( 163 *(Asset*)mempool_alloc(&cache->assets) = (Asset){
176 cache, &(Asset){ 164 .type = TextureAsset,
177 .type = TextureAsset, 165 .hash = hash,
178 .hash = hash, 166 .data = texture,
179 .data = texture, 167 };
180 });
181 } 168 }
182 return texture; 169 return texture;
183} 170}