Skip to content

Commit

Permalink
assets compiler and input splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Feb 18, 2025
1 parent 58e5eea commit a25e355
Show file tree
Hide file tree
Showing 46 changed files with 598 additions and 326 deletions.
Binary file added .255.cache.bin
Binary file not shown.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_subdirectory(modules/physics)

add_subdirectory(apps/editor)
add_subdirectory(apps/example_game)
add_subdirectory(apps/assets_compiler)

era_add_deploy_target(editor)
era_add_deploy_target(example_game)
Expand Down
12 changes: 12 additions & 0 deletions apps/assets_compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include(${CMAKE_SOURCE_DIR}/cmake/common.cmake)

era_begin(assets_compiler "APP")
require_thirdparty_module(assets_compiler EnTT)
require_thirdparty_module(assets_compiler yaml-cpp)
require_thirdparty_module(assets_compiler rttr_core)
require_thirdparty_module(assets_compiler DirectXTex)
require_module(assets_compiler base)
require_module(assets_compiler core)

target_include_directories(assets_compiler PUBLIC modules/thirdparty-imgui/imgui)
era_end(assets_compiler)
90 changes: 90 additions & 0 deletions apps/assets_compiler/src/compiler/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2023-present Eldar Muradov. All rights reserved.
#include <fstream>
#include <iostream>

#include <clara/clapa.hpp>

#include <asset/bin.h>
#include <asset/model_asset.h>

#include <core/log.h>
#include <core/string.h>

