diff --git a/modules/asset_baker/CMakeLists.txt b/modules/asset_baker/CMakeLists.txt index a3814d1..f11bcbf 100644 --- a/modules/asset_baker/CMakeLists.txt +++ b/modules/asset_baker/CMakeLists.txt @@ -1,9 +1,17 @@ -add_executable_module( - asset_baker entrypoint/baker.cpp +add_library_module(libasset_baker + bakers.cpp +) +target_link_libraries(libasset_baker +PUBLIC + assets + logger + lt_debug +) +add_test_module(libasset_baker + bakers.test.cpp ) -target_link_libraries( - asset_baker - PRIVATE asset_parser - PRIVATE logger +add_executable_module(asset_baker + entrypoint/baker.cpp ) +target_link_libraries(asset_baker PRIVATE libasset_baker) diff --git a/modules/asset_baker/private/bakers.cpp b/modules/asset_baker/private/bakers.cpp new file mode 100644 index 0000000..e69de29 diff --git a/modules/asset_baker/private/bakers.test.cpp b/modules/asset_baker/private/bakers.test.cpp new file mode 100644 index 0000000..5cd6b9e --- /dev/null +++ b/modules/asset_baker/private/bakers.test.cpp @@ -0,0 +1,7 @@ +#include +#include + +using ::lt::test::Case; +using ::lt::test::Suite; + +// TODO(Light): add asset baking tests! diff --git a/modules/asset_baker/private/entrypoint/baker.cpp b/modules/asset_baker/private/entrypoint/baker.cpp index 418feec..7ab8fe0 100644 --- a/modules/asset_baker/private/entrypoint/baker.cpp +++ b/modules/asset_baker/private/entrypoint/baker.cpp @@ -1,68 +1,5 @@ #include -#include -#include -#include -#include -#include - -void try_packing_texture( - const std::filesystem::path &in_path, - const std::filesystem::path &out_path -) -{ - auto texture_loader = lt::TextureLoaderFactory::create(in_path.extension().string()); - if (!texture_loader) - { - // Don't log anything; this is expected. - return; - } - - try - { - Assets::TextureAsset::pack(texture_loader->load(in_path), out_path); - - log_inf("Packed a texture asset:"); - log_inf("\tloader : {}", texture_loader->get_name()); - log_inf("\tin path: {}", in_path.string()); - log_inf("\tout path: {}", out_path.string()); - } - catch (const std::exception &exp) - { - log_err("Failed to pack texture asset:"); - log_err("\tloader : {}", texture_loader->get_name()); - log_err("\tin path : {}", in_path.string()); - log_err("\tout path: {}", out_path.string()); - log_err("\texp.what: {}", exp.what()); - } -} - -void try_packing_text(const std::filesystem::path &in_path, const std::filesystem::path &out_path) -{ - auto text_loader = lt::TextLoaderFactory::create(in_path.extension().string()); - if (!text_loader) - { - // Don't log anything; this is expected. - return; - } - - try - { - Assets::TextAsset::pack(text_loader->load(in_path), out_path); - - log_inf("Packed a text asset:"); - log_inf("\tloader : {}", text_loader->get_name()); - log_inf("\tin path: {}", in_path.string()); - log_inf("\tout path: {}", out_path.string()); - } - catch (const std::exception &exp) - { - log_err("Failed to pack a text asset:"); - log_err("\tloader : {}", text_loader->get_name()); - log_err("\tin path : {}", in_path.string()); - log_err("\tout path: {}", out_path.string()); - log_err("\texp.what: {}", exp.what()); - } -} +#include auto main(int argc, char *argv[]) -> int32_t try @@ -81,12 +18,16 @@ try } const auto &in_path = directory_iterator.path(); + const auto out_path = std::format("{}.asset", in_path.c_str()); - auto out_path = in_path; - out_path.replace_extension(".asset"); - - try_packing_texture(in_path, out_path); - try_packing_text(in_path, out_path); + if (in_path.extension() == ".vert") + { + bake_shader(in_path, out_path, lt::assets::ShaderAsset::Type::vertex); + } + else if (in_path.extension() == ".frag") + { + bake_shader(in_path, out_path, lt::assets::ShaderAsset::Type::fragment); + } } return EXIT_SUCCESS; diff --git a/modules/asset_baker/public/bakers.hpp b/modules/asset_baker/public/bakers.hpp index c16a803..0415fdc 100644 --- a/modules/asset_baker/public/bakers.hpp +++ b/modules/asset_baker/public/bakers.hpp @@ -1,114 +1,64 @@ #pragma once -#include -#include -#include -#include -#include -#include +#include -namespace lt { - -class Loader +inline void bake_shader( + const std::filesystem::path &in_path, + const std::filesystem::path &out_path, + lt::assets::ShaderAsset::Type type +) { -public: - [[nodiscard]] virtual auto get_name() const -> std::string_view = 0; + using lt::assets::ShaderAsset; + using enum lt::assets::ShaderAsset::Type; - Loader() = default; + auto glsl_path = in_path.string(); + auto spv_path = std::format("{}.spv", glsl_path); + log_trc( + "Compiling {} shader {} -> {}", + type == vertex ? "vertex" : "fragment", + glsl_path, + spv_path + ); - Loader(Loader &&) = default; + // Don't bother linking to shaderc, just invoke the command with a system call. + // NOLINTNEXTLINE(concurrency-mt-unsafe) + system( + std::format( + "glslc --target-env=vulkan1.4 -std=450core -fshader-stage={} {} -o {}", + type == vertex ? "vert" : "frag", + glsl_path, + spv_path + ) + .c_str() + ); - Loader(const Loader &) = delete; + auto stream = std::ifstream(spv_path, std::ios::binary); + lt::ensure( + stream.is_open(), + "Failed to open compiled {} shader at: {}", + type == vertex ? "vert" : "frag", + spv_path + ); - auto operator=(Loader &&) -> Loader & = default; + stream.seekg(0, std::ios::end); + const auto size = stream.tellg(); - auto operator=(const Loader &) -> Loader & = delete; + auto bytes = std::vector(size); + stream.seekg(0, std::ios::beg); + stream.read((char *)bytes.data(), size); // NOLINT + log_dbg("BYTES: {}", bytes.size()); + stream.close(); + std::filesystem::remove(spv_path); - virtual ~Loader() = default; - -private: -}; - -class TextureLoader: public Loader -{ -public: - TextureLoader() = default; - - [[nodiscard]] virtual auto load(std::filesystem::path file_path) const - -> Assets::TextureAsset::PackageData - = 0; -}; - -class TextureLoaderFactory -{ -public: - static auto create(std::string_view file_extension) -> std::unique_ptr - { - return {}; - } -}; - -class TextLoader: Loader -{ -public: - [[nodiscard]] static auto get_supported_extensions() -> std::unordered_set - { - return { ".glsl", ".txt", ".hlsl" }; - } - - [[nodiscard]] auto get_name() const -> std::string_view override - { - return "TextLoader"; - } - - [[nodiscard]] auto load(const std::filesystem::path &file_path) const - -> Assets::TextAsset::PackageData - { - auto stream = std::ifstream { file_path, std::ios::binary }; - if (!stream.good()) - { - throw std::runtime_error { - std::format( - "Failed to open ifstream for text loading of file: {}", - file_path.string() - ), - }; - } - - auto file_size = std::filesystem::file_size(file_path); - - auto text_blob = Assets::Blob(file_size); - - stream.read((char *)(text_blob.data()), static_cast(file_size)); // NOLINT - - const auto metadata = Assets::Asset::Metadata { - .type = Assets::Asset::Type::Text, - }; - - const auto text_metadata = Assets::TextAsset::Metadata { - .lines = {}, - }; - - return Assets::TextAsset::PackageData { - .metadata = metadata, - .text_metadata = {}, - .text_blob = std::move(text_blob), - }; - } -}; - -class TextLoaderFactory -{ -public: - static auto create(std::string_view file_extension) -> std::unique_ptr - { - if (TextLoader::get_supported_extensions().contains(file_extension)) - { - return std::make_unique(); - } - - return {}; - } -}; - -} // namespace lt + ShaderAsset::pack( + out_path, + lt::assets::AssetMetadata { + .version = lt::assets::current_version, + .type = ShaderAsset::asset_type_identifier, + }, + ShaderAsset::Metadata { + .type = type, + }, + std::move(bytes) + ); +}