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(
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)

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_parser/assets/text.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());
}
}
#include <assets/shader.hpp>
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;

View file

@ -1,114 +1,64 @@
#pragma once
#include <asset_parser/assets/text.hpp>
#include <asset_parser/assets/texture.hpp>
#include <filesystem>
#include <logger/logger.hpp>
#include <string_view>
#include <unordered_set>
#include <assets/shader.hpp>
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;
Loader(const Loader &) = delete;
auto operator=(Loader &&) -> Loader & = default;
auto operator=(const Loader &) -> Loader & = delete;
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<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 {
// Don't bother linking to shaderc, just invoke the command with a system call.
// NOLINTNEXTLINE(concurrency-mt-unsafe)
system(
std::format(
"Failed to open ifstream for text loading of file: {}",
file_path.string()
),
};
}
"glslc --target-env=vulkan1.4 -std=450core -fshader-stage={} {} -o {}",
type == vertex ? "vert" : "frag",
glsl_path,
spv_path
)
.c_str()
);
auto file_size = std::filesystem::file_size(file_path);
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 text_blob = Assets::Blob(file_size);
stream.seekg(0, std::ios::end);
const auto size = stream.tellg();
stream.read((char *)(text_blob.data()), static_cast<long>(file_size)); // NOLINT
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);
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
ShaderAsset::pack(
out_path,
lt::assets::AssetMetadata {
.version = lt::assets::current_version,
.type = ShaderAsset::asset_type_identifier,
},
ShaderAsset::Metadata {
.type = type,
},
std::move(bytes)
);
}