Skip to content

Commit

Permalink
gh-82: use new data file request api in asset loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Oct 7, 2024
1 parent b0d04a3 commit d8002cb
Show file tree
Hide file tree
Showing 36 changed files with 327 additions and 199 deletions.
26 changes: 12 additions & 14 deletions engine/plugins/assimp/assimp_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@

namespace wmoge {

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

Status AssimpMeshAssetLoader::fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) {
Ref<AssimpMeshImportData> import_data = context.asset_meta.import_data.cast<AssimpMeshImportData>();
if (!import_data) {
WG_LOG_ERROR("no import data for " << asset_id);
Expand All @@ -56,25 +54,25 @@ namespace wmoge {
WG_LOG_ERROR("no source file " << asset_id);
return StatusCode::InvalidData;
}
request.add_data_file(FILE_TAG, import_data->source_files[0].file);
return WG_OK;
}

FileSystem* file_system = IocContainer::iresolve_v<FileSystem>();
std::string file_name = import_data->source_files[0].file;
Status AssimpMeshAssetLoader::load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Mesh>& asset) {
WG_AUTO_PROFILE_ASSET("AssimpMeshAssetLoader::load_typed");

std::vector<std::uint8_t> file_data;
if (!file_system->read_file(file_name, file_data)) {
WG_LOG_ERROR("failed to load file " << file_name);
return StatusCode::FailedRead;
}
Ref<AssimpMeshImportData> import_data = context.asset_meta.import_data.cast<AssimpMeshImportData>();
assert(import_data);

AssimpMeshImporter importer;
if (!importer.read(file_name, file_data, import_data->process)) {
WG_LOG_ERROR("failed to import file " << file_name);
if (!importer.read(asset_id.str(), result.get_data_file(FILE_TAG), import_data->process)) {
WG_LOG_ERROR("failed to import mesh " << asset_id);
return StatusCode::Error;
}

importer.set_attribs(import_data->attributes);
if (!importer.process()) {
WG_LOG_ERROR("failed to process file " << file_name);
WG_LOG_ERROR("failed to process mesh " << asset_id);
return StatusCode::Error;
}

Expand All @@ -89,7 +87,7 @@ namespace wmoge {
MeshBuilder& builder = importer.get_builder();
builder.set_mesh(asset);
if (!builder.build()) {
WG_LOG_ERROR("failed to build mesh " << file_name);
WG_LOG_ERROR("failed to build mesh " << asset_id);
return StatusCode::Error;
}

Expand Down
1 change: 1 addition & 0 deletions engine/plugins/assimp/assimp_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace wmoge {
AssimpMeshAssetLoader() = default;
~AssimpMeshAssetLoader() override = default;

Status fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Mesh>& asset) override;
};

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 @@ -38,7 +38,7 @@

namespace wmoge {

Status AssimpImporter::read(std::string file_name, array_view<std::uint8_t> data, const AssimpProcess& flags) {
Status AssimpImporter::read(std::string file_name, array_view<const std::uint8_t> data, const AssimpProcess& flags) {
WG_AUTO_PROFILE_ASSET("AssimpImporter::read");

if (flags.triangulate) m_options |= aiProcess_Triangulate;
Expand Down
2 changes: 1 addition & 1 deletion engine/plugins/assimp/assimp_importer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace wmoge {
public:
AssimpImporter() = default;

Status read(std::string file_name, array_view<std::uint8_t> data, const AssimpProcess& flags);
Status read(std::string file_name, array_view<const std::uint8_t> data, const AssimpProcess& flags);
Status process();

[[nodiscard]] const AssimpProcess& get_flags() const { return m_flags; }
Expand Down
17 changes: 12 additions & 5 deletions engine/plugins/freetype/freetype_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,31 @@

namespace wmoge {

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

Status FreetypeAssetLoader::fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) {
Ref<FreetypeImportData> import_data = context.asset_meta.import_data.cast<FreetypeImportData>();
if (!import_data) {
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 " << asset_id);
WG_LOG_ERROR("no source files for " << asset_id);
return StatusCode::InvalidData;
}
request.add_data_file(FILE_TAG, import_data->source_files[0].file);
return WG_OK;
}

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 = context.asset_meta.import_data.cast<FreetypeImportData>();
assert(import_data);

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

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

}// namespace wmoge
1 change: 1 addition & 0 deletions engine/plugins/freetype/freetype_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace wmoge {
FreetypeAssetLoader() = default;
~FreetypeAssetLoader() override = default;

Status fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Font>& asset) override;
};

Expand Down
10 changes: 2 additions & 8 deletions engine/plugins/freetype/freetype_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ namespace wmoge {
m_texture_manager = IocContainer::iresolve_v<TextureManager>();
}

Status FreetypeFont::load(const Ref<Font>& font, const std::string& path, int height, int glyphs_in_row) {
Status FreetypeFont::load(const Ref<Font>& font, array_view<const std::uint8_t> ttf_data, int height, int glyphs_in_row) {
WG_AUTO_PROFILE_ASSET("FreetypeFont::load");

static const int GLYPHS_SIZE_SHIFT = 6;
static const int GLYPHS_BITMAP_OFFSET = 2;

std::vector<std::uint8_t> ttf_data;
if (!m_file_system->read_file(path, ttf_data)) {
WG_LOG_ERROR("failed to load font data from asset pak " << path);
return StatusCode::FailedRead;
}

FT_Library ft_library;
if (FT_Init_FreeType(&ft_library)) {
WG_LOG_ERROR("failed to init free type library");
Expand All @@ -64,7 +58,7 @@ namespace wmoge {

FT_Face ft_face;
if (FT_New_Memory_Face(ft_library, ttf_data.data(), static_cast<FT_Long>(ttf_data.size()), 0, &ft_face)) {
WG_LOG_ERROR("failed to parse font data for " << path);
WG_LOG_ERROR("failed to parse font data for " << font->get_name());
FT_Done_FreeType(ft_library);
return StatusCode::FailedParse;
}
Expand Down
5 changes: 3 additions & 2 deletions engine/plugins/freetype/freetype_font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#pragma once

#include "core/array_view.hpp"
#include "core/status.hpp"
#include "core/string_utils.hpp"
#include "gfx/gfx_driver.hpp"
Expand All @@ -50,13 +51,13 @@ namespace wmoge {
* @note Uses FreeType2 library for .ttf file loading
*
* @param font Font to load into
* @param filepath Path to the font .ttf file in a file system
* @param ttf_data Content of the font .ttf file in a file system
* @param height Font height in pixels
* @param glyphs_in_row Num of glyphs in a row of a bitmap
*
* @return True if font loaded
*/
Status load(const Ref<Font>& font, const std::string& path, int height = 40, int glyphs_in_row = 16);
Status load(const Ref<Font>& font, array_view<const std::uint8_t> ttf_data, int height = 40, int glyphs_in_row = 16);

private:
GfxDriver* m_gfx_driver;
Expand Down
22 changes: 11 additions & 11 deletions engine/plugins/runtime/asset/default_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@

namespace wmoge {

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

Status DefaultAssetLoader::fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) {
Ref<AssetImportData> import_data = context.asset_meta.import_data.cast<AssetImportData>();
if (!import_data) {
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 " << asset_id);
return StatusCode::InvalidData;
}
request.add_data_file(FILE_TAG, import_data->source_files[0].file);
return WG_OK;
}

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 " << asset_id);
return StatusCode::InvalidData;
}
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 = context.asset_meta.import_data.cast<AssetImportData>();
assert(import_data);

RttiClass* rtti = IocContainer::iresolve_v<RttiTypeStorage>()->find_class(context.asset_meta.rtti);
if (!rtti) {
Expand All @@ -70,10 +70,10 @@ namespace wmoge {
asset->set_id(asset_id);

IoYamlTree asset_tree;
WG_CHECKED(asset_tree.parse_file(path_on_disk));
WG_CHECKED(asset_tree.parse_data(result.get_data_file(FILE_TAG)));

if (!asset->read_from_tree(context.io_context, asset_tree)) {
WG_LOG_ERROR("failed to load asset from file " << path_on_disk);
WG_LOG_ERROR("failed to load asset from file " << asset_id);
return StatusCode::FailedRead;
}

Expand Down
1 change: 1 addition & 0 deletions engine/plugins/runtime/asset/default_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace wmoge {
DefaultAssetLoader() = default;
~DefaultAssetLoader() override = default;

Status fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Asset>& asset) override;
};

Expand Down
16 changes: 11 additions & 5 deletions engine/plugins/runtime/asset/image_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,30 @@

namespace wmoge {

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

Status ImageAssetLoader::fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) {
Ref<ImageImportData> import_data = context.asset_meta.import_data.cast<ImageImportData>();
if (!import_data) {
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 " << asset_id);
return StatusCode::InvalidData;
}
request.add_data_file(FILE_TAG, import_data->source_files[0].file);
return WG_OK;
}

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 = context.asset_meta.import_data.cast<ImageImportData>();
assert(import_data);

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

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

}// namespace wmoge
1 change: 1 addition & 0 deletions engine/plugins/runtime/asset/image_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace wmoge {
ImageAssetLoader() = default;
~ImageAssetLoader() override = default;

Status fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Image>& asset) override;
};

Expand Down
19 changes: 10 additions & 9 deletions engine/plugins/runtime/asset/shader_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@

namespace wmoge {

Status ShaderAssetLoader::load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Shader>& asset) {
WG_AUTO_PROFILE_ASSET("ShaderAssetLoader::load_typed");

Status ShaderAssetLoader::fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) {
Ref<AssetImportData> import_data = context.asset_meta.import_data.cast<AssetImportData>();
if (!import_data) {
WG_LOG_ERROR("no import data to load " << asset_id);
Expand All @@ -48,16 +46,19 @@ namespace wmoge {
WG_LOG_ERROR("no source file " << asset_id);
return StatusCode::InvalidData;
}
request.add_data_file(FILE_TAG, import_data->source_files[0].file);
return WG_OK;
}

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 " << asset_id);
return StatusCode::InvalidData;
}
Status ShaderAssetLoader::load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Shader>& asset) {
WG_AUTO_PROFILE_ASSET("ShaderAssetLoader::load_typed");

Ref<AssetImportData> import_data = context.asset_meta.import_data.cast<AssetImportData>();
assert(import_data);

ShaderFile shader_file;
IoYamlTree tree;
WG_CHECKED(tree.parse_file(path_on_disk));
WG_CHECKED(tree.parse_data(result.get_data_file(FILE_TAG)));
WG_TREE_READ(context.io_context, tree, shader_file);

auto* shader_manager = IocContainer::iresolve_v<ShaderManager>();
Expand Down
1 change: 1 addition & 0 deletions engine/plugins/runtime/asset/shader_asset_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace wmoge {
ShaderAssetLoader() = default;
~ShaderAssetLoader() override = default;

Status fill_request(AssetLoadContext& context, const AssetId& asset_id, AssetLoadRequest& request) override;
Status load_typed(AssetLoadContext& context, const AssetId& asset_id, const AssetLoadResult& result, Ref<Shader>& asset) override;
};

Expand Down
Loading

0 comments on commit d8002cb

Please sign in to comment.