Skip to content

Commit

Permalink
Reduced memory usage for loading assets
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklbell committed Sep 25, 2022
1 parent 4456f22 commit bd4241b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
11 changes: 6 additions & 5 deletions include/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ struct Mesh {

// Serialised Section
unsigned int num_indices = 0;
unsigned int *indices = nullptr;
unsigned int* indices = nullptr;

// @note Mesh is not responsible for material's pointers
uint64_t num_materials = 0;
Material *materials = nullptr;
uint64_t *material_indices = nullptr;
Material* materials = nullptr;
uint64_t* material_indices = nullptr;

uint64_t num_vertices = 0;
glm::fvec3* vertices = nullptr;
Expand All @@ -76,8 +76,8 @@ struct Mesh {
glm::mat4x4* transforms = nullptr;
GLenum draw_mode = GL_TRIANGLES;
GLenum draw_type = GL_UNSIGNED_INT;
GLint *draw_start = nullptr;
GLint *draw_count = nullptr;
GLint* draw_start = nullptr;
GLint* draw_count = nullptr;

GLuint indices_vbo = GL_FALSE;
GLuint vertices_vbo = GL_FALSE;
Expand Down Expand Up @@ -275,6 +275,7 @@ struct AssetManager {
Texture* getTexture(const std::string &path);
Texture* getColorTexture(const glm::vec3 &col, GLint internal_format=GL_RGB);

void clearExcluding(const std::set<std::string> &excluded);
void clear();
};

Expand Down
35 changes: 35 additions & 0 deletions src/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Mesh::~Mesh(){

free(materials);
free(material_indices);

free(indices);
free(vertices);
free(normals);
Expand All @@ -119,6 +120,7 @@ Mesh::~Mesh(){
free(bone_ids);
free(weights);

free(transforms);
free(draw_start);
free(draw_count);
}
Expand Down Expand Up @@ -1407,6 +1409,39 @@ Texture* AssetManager::getColorTexture(const glm::vec3 &col, GLint internal_form
return nullptr;
}

// Pretty crappy function that doesn't actaully clear unused, to improve
// may have to make assets only accessible by handle, check performance
void AssetManager::clearExcluding(const std::set<std::string>& excluded) {
for (auto it = handle_mesh_map.cbegin(); it != handle_mesh_map.cend(); ) {
if (excluded.find(it->first) == excluded.end()) {
handle_mesh_map.erase(it++); // or "it = m.erase(it)" since C++11
}
else {
++it;
}
}
for (auto it = handle_animated_mesh_map.cbegin(); it != handle_animated_mesh_map.cend(); ) {
if (excluded.find(it->first) == excluded.end()) {
handle_animated_mesh_map.erase(it++); // or "it = m.erase(it)" since C++11
}
else {
++it;
}
}

// Can't clear texture or color since mesh relies on it and excluded doesn't
// properly store

for (auto it = handle_audio_map.cbegin(); it != handle_audio_map.cend(); ) {
if (excluded.find(it->first) == excluded.end()) {
handle_audio_map.erase(it++); // or "it = m.erase(it)" since C++11
}
else {
++it;
}
}
}

void AssetManager::clear() {
handle_mesh_map.clear();
handle_texture_map.clear();
Expand Down
40 changes: 26 additions & 14 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ bool loadLevel(EntityManager &entity_manager, AssetManager &asset_manager, const

uint64_t asset_index = 0;
std::unordered_map<uint64_t, void *> index_to_asset;
std::set<std::string> asset_handles;

AssetType asset_type;
while(1) {
Expand All @@ -364,39 +365,49 @@ bool loadLevel(EntityManager &entity_manager, AssetManager &asset_manager, const
std::string handle;
handle.resize(handle_len);
fread(handle.data(), handle_len, 1, f);
asset_handles.emplace(handle);

// @perf move out of loop
switch (asset_type) {
case MESH_ASSET:
{
// In future this should probably be true for all meshes by saving to mesh
auto mesh = asset_manager.createMesh(handle);
if (endsWith(handle, ".mesh")) {
asset_manager.loadMeshFile(mesh, handle);
}
else {
std::cerr << "Warning, there is a model file being loaded with assimp\n";
asset_manager.loadMeshAssimp(mesh, handle);
auto mesh = asset_manager.getMesh(handle);
if (mesh == nullptr) {
// In future this should probably be true for all meshes by saving to mesh
mesh = asset_manager.createMesh(handle);
if (endsWith(handle, ".mesh")) {
asset_manager.loadMeshFile(mesh, handle);
}
else {
std::cerr << "Warning, there is a model file being loaded with assimp\n";
asset_manager.loadMeshAssimp(mesh, handle);
}
}

index_to_asset[asset_index] = (void*)mesh;
break;
}
case ANIMATED_MESH_ASSET:
{
auto animesh = asset_manager.createAnimatedMesh(handle);
auto animesh = asset_manager.getAnimatedMesh(handle);
if (animesh == nullptr) {
auto animesh = asset_manager.createAnimatedMesh(handle);

asset_manager.loadAnimationFile(animesh, handle);
asset_manager.loadAnimationFile(animesh, handle);
}

index_to_asset[asset_index] = (void*)animesh;
break;
}
case TEXTURE_ASSET:
{
// In future this should probably be true for all meshes by saving to mesh
auto texture = asset_manager.createTexture(handle);
// @todo seperate mesh and animation and load with our loader
asset_manager.loadTexture(texture, handle);
auto texture = asset_manager.getTexture(handle);
if (texture == nullptr) {
// In future this should probably be true for all meshes by saving to mesh
auto texture = asset_manager.createTexture(handle);
// @todo seperate mesh and animation and load with our loader
asset_manager.loadTexture(texture, handle);
}

index_to_asset[asset_index] = (void*)texture;
break;
Expand All @@ -411,6 +422,7 @@ bool loadLevel(EntityManager &entity_manager, AssetManager &asset_manager, const
asset_index++;
}
}
//asset_manager.clearExcluding(asset_handles);

//
// Read entities until end of file
Expand Down

0 comments on commit bd4241b

Please sign in to comment.