refactor(asset_baker): removed unused cod & add shader baking support through system glslc calls

This commit is contained in:
light7734 2025-10-01 17:30:22 +03:30
parent 0c4b3dd0f9
commit 4ad50122ef
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
5 changed files with 85 additions and 179 deletions

View file

@ -1,9 +1,17 @@
add_executable_module( add_library_module(libasset_baker
asset_baker entrypoint/baker.cpp bakers.cpp
)
target_link_libraries(libasset_baker
PUBLIC
assets
logger
lt_debug
)
add_test_module(libasset_baker
bakers.test.cpp
) )
target_link_libraries( add_executable_module(asset_baker
asset_baker entrypoint/baker.cpp
PRIVATE asset_parser
PRIVATE logger
) )
target_link_libraries(asset_baker PRIVATE libasset_baker)

View file

View file

@ -0,0 +1,7 @@
#include <asset_baker/bakers.hpp>
#include <test/test.hpp>
using ::lt::test::Case;
using ::lt::test::Suite;
// TODO(Light): add asset baking tests!

View file

@ -1,68 +1,5 @@
#include <asset_baker/bakers.hpp> #include <asset_baker/bakers.hpp>
#include <asset_parser/assets/text.hpp> #include <assets/shader.hpp>
#include <asset_parser/assets/texture.hpp>
#include <asset_parser/parser.hpp>
#include <filesystem>
#include <logger/logger.hpp>
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());
}
}
auto main(int argc, char *argv[]) -> int32_t auto main(int argc, char *argv[]) -> int32_t
try try
@ -81,12 +18,16 @@ try
} }
const auto &in_path = directory_iterator.path(); const auto &in_path = directory_iterator.path();
const auto out_path = std::format("{}.asset", in_path.c_str());
auto out_path = in_path; if (in_path.extension() == ".vert")
out_path.replace_extension(".asset"); {
bake_shader(in_path, out_path, lt::assets::ShaderAsset::Type::vertex);
try_packing_texture(in_path, out_path); }
try_packing_text(in_path, out_path); else if (in_path.extension() == ".frag")
{
bake_shader(in_path, out_path, lt::assets::ShaderAsset::Type::fragment);
}
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

View file

@ -1,114 +1,64 @@
#pragma once #pragma once
#include <asset_parser/assets/text.hpp> #include <assets/shader.hpp>
#include <asset_parser/assets/texture.hpp>
#include <filesystem>
#include <logger/logger.hpp>
#include <string_view>
#include <unordered_set>
namespace lt { inline void bake_shader(
const std::filesystem::path &in_path,
class Loader const std::filesystem::path &out_path,
lt::assets::ShaderAsset::Type type
)
{ {
public: using lt::assets::ShaderAsset;
[[nodiscard]] virtual auto get_name() const -> std::string_view = 0; 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<std::byte>(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; ShaderAsset::pack(
out_path,
private: lt::assets::AssetMetadata {
}; .version = lt::assets::current_version,
.type = ShaderAsset::asset_type_identifier,
class TextureLoader: public Loader },
{ ShaderAsset::Metadata {
public: .type = type,
TextureLoader() = default; },
std::move(bytes)
[[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<TextureLoader>
{
return {};
}
};
class TextLoader: Loader
{
public:
[[nodiscard]] static auto get_supported_extensions() -> std::unordered_set<std::string_view>
{
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<long>(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<TextLoader>
{
if (TextLoader::get_supported_extensions().contains(file_extension))
{
return std::make_unique<TextLoader>();
}
return {};
}
};
} // namespace lt