Skip to content

Commit

Permalink
gh-82: add new asset loading api
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Sep 29, 2024
1 parent 096a3ab commit 53a2383
Show file tree
Hide file tree
Showing 92 changed files with 931 additions and 773 deletions.
4 changes: 2 additions & 2 deletions engine/config/engine.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ desc_pool_max_sb = 8192
desc_pool_max_sets = 2048

[render.aux]
font = "asset://fonts/consolas"
font = "assets/fonts/consolas"
screen_width = 1280
screen_height = 720

Expand All @@ -65,7 +65,7 @@ expand_size = 2
enable = true

[debug.console]
font = "asset://fonts/anonymous_pro"
font = "assets/fonts/anonymous_pro"
color_back = "000000ee"
color_line = "060606be"
color_text = "efefefff"
Expand Down
20 changes: 9 additions & 11 deletions engine/plugins/assimp/assimp_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@

namespace wmoge {

Status AssimpMeshAssetLoader::load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) {
WG_AUTO_PROFILE_ASSET("AssimpMeshAssetLoader::load");
Status AssimpMeshAssetLoader::load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Mesh>& asset) {
WG_AUTO_PROFILE_ASSET("AssimpMeshAssetLoader::load_typed");

Ref<AssimpMeshImportData> import_data = meta.import_data.cast<AssimpMeshImportData>();
Ref<AssimpMeshImportData> import_data = context.asset_meta.import_data.cast<AssimpMeshImportData>();
if (!import_data) {
WG_LOG_ERROR("no import data for " << name);
WG_LOG_ERROR("no import data for " << asset_id);
return StatusCode::InvalidData;
}
if (!import_data->has_soruce_files()) {
WG_LOG_ERROR("no source file " << name);
WG_LOG_ERROR("no source file " << asset_id);
return StatusCode::InvalidData;
}

Expand Down Expand Up @@ -83,19 +83,17 @@ namespace wmoge {
MeshFlags flags;
flags.set(MeshFlag::FromDisk);

Ref<Mesh> mesh = mesh_manager->create_mesh(flags);

asset = mesh;
asset->set_name(name);
asset = mesh_manager->create_mesh(flags);
asset->set_id(asset_id);

MeshBuilder& builder = importer.get_builder();
builder.set_mesh(mesh);
builder.set_mesh(asset);
if (!builder.build()) {
WG_LOG_ERROR("failed to build mesh " << file_name);
return StatusCode::Error;
}

mesh_manager->init_mesh(mesh.get());
mesh_manager->init_mesh(asset.get());

return WG_OK;
}
Expand Down
10 changes: 4 additions & 6 deletions engine/plugins/assimp/assimp_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@

#pragma once

#include "asset/asset.hpp"
#include "asset/asset_loader.hpp"
#include "asset/asset_meta.hpp"
#include "asset/asset_pak.hpp"
#include "asset/asset_loader_adapter.hpp"
#include "math/mat.hpp"
#include "math/vec.hpp"
#include "mesh/mesh.hpp"

#include <assimp/mesh.h>
#include <assimp/scene.h>
Expand All @@ -45,14 +43,14 @@ namespace wmoge {
* @class AssimpMeshAssetLoader
* @brief Loader for a mesh data based on assimp library
*/
class AssimpMeshAssetLoader final : public AssetLoader {
class AssimpMeshAssetLoader final : public AssetLoaderTyped<Mesh> {
public:
WG_RTTI_CLASS(AssimpMeshAssetLoader, AssetLoader);

AssimpMeshAssetLoader() = default;
~AssimpMeshAssetLoader() override = default;

Status load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Mesh>& asset) override;
};

WG_RTTI_CLASS_BEGIN(AssimpMeshAssetLoader) {
Expand Down
2 changes: 1 addition & 1 deletion engine/plugins/assimp/assimp_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace wmoge {
const GfxVertAttribs attribs = m_attribs;

Ref<ArrayMesh> array_mesh = make_ref<ArrayMesh>();
array_mesh->set_name(SID(get_file_name() + "." + name.str()));
array_mesh->set_id(SID(get_file_name() + "." + name.str()));
array_mesh->set_aabb(aabb);

for (unsigned int vert_id = 0; vert_id < num_vertices; vert_id++) {
Expand Down
24 changes: 9 additions & 15 deletions engine/plugins/freetype/freetype_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,30 @@

#include "freetype_asset_loader.hpp"

#include "freetype_font.hpp"
#include "freetype_import_data.hpp"
#include "grc/font.hpp"
#include "profiler/profiler.hpp"

namespace wmoge {

Status FreetypeAssetLoader::load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) {
WG_AUTO_PROFILE_ASSET("FreetypeAssetLoader::load");
Status FreetypeAssetLoader::load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Font>& asset) {
WG_AUTO_PROFILE_ASSET("FreetypeAssetLoader::load_typed");

Ref<FreetypeImportData> import_data = meta.import_data.cast<FreetypeImportData>();
Ref<FreetypeImportData> import_data = context.asset_meta.import_data.cast<FreetypeImportData>();
if (!import_data) {
WG_LOG_ERROR("no valid import data for " << name);
WG_LOG_ERROR("no valid import data for " << asset_id);
return StatusCode::InvalidData;
}
if (!import_data->has_soruce_files()) {
WG_LOG_ERROR("no source file " << name);
WG_LOG_ERROR("no source file " << asset_id);
return StatusCode::InvalidData;
}

Ref<Font> font = meta.rtti->instantiate().cast<Font>();
if (!font) {
WG_LOG_ERROR("failed to instantiate font " << name);
return StatusCode::FailedInstantiate;
}

asset = font;
asset->set_name(name);
asset = make_ref<Font>();
asset->set_id(asset_id);

FreetypeFont loader;
return loader.load(font, import_data->source_files[0].file, import_data->height, import_data->glyphs_in_row);
return loader.load(asset, import_data->source_files[0].file, import_data->height, import_data->glyphs_in_row);
}

}// namespace wmoge
8 changes: 4 additions & 4 deletions engine/plugins/freetype/freetype_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@

#pragma once

#include "asset/asset_loader.hpp"
#include "freetype_font.hpp"
#include "asset/asset_loader_adapter.hpp"
#include "grc/font.hpp"

namespace wmoge {

/**
* @class FreetypeAssetLoader
* @brief Loader for ttf fonts through freetype2 library
*/
class FreetypeAssetLoader final : public AssetLoader {
class FreetypeAssetLoader final : public AssetLoaderTyped<Font> {
public:
WG_RTTI_CLASS(FreetypeAssetLoader, AssetLoader);

FreetypeAssetLoader() = default;
~FreetypeAssetLoader() override = default;

Status load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Font>& asset) override;
};

WG_RTTI_CLASS_BEGIN(FreetypeAssetLoader) {
Expand Down
4 changes: 2 additions & 2 deletions engine/plugins/freetype/freetype_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace wmoge {
int bitmap_size = bitmap_width * bitmap_height;

Ref<Image> bitmap = make_ref<Image>();
bitmap->set_name(SID(font->get_name().str() + "_bitmap"));
bitmap->set_id(SID(font->get_name().str() + "_bitmap"));
bitmap->create(bitmap_width, bitmap_height, 1, 1);
auto* dst_ptr = bitmap->get_pixel_data()->buffer();
auto* src_ptr = glyphs_rendered.data();
Expand Down Expand Up @@ -173,7 +173,7 @@ namespace wmoge {
flags.set(TextureFlag::Compressed);

font_desc.texture = m_texture_manager->create_2d(flags, GfxFormat::R8, bitmap_width, bitmap_height, GfxTexSwizz::RRRRtoRGBA);
font_desc.texture->set_name(SID(font->get_name().str() + "_bitmap"));
font_desc.texture->set_id(SID(font->get_name().str() + "_bitmap"));
font_desc.texture->set_sampler(m_gfx_driver->make_sampler(sampler_desc, SID(sampler_desc.to_string())));
font_desc.texture->set_compression(compression_params);
font_desc.texture->set_source_images({bitmap});
Expand Down
35 changes: 22 additions & 13 deletions engine/plugins/runtime/asset/default_asset_loader.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "default_asset_loader.hpp"
/**********************************************************************************/
/* Wmoge game engine */
/* Available at github https://github.com/EgorOrachyov/wmoge */
Expand Down Expand Up @@ -27,43 +28,51 @@

#include "default_asset_loader.hpp"

#include "io/yaml.hpp"
#include "io/tree_yaml.hpp"
#include "profiler/profiler.hpp"
#include "rtti/type_storage.hpp"
#include "system/ioc_container.hpp"

namespace wmoge {

Status DefaultAssetLoader::load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) {
WG_AUTO_PROFILE_ASSET("DefaultAssetLoader::load");
Status DefaultAssetLoader::load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Asset>& asset) {
WG_AUTO_PROFILE_ASSET("DefaultAssetLoader::load_typed");

Ref<AssetImportData> import_data = meta.import_data.cast<AssetImportData>();
Ref<AssetImportData> import_data = context.asset_meta.import_data.cast<AssetImportData>();
if (!import_data) {
WG_LOG_ERROR("no import data to load " << name);
WG_LOG_ERROR("no import data to load " << asset_id);
return StatusCode::InvalidData;
}

if (!import_data->has_soruce_files()) {
WG_LOG_ERROR("no source file " << name);
WG_LOG_ERROR("no source file " << asset_id);
return StatusCode::InvalidData;
}

std::string path_on_disk = import_data->source_files[0].file;
if (path_on_disk.empty()) {
WG_LOG_ERROR("no path on disk to load asset file " << name);
WG_LOG_ERROR("no path on disk to load asset file " << asset_id);
return StatusCode::InvalidData;
}

asset = meta.rtti->instantiate().cast<Asset>();
RttiClass* rtti = IocContainer::iresolve_v<RttiTypeStorage>()->find_class(context.asset_meta.rtti);
if (!rtti) {
WG_LOG_ERROR("no rtti type for " << asset_id);
return StatusCode::InvalidData;
}

asset = rtti->instantiate().cast<Asset>();
if (!asset) {
WG_LOG_ERROR("failed to instantiate asset " << name);
WG_LOG_ERROR("failed to instantiate asset " << asset_id);
return StatusCode::FailedInstantiate;
}

IoContext context;
asset->set_id(asset_id);

IoYamlTree asset_tree;
WG_CHECKED(asset_tree.parse_file(path_on_disk));

asset->set_name(name);

if (!asset->read_from_tree(context, asset_tree)) {
if (!asset->read_from_tree(context.io_context, asset_tree)) {
WG_LOG_ERROR("failed to load asset from file " << path_on_disk);
return StatusCode::FailedRead;
}
Expand Down
9 changes: 3 additions & 6 deletions engine/plugins/runtime/asset/default_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,22 @@

#pragma once

#include "asset/asset.hpp"
#include "asset/asset_loader.hpp"
#include "asset/asset_meta.hpp"
#include "asset/asset_pak.hpp"
#include "asset/asset_loader_adapter.hpp"

namespace wmoge {

/**
* @class DefaultAssetLoader
* @brief Loader for default single-file assets stored in yaml format
*/
class DefaultAssetLoader final : public AssetLoader {
class DefaultAssetLoader final : public AssetLoaderTyped<Asset> {
public:
WG_RTTI_CLASS(DefaultAssetLoader, AssetLoader);

DefaultAssetLoader() = default;
~DefaultAssetLoader() override = default;

Status load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Asset>& asset) override;
};

WG_RTTI_CLASS_BEGIN(DefaultAssetLoader) {
Expand Down
20 changes: 9 additions & 11 deletions engine/plugins/runtime/asset/image_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,29 @@

#include "image_asset_loader.hpp"

#include "grc/image.hpp"
#include "image_import_data.hpp"
#include "profiler/profiler.hpp"

namespace wmoge {

Status ImageAssetLoader::load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) {
WG_AUTO_PROFILE_ASSET("ImageAssetLoader::load");
Status ImageAssetLoader::load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Image>& asset) {
WG_AUTO_PROFILE_ASSET("ImageAssetLoader::load_typed");

Ref<ImageImportData> import_data = meta.import_data.cast<ImageImportData>();
Ref<ImageImportData> import_data = context.asset_meta.import_data.cast<ImageImportData>();
if (!import_data) {
WG_LOG_ERROR("no import data to load image " << name);
WG_LOG_ERROR("no import data to load image " << asset_id);
return StatusCode::InvalidData;
}

if (!import_data->has_soruce_files()) {
WG_LOG_ERROR("no source file " << name);
WG_LOG_ERROR("no source file " << asset_id);
return StatusCode::InvalidData;
}

Ref<Image> image = make_ref<Image>();

asset = image;
asset->set_name(name);
asset = make_ref<Image>();
asset->set_id(asset_id);

return image->load(import_data->source_files[0].file, import_data->channels);
return asset->load(import_data->source_files[0].file, import_data->channels);
}

}// namespace wmoge
7 changes: 4 additions & 3 deletions engine/plugins/runtime/asset/image_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@

#pragma once

#include "asset/asset_loader.hpp"
#include "asset/asset_loader_adapter.hpp"
#include "grc/image.hpp"

namespace wmoge {

/**
* @class ImageAssetLoader
* @brief Loader for images through stb image library
*/
class ImageAssetLoader final : public AssetLoader {
class ImageAssetLoader final : public AssetLoaderTyped<Image> {
public:
WG_RTTI_CLASS(ImageAssetLoader, AssetLoader);

ImageAssetLoader() = default;
~ImageAssetLoader() override = default;

Status load(const Strid& name, const AssetMeta& meta, Ref<Asset>& asset) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Image>& asset) override;
};

WG_RTTI_CLASS_BEGIN(ImageAssetLoader) {
Expand Down
Loading

0 comments on commit 53a2383

Please sign in to comment.