- resources dependencies

This commit is contained in:
tluqo 2014-03-12 23:03:07 +01:00
parent 3ca476e6f1
commit 60accc5710
7 changed files with 59 additions and 8 deletions

View file

@ -3,7 +3,7 @@
#include "core/math_utils.h"
#include "core/spin_mutex.h"
#include <new>
//#include <new>
#include <stdio.h>
#include <vadefs.h>
#include <Windows.h>

View file

@ -8,7 +8,8 @@
namespace Lux
{
Resource::Resource(const Path& path, ResourceManager& resource_manager)
: m_ref_count(0)
: m_ref_count()
, m_dep_count(1)
, m_state(State::EMPTY)
, m_path(path)
, m_cb()
@ -39,6 +40,7 @@ namespace Lux
void Resource::onUnloading(void)
{
m_state = State::UNLOADING;
++m_dep_count;
m_cb.invoke(State::UNLOADING);
}
@ -59,4 +61,41 @@ namespace Lux
FS::FileSystem& fs = m_resource_manager.getFileSystem();
fs.openAsync(fs.getDefaultDevice(), m_path, FS::Mode::OPEN | FS::Mode::READ, getReadCallback());
}
void Resource::addResDependency(Resource& dependent_resource)
{
dependent_resource.m_cb.bind<Resource, &Resource::onResourceUpdated>(this);
m_dep_count += (dependent_resource.isReady() ? 0 : 1);
}
void Resource::remResDependency(Resource& dependent_resource)
{
dependent_resource.m_cb.unbind<Resource, &Resource::onResourceUpdated>(this);
if(dependent_resource.isReady())
{
decrementDepCount();
}
}
void Resource::onResourceUpdated(uint32_t new_state)
{
if(State::READY == new_state)
{
decrementDepCount();
}
else
{
m_dep_count++;
if(isReady())
{
onUnloading();
}
}
}
void Resource::decrementDepCount()
{
if(--m_dep_count == 0)
onReady();
}
} // ~namespace Lux

View file

@ -65,8 +65,17 @@ namespace Lux
uint32_t addRef(void) { return ++m_ref_count; }
uint32_t remRef(void) { return --m_ref_count; }
// this method should be called only from method, which parses the file. Otherwise this method
// should be reimplemented.
void addResDependency(Resource& dependent_resource);
void remResDependency(Resource& dependent_resource);
void onResourceUpdated(uint32_t new_state);
void decrementDepCount();
private:
uint16_t m_ref_count;
uint16_t m_dep_count;
State m_state;
protected:

View file

@ -54,13 +54,16 @@ void Material::loaded(FS::IFile* file, bool success, FS::FileSystem& fs)
JsonSerializer serializer(*file, JsonSerializer::READ);
char path[MAX_PATH];
serializer.deserialize("texture", path, MAX_PATH);
m_textures.push(static_cast<Texture*>(m_resource_manager.get(ResourceManager::TEXTURE)->load(path)));
Texture* texture = static_cast<Texture*>(m_resource_manager.get(ResourceManager::TEXTURE)->load(path));
m_textures.push(texture);
serializer.deserialize("shader", path, MAX_PATH);
m_shader = static_cast<Shader*>(m_resource_manager.get(ResourceManager::SHADER)->load(path));
addResDependency(*texture);
addResDependency(*m_shader);
m_size = file->size();
onReady();
decrementDepCount();
}
fs.close(file);

View file

@ -196,15 +196,15 @@ void Model::loaded(FS::IFile* file, bool success, FS::FileSystem& fs)
char mesh_name[MAX_PATH];
mesh_name[str_size] = 0;
file->read(mesh_name, str_size);
Material* material = static_cast<Material*>(m_resource_manager.get(ResourceManager::MATERIAL)->load(material_path));
Mesh mesh(material, mesh_vertex_offset, mesh_tri_count * 3, mesh_name);
mesh_vertex_offset += mesh_tri_count * 3;
m_meshes.push(mesh);
addResDependency(*material);
}
m_size = file->size();
onReady();
decrementDepCount();
}
fs.close(file);

View file

@ -95,7 +95,7 @@ void Shader::loaded(FS::IFile* file, bool success, FS::FileSystem& fs)
LUX_DELETE_ARRAY(buf);
m_size = file->size();
onReady();
decrementDepCount();
}
fs.close(file);

View file

@ -129,7 +129,7 @@ void Texture::loaded(FS::IFile* file, bool success, FS::FileSystem& fs)
LUX_DELETE_ARRAY(buffer);
m_size = file->size();
onReady();
decrementDepCount();
}
else
{