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.c42
1 files changed, 19 insertions, 23 deletions
diff --git a/gfx/src/asset/asset_cache.c b/gfx/src/asset/asset_cache.c
index 0075d41..037f62b 100644
--- a/gfx/src/asset/asset_cache.c
+++ b/gfx/src/asset/asset_cache.c
@@ -4,15 +4,12 @@
4#include "texture.h" 4#include "texture.h"
5#include <gfx/asset.h> 5#include <gfx/asset.h>
6#include <gfx/gfx.h> 6#include <gfx/gfx.h>
7#include <gfx/scene/node.h>
8#include <gfx_assert.h> 7#include <gfx_assert.h>
9 8
10#include "scene/node_impl.h"
11
12#include <cstring.h> 9#include <cstring.h>
13#include <log/log.h> 10#include <log/log.h>
14 11
15static Hash calc_scene_hash(const LoadSceneCmd* cmd) { 12static Hash calc_model_hash(const LoadModelCmd* cmd) {
16 assert(cmd); 13 assert(cmd);
17 switch (cmd->origin) { 14 switch (cmd->origin) {
18 case AssetFromFile: 15 case AssetFromFile:
@@ -20,7 +17,7 @@ static Hash calc_scene_hash(const LoadSceneCmd* cmd) {
20 case AssetFromMemory: 17 case AssetFromMemory:
21 return (Hash)cmd->data; 18 return (Hash)cmd->data;
22 } 19 }
23 FAIL("Unhandled scene asset origin"); 20 FAIL("Unhandled model asset origin");
24 return 0; 21 return 0;
25} 22}
26 23
@@ -74,7 +71,7 @@ static Asset* lookup_cache(AssetCache* cache, Hash hash) {
74 return 0; 71 return 0;
75} 72}
76 73
77static void log_scene_cache_hit(const LoadSceneCmd* cmd, Hash hash) { 74static void log_model_cache_hit(const LoadModelCmd* cmd, Hash hash) {
78 assert(cmd); 75 assert(cmd);
79 switch (cmd->origin) { 76 switch (cmd->origin) {
80 case AssetFromFile: 77 case AssetFromFile:
@@ -88,7 +85,7 @@ static void log_scene_cache_hit(const LoadSceneCmd* cmd, Hash hash) {
88 } 85 }
89} 86}
90 87
91static void log_scene_loaded(const LoadSceneCmd* cmd) { 88static void log_model_loaded(const LoadModelCmd* cmd) {
92 assert(cmd); 89 assert(cmd);
93 switch (cmd->origin) { 90 switch (cmd->origin) {
94 case AssetFromFile: 91 case AssetFromFile:
@@ -115,32 +112,31 @@ void gfx_destroy_asset_cache(AssetCache* cache) {
115 mempool_del(&cache->assets); 112 mempool_del(&cache->assets);
116} 113}
117 114
118SceneNode* gfx_load_scene( 115Model* gfx_load_model(Gfx* gfx, const LoadModelCmd* cmd) {
119 Gfx* gfx, SceneNode* parent_node, const LoadSceneCmd* cmd) {
120 assert(gfx); 116 assert(gfx);
121 117
122 AssetCache* cache = gfx_get_asset_cache(gfx); 118 AssetCache* cache = gfx_get_asset_cache(gfx);
123 119
124 // First search for the asset in the cache. 120 // First search for the asset in the cache.
125 const uint64_t hash = calc_scene_hash(cmd); 121 const uint64_t hash = calc_model_hash(cmd);
126 Asset* asset = lookup_cache(cache, hash); 122 Asset* asset = lookup_cache(cache, hash);
127 if (asset) { 123 if (asset) {
128 log_scene_cache_hit(cmd, hash); 124 log_model_cache_hit(cmd, hash);
129 return (SceneNode*)asset; 125 return asset->model;
130 } 126 }
131 127
132 // Asset not found in the cache. 128 // Asset not found in the cache.
133 // Load it, insert it into the cache, and return it. 129 // Load it, insert it into the cache, and return it.
134 SceneNode* node = gfx_scene_load(gfx, parent_node, cmd); 130 Model* model = gfx_model_load(gfx, cmd);
135 if (node) { 131 if (model) {
136 *(Asset*)mempool_alloc(&cache->assets) = (Asset){ 132 *(Asset*)mempool_alloc(&cache->assets) = (Asset){
137 .type = SceneAsset, 133 .type = ModelAsset,
138 .hash = hash, 134 .hash = hash,
139 .data = node, 135 .model = model,
140 }; 136 };
141 log_scene_loaded(cmd); 137 log_model_loaded(cmd);
142 } 138 }
143 return node; 139 return model;
144} 140}
145 141
146Texture* gfx_load_texture(Gfx* gfx, const LoadTextureCmd* cmd) { 142Texture* gfx_load_texture(Gfx* gfx, const LoadTextureCmd* cmd) {
@@ -153,7 +149,7 @@ Texture* gfx_load_texture(Gfx* gfx, const LoadTextureCmd* cmd) {
153 const uint64_t hash = calc_texture_hash(cmd); 149 const uint64_t hash = calc_texture_hash(cmd);
154 Asset* asset = lookup_cache(cache, hash); 150 Asset* asset = lookup_cache(cache, hash);
155 if (asset) { 151 if (asset) {
156 return (Texture*)asset; 152 return asset->texture;
157 } 153 }
158 154
159 // Asset not found in the cache. 155 // Asset not found in the cache.
@@ -162,9 +158,9 @@ Texture* gfx_load_texture(Gfx* gfx, const LoadTextureCmd* cmd) {
162 Texture* texture = gfx_texture_load(render_backend, cmd); 158 Texture* texture = gfx_texture_load(render_backend, cmd);
163 if (texture) { 159 if (texture) {
164 *(Asset*)mempool_alloc(&cache->assets) = (Asset){ 160 *(Asset*)mempool_alloc(&cache->assets) = (Asset){
165 .type = TextureAsset, 161 .type = TextureAsset,
166 .hash = hash, 162 .hash = hash,
167 .data = texture, 163 .texture = texture,
168 }; 164 };
169 } 165 }
170 return texture; 166 return texture;