int main(int argc, char** argv)
{
using namespace era_engine;
using namespace clara;

try
{
fs::path path;
bool verbose = false;

Parser cli;
cli += Opt(verbose, "verbose")["-v"]["--verbose"]("Enable verbose logging");
cli += Opt(path, "path")["-p"]["--path"]("Path to asset");

auto result = cli.parse(Args(argc, argv));
if (!result)
{
std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
}

path = get_full_path(path);

if (!fs::exists(path))
{
std::cerr << "Could not find file '" << path << "'.\n";
return EXIT_FAILURE;
}

std::string extension = path.extension().string();

fs::path cached_filename = path;
cached_filename.replace_extension("." + std::to_string(mesh_flag_default) + ".cache.bin");
fs::path cache_filepath = L"asset_cache" / cached_filename;

if (fs::exists(cache_filepath))
{
auto last_cache_write_time = fs::last_write_time(cache_filepath);
auto last_original_write_time = fs::last_write_time(path);

if (last_cache_write_time > last_original_write_time)
{
std::cout << "Asset is already compiled! \n";
return EXIT_SUCCESS;
}
}

std::cout << "Preprocessing asset '" << path << "' for faster loading next time.\n";

ModelAsset result_mesh;

std::transform(extension.begin(), extension.end(), extension.begin(),
[](char c) { return std::tolower(c); });
if (extension == ".fbx")
{
result_mesh = loadFBX(path, mesh_flag_default);
}
else if (extension == ".obj")
{
result_mesh = loadOBJ(path, mesh_flag_default);
}

fs::create_directories(cache_filepath.parent_path());
writeBIN(result_mesh, cache_filepath);
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << "\n";

std::ofstream output("logs/compiler_error_log.txt");
output << "Runtime Error>" << ex.what() << std::endl;
output.close();

std::this_thread::sleep_for(std::chrono::duration<float>(5000.0f));
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
4 changes: 4 additions & 0 deletions apps/editor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
include(${CMAKE_SOURCE_DIR}/cmake/common.cmake)

era_begin(editor "APP")
require_thirdparty_module(editor EnTT)
require_thirdparty_module(editor yaml-cpp)
require_thirdparty_module(editor rttr_core)
require_thirdparty_module(editor DirectXTex)
require_module(editor base)
require_module(editor core)
require_module(editor physics)
Expand Down
68 changes: 68 additions & 0 deletions apps/editor/src/editor/editor_init_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "editor/editor_init_system.h"

#include <core/ecs/input_reciever_component.h>
#include <core/ecs/input_sender_component.h>
#include <core/string.h>

#include <rendering/mesh_shader.h>
#include <rendering/ecs/renderer_holder_root_component.h>

#include <ecs/update_groups.h>
#include <ecs/base_components/transform_component.h>
#include <ecs/rendering/world_renderer.h>
#include <ecs/rendering/scene_rendering.h>
#include <core/ecs/camera_holder_component.h>
#include <ecs/rendering/mesh_component.h>

#include <audio/audio.h>

#include <terrain/terrain.h>

#include <animation/skinning.h>

#include <rttr/policy.h>
#include <rttr/registration>

namespace era_engine
{
RTTR_REGISTRATION
{
using namespace rttr;

registration::class_<EditorInitSystem>("GameInitSystem")
.constructor<World*>()(policy::ctor::as_raw_ptr)
.method("update", &EditorInitSystem::update)(metadata("update_group", update_types::BEGIN));
}

EditorInitSystem::EditorInitSystem(World* _world)
: System(_world)
{
}

EditorInitSystem::~EditorInitSystem()
{
}

void EditorInitSystem::init()
{
RendererHolderRootComponent* renderer_holder_rc = world->add_root_component<RendererHolderRootComponent>();
ASSERT(renderer_holder_rc != nullptr);

Entity camera_entity = world->create_entity("CameraEntity");
camera_entity.add_component<CameraHolderComponent>()
.add_component<InputRecieverComponent>()
.add_component<InputSenderComponent>();

CameraHolderComponent& camera_holder_component = camera_entity.get_component<CameraHolderComponent>();
camera_holder_component.set_camera_type(CameraHolderComponent::FREE_CAMERA);
camera_holder_component.set_render_camera(&renderer_holder_rc->camera);

camera_entity.get_component<InputSenderComponent>().add_reciever(camera_entity.get_component_if_exists<InputRecieverComponent>());
}

void EditorInitSystem::update(float dt)
{

}

}
18 changes: 18 additions & 0 deletions apps/editor/src/editor/editor_init_system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <ecs/system.h>

namespace era_engine
{

class EditorInitSystem final : public System
{
public:
EditorInitSystem(World* _world);
~EditorInitSystem();

void init() override;
void update(float dt) override;
ERA_VIRTUAL_REFLECT(System)
};
}
31 changes: 19 additions & 12 deletions apps/example_game/src/game/game_init_system.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#include "game/game_init_system.h"

#include <core/ecs/input_reciever_component.h>
#include <core/ecs/input_sender_component.h>
#include <core/string.h>

#include <rendering/mesh_shader.h>
#include <rendering/ecs/renderer_holder_root_component.h>

#include <ecs/update_groups.h>
#include <ecs/base_components/transform_component.h>
#include <ecs/rendering/world_renderer.h>
#include <ecs/rendering/scene_rendering.h>
#include <core/ecs/camera_holder_component.h>
#include <ecs/rendering/mesh_component.h>

#include <game/movement/movement_component.h>

#include <audio/audio.h>

#include <terrain/terrain.h>

#include <animation/skinning.h>

#include <rttr/policy.h>
Expand Down Expand Up @@ -49,14 +50,19 @@ namespace era_engine
RendererHolderRootComponent* renderer_holder_rc = world->add_root_component<RendererHolderRootComponent>();
ASSERT(renderer_holder_rc != nullptr);

Entity camera_entity = world->create_entity("Entity1");
camera_entity.add_component<CameraHolderComponent>().add_component<InputRecieverComponent>().add_component<MovementComponent>();
camera_entity.get_component<CameraHolderComponent>().set_camera_type(CameraHolderComponent::ATTACHED_TO_TRS);
camera_entity.get_component<CameraHolderComponent>().set_render_camera(&renderer_holder_rc->camera);
Entity camera_entity = world->create_entity("CameraEntity");
camera_entity.add_component<CameraHolderComponent>()
.add_component<InputSenderComponent>()
.add_component<InputRecieverComponent>()
.add_component<MovementComponent>();

CameraHolderComponent& camera_holder_component = camera_entity.get_component<CameraHolderComponent>();
camera_holder_component.set_camera_type(CameraHolderComponent::ATTACHED_TO_TRS);
camera_holder_component.set_render_camera(&renderer_holder_rc->camera);

Entity entity2 = world->create_entity("Entity2");
camera_entity.get_component<InputSenderComponent>().add_reciever(camera_entity.get_component_if_exists<InputRecieverComponent>());

#if 0
#if 1
auto defaultmat = createPBRMaterialAsync({ "", "" });
defaultmat->shader = pbr_material_shader_double_sided;

Expand Down Expand Up @@ -88,6 +94,7 @@ namespace era_engine
{
auto& en = world->create_entity("Veribot")
.add_component<animation::AnimationComponent>()
.add_component<animation::SkeletonComponent>()
.add_component<MeshComponent>(mesh);

TransformComponent& transform_component = en.get_component<TransformComponent>();
Expand Down
2 changes: 1 addition & 1 deletion apps/example_game/src/game/game_init_system.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "ecs/system.h"
#include <ecs/system.h>

namespace era_engine
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <game/movement/movement_component.h>

#include <core/ecs/input_reciever_component.h>
#include <core/ecs/input_root_component.h>
#include <core/ecs/input_sender_component.h>
#include <core/ecs/camera_holder_component.h>

#include <ecs/update_groups.h>
Expand All @@ -26,8 +26,6 @@ namespace era_engine
MovementSystem::MovementSystem(World* _world)
: System(_world)
{
input_rc = world->add_root_component<InputRootComponent>();
ASSERT(input_rc != nullptr);
}

MovementSystem::~MovementSystem()
Expand Down
6 changes: 0 additions & 6 deletions apps/example_game/src/game/movement/private/movement_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace era_engine
{
class InputRootComponent;
class MovementComponent;

class MovementSystem final : public System
{
public:
Expand All @@ -19,8 +16,5 @@ namespace era_engine
void reset_input(float dt);

ERA_VIRTUAL_REFLECT(System)

private:
InputRootComponent* input_rc = nullptr;
};
}
4 changes: 3 additions & 1 deletion modules/core/src/asset/bin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2023-present Eldar Muradov. All rights reserved.

#include "asset/bin.h"

#include "asset/model_asset.h"
#include "asset/io.h"

Expand Down Expand Up @@ -359,7 +361,7 @@ namespace era_engine
return result;
}

NODISCARD ModelAsset loadBIN(const fs::path& path)
ModelAsset loadBIN(const fs::path& path)
{
PROFILE("Loading BIN");

Expand Down
14 changes: 14 additions & 0 deletions modules/core/src/asset/bin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "core_api.h"

namespace era_engine
{
struct ModelAsset;

ERA_CORE_API void writeBIN(const ModelAsset& asset, const fs::path& path);

ERA_CORE_API ModelAsset loadFBX(const fs::path& path, uint32 flags);
ERA_CORE_API ModelAsset loadOBJ(const fs::path& path, uint32 flags);
ERA_CORE_API ModelAsset loadBIN(const fs::path& path);
}
3 changes: 2 additions & 1 deletion modules/core/src/asset/fbx.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2023-present Eldar Muradov. All rights reserved.

#include "asset/bin.h"
#include "asset/deflate.h"
#include "asset/io.h"
#include "asset/model_asset.h"
Expand Down Expand Up @@ -2004,7 +2005,7 @@ namespace era_engine
return 0;
}

NODISCARD ModelAsset loadFBX(const fs::path& path, uint32 flags)
ModelAsset loadFBX(const fs::path& path, uint32 flags)
{
std::string pathStr = path.string();
const char* s = pathStr.c_str();
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/asset/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ namespace era_engine
return true;
}

bool saveImageToFile(const fs::path& filepath, DirectX::Image image)
bool saveImageToFile(const fs::path& filepath, const DirectX::Image& image)
{
fs::path extension = filepath.extension();

Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/asset/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace era_engine
bool loadImageFromFile(const fs::path& filepath, uint32 flags, DirectX::ScratchImage& scratchImage, D3D12_RESOURCE_DESC& textureDesc);
bool loadSVGFromFile(const fs::path& filepath, uint32 flags, DirectX::ScratchImage& scratchImage, D3D12_RESOURCE_DESC& textureDesc);

bool saveImageToFile(const fs::path& filepath, DirectX::Image image);
bool saveImageToFile(const fs::path& filepath, const DirectX::Image& image);

NODISCARD inline constexpr bool isUAVCompatibleFormat(DXGI_FORMAT format)
{
Expand Down
Loading

0 comments on commit a25e355

Please sign in to comment.