light/modules/assets/shader.test.cpp

116 lines
3.1 KiB
C++
Raw Normal View History

2026-01-20 13:22:30 +03:30
import test;
import assets.metadata;
import assets.shader;
2025-10-01 17:29:45 +03:30
using ::lt::assets::AssetMetadata;
2026-01-20 13:22:30 +03:30
using ::lt::assets::Blob;
2025-10-01 17:29:45 +03:30
using ::lt::assets::BlobMetadata;
using ::lt::assets::ShaderAsset;
const auto test_data_path = std::filesystem::path { "./data/test_assets" };
const auto tmp_path = std::filesystem::path { "/tmp/lt_assets_tests/" };
2026-01-20 13:22:30 +03:30
[[nodiscard]] auto generate_blob(size_t size) -> Blob
{
auto blob = Blob {};
for (auto idx : std::views::iota(0u, size))
{
blob.emplace_back(static_cast<byte>(idx));
}
return blob;
}
2025-10-01 17:29:45 +03:30
Suite raii = "shader_raii"_suite = [] {
std::filesystem::current_path(test_data_path);
std::filesystem::create_directories(tmp_path);
2026-01-20 13:22:30 +03:30
Case { "happy paths" } = [] {
auto shader_asset = ShaderAsset { "triangle.frag.asset" };
2025-10-01 17:29:45 +03:30
};
2026-01-20 13:22:30 +03:30
Case { "unhappy paths" } = [] {
// non-existent file
expect_throw([] { ShaderAsset { "path" }; });
// incompatible type
expect_throw([] { ShaderAsset { "dummytext" }; });
2026-01-20 13:22:30 +03:30
// some random stressing
expect_throw([] {
for (auto idx : std::views::iota(0u, 1'000u))
{
auto shader_asset = ShaderAsset { std::to_string(idx) };
}
});
2025-10-01 17:29:45 +03:30
};
2026-01-20 13:22:30 +03:30
Case { "many" } = [] {
for (auto idx : std::views::iota(0u, 1'000u))
{
ignore = idx;
auto shader_asset = ShaderAsset { "triangle.frag.asset" };
}
};
2025-10-01 17:29:45 +03:30
};
Suite packing = "shader_pack"_suite = [] {
2026-01-20 13:22:30 +03:30
Case { "Unpacking packed data returns the same data" } = [] {
2025-10-01 17:29:45 +03:30
const auto out_path = tmp_path / "shader_packing";
2026-01-20 13:22:30 +03:30
constexpr auto blob_size = size_t { 255u };
auto blob = generate_blob(blob_size);
2025-10-01 17:29:45 +03:30
const auto expected_size = //
sizeof(AssetMetadata::type) //
+ sizeof(AssetMetadata::version) //
+ sizeof(ShaderAsset::Metadata::type) //
+ sizeof(BlobMetadata::tag) //
+ sizeof(BlobMetadata::offset) //
+ sizeof(BlobMetadata::compression_type) //
+ sizeof(BlobMetadata::compressed_size) //
+ sizeof(BlobMetadata::uncompressed_size) //
2026-01-20 13:22:30 +03:30
+ blob.size();
2025-10-01 17:29:45 +03:30
ShaderAsset::pack(
out_path,
lt::assets::AssetMetadata {
.version = lt::assets::current_version,
.type = ShaderAsset::asset_type_identifier,
},
ShaderAsset::Metadata {
.type = ShaderAsset::Type::vertex,
},
2026-01-20 13:22:30 +03:30
std::move(blob)
2025-10-01 17:29:45 +03:30
);
auto stream = std::ifstream {
out_path,
2025-10-08 06:28:34 +03:30
std::ios::binary,
2025-10-01 17:29:45 +03:30
};
expect_true(stream.is_open());
2026-01-09 21:53:37 +03:30
stream.seekg(0u, std::ios::end);
2026-01-20 09:58:35 +03:30
const auto file_size = static_cast<size_t>(stream.tellg());
2025-10-01 17:29:45 +03:30
expect_eq(file_size, expected_size);
stream.close();
auto shader_asset = ShaderAsset { out_path };
const auto &asset_metadata = shader_asset.get_asset_metadata();
expect_eq(asset_metadata.type, ShaderAsset::asset_type_identifier);
expect_eq(asset_metadata.version, lt::assets::current_version);
const auto &metadata = shader_asset.get_metadata();
expect_eq(metadata.type, ShaderAsset::Type::vertex);
2026-01-20 13:22:30 +03:30
auto unpakced_blob = shader_asset.unpack(ShaderAsset::BlobTag::code);
expect_eq(unpakced_blob.size(), blob_size);
2025-10-01 17:29:45 +03:30
2026-01-20 13:22:30 +03:30
for (auto idx : std::views::iota(0u, blob_size))
2025-10-01 17:29:45 +03:30
{
2026-01-20 13:22:30 +03:30
expect_eq(unpakced_blob[idx], static_cast<byte>(idx));
2025-10-01 17:29:45 +03:30
}
};
};