light/modules/asset_baker/bakers.cppm

70 lines
1.7 KiB
Text
Raw Normal View History

export module bakers;
import debug.assertions;
import assets.metadata;
import assets.shader;
import logger;
2025-11-18 19:06:44 +03:30
import lsd;
namespace lt {
export void bake_shader(
2025-11-18 19:06:44 +03:30
const lsd::file::path &in_path,
const lsd::file::path &out_path,
assets::ShaderAsset::Type type
)
{
2025-11-18 19:06:44 +03:30
using assets::ShaderAsset;
using enum assets::ShaderAsset::Type;
2025-11-18 19:06:44 +03:30
auto glsl_path = lsd::str { in_path.string() };
auto spv_path = lsd::format("{}.spv", glsl_path);
log::trace(
"Compiling {} shader {} -> {}",
type == vertex ? "vertex" : "fragment",
2025-11-18 19:06:44 +03:30
lsd::str { glsl_path },
lsd::str { spv_path }
);
// Don't bother linking to shaderc, just invoke the command with a system call.
// NOLINTNEXTLINE(concurrency-mt-unsafe)
2025-11-18 19:06:44 +03:30
lsd::system(lsd::format(
"glslc --target-env=vulkan1.4 -std=450core -fshader-stage={} {} -o {}",
type == vertex ? "vert" : "frag",
glsl_path,
spv_path
)
.c_str());
2025-11-18 19:06:44 +03:30
auto stream = lsd::file::in_stream(spv_path, lsd::file::ios_binary);
debug::ensure(
stream.is_open(),
"Failed to open compiled {} shader at: {}",
type == vertex ? "vert" : "frag",
spv_path
);
2025-11-18 19:06:44 +03:30
stream.seekg(0, lsd::file::ios_end);
const auto size = stream.tellg();
2025-11-18 19:06:44 +03:30
auto bytes = lsd::vec<byte>(size);
stream.seekg(0, lsd::file::ios_beg);
stream.read((char *)bytes.data(), size); // NOLINT
2025-11-18 19:06:44 +03:30
log::debug("BYTES: {}", bytes.size());
stream.close();
2025-11-18 19:06:44 +03:30
lsd::file::remove(spv_path);
ShaderAsset::pack(
out_path,
2025-11-18 19:06:44 +03:30
assets::AssetMetadata {
.version = assets::current_version,
.type = ShaderAsset::asset_type_identifier,
},
ShaderAsset::Metadata {
.type = type,
},
2025-11-18 19:06:44 +03:30
lsd::move(bytes)
);
}
2025-11-18 19:06:44 +03:30
} // namespace lt