fix(assets/shader): opening the file in non-binary mode

This commit is contained in:
light7734 2026-01-12 14:41:41 +03:30
parent bda9bb3dce
commit 0abd1c7e7f
2 changed files with 24 additions and 6 deletions

View file

@ -89,7 +89,8 @@ constexpr auto total_metadata_size = //
+ sizeof(BlobMetadata::compressed_size) // + sizeof(BlobMetadata::compressed_size) //
+ sizeof(BlobMetadata::uncompressed_size); + sizeof(BlobMetadata::uncompressed_size);
ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path) ShaderAsset::ShaderAsset(const std::filesystem::path &path)
: m_stream(path, std::ios::beg | std::ios::binary)
{ {
debug::ensure(m_stream.is_open(), "Failed to open shader asset at: {}", path.string()); debug::ensure(m_stream.is_open(), "Failed to open shader asset at: {}", path.string());
const auto read = [this](auto &field) { const auto read = [this](auto &field) {
@ -210,10 +211,10 @@ void ShaderAsset::unpack_to(BlobTag tag, std::span<std::byte> destination) const
m_code_blob_metadata.uncompressed_size m_code_blob_metadata.uncompressed_size
); );
m_stream.seekg(static_cast<long long>(m_code_blob_metadata.offset)); m_stream.seekg(static_cast<long long>(m_code_blob_metadata.offset), std::ifstream::beg);
m_stream.read( m_stream.read(
std::bit_cast<char *>(destination.data()), std::bit_cast<char *>(destination.data()),
static_cast<long long>(m_code_blob_metadata.uncompressed_size) m_code_blob_metadata.uncompressed_size
); );
} }

View file

@ -1,6 +1,7 @@
import assets.metadata; import assets.metadata;
import assets.shader; import assets.shader;
import logger; import logger;
import logger;
import test.test; import test.test;
import test.expects; import test.expects;
import std; import std;
@ -23,18 +24,34 @@ Suite raii = "shader_raii"_suite = [] {
std::filesystem::create_directories(tmp_path); std::filesystem::create_directories(tmp_path);
Case { "happy path won't throw" } = [] { Case { "happy path won't throw" } = [] {
auto shader_asset = ShaderAsset { "triangle.frag.asset" };
}; };
Case { "many won't freeze/throw" } = [] { Case { "many won't freeze/throw" } = [] {
for (auto idx : std::views::iota(0u, 1'000u))
{
std::ignore = idx;
auto shader_asset = ShaderAsset { "triangle.frag.asset" };
}
}; };
Case { "unhappy path throws" } = [] { Case { "unhappy path throws" } = [] {
expect_throw([] { ShaderAsset { "random_path" }; }); // non-existent file
expect_throw([] { ShaderAsset { "path" }; });
// incompatible type
expect_throw([] { ShaderAsset { "dummytext" }; });
// random stressing
expect_throw([] {
for (auto idx : std::views::iota(0u, 1'000u))
{
auto shader_asset = ShaderAsset { std::to_string(idx) };
}
});
}; };
}; };
// NOLINTNEXTLINE(cppcoreguidelines-interfaces-global-init)
Suite packing = "shader_pack"_suite = [] { Suite packing = "shader_pack"_suite = [] {
Case { "" } = [] { Case { "" } = [] {
const auto out_path = tmp_path / "shader_packing"; const auto out_path = tmp_path / "shader_packing";