18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 14:07:06 +01:00
Files
maszyna/tests/unit/test_gref.cpp
T
maj00r d930b6519d feat(model): the geometry shared by the variants of a family kept once, in a store of the folder
The variants of a vehicle repeat the same parts byte for byte, and each EU07M file carried them all again. PR #521 showed the
problem on E3D; EU07M solves it in the format.

- Block: the geometry of one primitive, its vertices in every vertex stream of the file and its indices, with a 16-byte header;
  its id is 128 bits, two FNV-1a 64 of its bytes. Only a primitive whose vertex and index ranges are its own can be shared.
- Store: geometry.eu07mpack in the folder of the models, the blocks each once (zstd when it saves an eighth) and an index
  sorted by their id (model/eu07m/eu07m_store.{h,cpp}).
- GREF, a new required section: the primitives kept in the store. The writer leaves their ranges out of the streams of the
  file; the reader takes their blocks from the store (read_options::blocks), checks the id and the counts of each and puts
  them back in place, so the model in memory has its whole streams and nothing past the reader knows the difference. A
  model with references and no store, or with a block which isn't the one referred to, is refused with the reason. The
  validator checks the references (primitive, counts, ranges of its own).
- The game opens the store of a folder once and keeps it (eu07m::family_store()); dump and validate read it too.
- Tools: "-eu07m pack <folder> [minimum bytes]" moves every block of at least 4 KiB (by default) which occurs more than
  once in the models of the folder into its store, made anew from all of them, and writes the models again referring to it,
  each read back and compared with what it was before it's replaced; "-eu07m unpack <folder>" writes them whole again.

Measured on copies of the converted models of the game: cd/181_v2 60.8 MB -> 4.0 MB (7%), pkp/11xa_v2/main 69.3 MB ->
5.8 MB (8%), pkp/elf_v1 109.6 MB -> 39.0 MB (36%), pkp/en57_v1 104.1 MB -> 39.3 MB (38%), 2-4 s a folder. All 223 packed
files pass -eu07m validate with their store. Four ELF parts drawn in the game (Vulkan) from the packed and from the whole
files, with the auto exposure off and the same seed: no pixel differs by more than 1. Unit tests 1208/1208, 4 new ones for a
round trip through a store, a model without its store, a wrong block, a primitive sharing its vertices and a folder packed,
packed again and unpacked.

The renderer still makes the geometry of each model on its own; sharing one range of the buffers between the models of a
family is the next step. No asset change: the game data has no .eu07m files, and the release doesn't read them.
2026-11-29 04:03:49 +01:00

214 lines
8.7 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_io.h"
#include "model/eu07m/eu07m_pack.h"
#include "model/eu07m/eu07m_store.h"
#include <filesystem>
#include <fstream>
#include <sstream>
namespace {
// a model of two meshes: a grid of Size x Size vertices, offset by Shift (the part the variants share when they have the same
// shift), and a small quad of its own
eu07m::model variant(int const Size, float const Shift, float const QuadHeight) {
eu07m::model model;
auto const node{[&model](std::string_view const Name, std::int32_t const Parent) {
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
model.nodes.push_back(record);
return static_cast<std::uint32_t>(model.nodes.size() - 1);
}};
auto const root{node("root", -1)};
auto const body{node("body", static_cast<std::int32_t>(root))};
auto const plate{node("plate", static_cast<std::int32_t>(root))};
model.nodes[root].subtree_end = 3;
model.nodes[body].subtree_end = 2;
model.nodes[plate].subtree_end = 3;
model.materials.push_back({});
auto const add_primitive = [&model](std::uint32_t const Node, std::uint32_t const First, std::uint32_t const Base, std::uint32_t const Vertices, std::uint32_t const Indices) {
eu07m::primitive_record primitive;
primitive.first_index = First;
primitive.index_count = Indices;
primitive.base_vertex = Base;
primitive.vertex_count = Vertices;
primitive.node = Node;
primitive.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
model.meshes.push_back({static_cast<std::uint32_t>(model.primitives.size()), 1, -1, 0});
model.nodes[Node].mesh = static_cast<std::int32_t>(model.meshes.size() - 1);
model.primitives.push_back(primitive);
};
auto const vertex = [&model](float const X, float const Y, float const Z) {
model.positions.push_back({X, Y, Z});
eu07m::attributes attribute;
attribute.qtangent = eu07m::encode_qtangent({{0.f, 0.f, 1.f}, {1.f, 0.f, 0.f}, 1.f});
attribute.uv = {X, Y};
model.vertex_attributes.push_back(attribute);
};
for (int y = 0; y < Size; ++y)
for (int x = 0; x < Size; ++x)
vertex(static_cast<float>(x) + Shift, static_cast<float>(y), 0.f);
for (int y = 0; y + 1 < Size; ++y) {
for (int x = 0; x + 1 < Size; ++x) {
auto const a{static_cast<std::uint16_t>(y * Size + x)};
auto const b{static_cast<std::uint16_t>(a + 1)};
auto const c{static_cast<std::uint16_t>(a + Size)};
auto const d{static_cast<std::uint16_t>(c + 1)};
for (auto const index : {a, b, d, a, d, c})
model.indices16.push_back(index);
}
}
add_primitive(body, 0, 0, static_cast<std::uint32_t>(Size * Size), static_cast<std::uint32_t>(model.indices16.size()));
auto const base{static_cast<std::uint32_t>(model.positions.size())};
auto const first{static_cast<std::uint32_t>(model.indices16.size())};
for (auto const &corner : std::array<std::array<float, 2>, 4>{{{0.f, 0.f}, {1.f, 0.f}, {1.f, 1.f}, {0.f, 1.f}}})
vertex(corner[0], corner[1], QuadHeight);
model.indices16.insert(model.indices16.end(), {0, 1, 2, 0, 2, 3});
add_primitive(plate, first, base, 4, 6);
eu07m::build_name_index(model);
eu07m::compute_bounds(model);
return model;
}
eu07m::block_source memory_source(eu07m::geometry_store const &Store) {
return [&Store](eu07m::geometry_reference_record const &Reference, std::vector<std::byte> &Block) { return Store.block(Reference.hash, Block); };
}
bool same_streams(eu07m::model const &A, eu07m::model const &B) {
return std::ranges::equal(std::as_bytes(std::span{A.positions}), std::as_bytes(std::span{B.positions})) &&
std::ranges::equal(std::as_bytes(std::span{A.vertex_attributes}), std::as_bytes(std::span{B.vertex_attributes})) && A.indices16 == B.indices16;
}
void save(std::filesystem::path const &Path, std::span<std::byte const> Data) {
std::ofstream file{Path, std::ios::binary};
file.write(reinterpret_cast<char const *>(Data.data()), static_cast<std::streamsize>(Data.size()));
}
std::vector<std::byte> load(std::filesystem::path const &Path) {
std::ifstream file{Path, std::ios::binary | std::ios::ate};
std::vector<std::byte> data(static_cast<std::size_t>(file.tellg()));
file.seekg(0);
file.read(reinterpret_cast<char *>(data.data()), static_cast<std::streamsize>(data.size()));
return data;
}
} // namespace
TEST_CASE("gref: a primitive kept in the store comes back whole") {
auto model{variant(30, 0.f, 2.f)};
REQUIRE(eu07m::shareable(model, 0));
auto const block{eu07m::make_block(model, model.primitives[0])};
eu07m::geometry_store store;
std::string error;
REQUIRE(store.open(eu07m::write_store({block}), error));
CHECK(store.size() == 1);
eu07m::geometry_reference_record reference;
reference.primitive = 0;
reference.hash = eu07m::block_hash(block);
reference.vertex_count = model.primitives[0].vertex_count;
reference.index_count = model.primitives[0].index_count;
model.geometry_references.push_back(reference);
CHECK(eu07m::validate(model, 16).empty());
auto const whole{eu07m::write(variant(30, 0.f, 2.f))};
auto const file{eu07m::write(model)};
CHECK(file.size() < whole.size() / 2);
eu07m::model read;
eu07m::read_options options;
options.blocks = memory_source(store);
REQUIRE(eu07m::read(file, read, options).ok);
CHECK(same_streams(read, model));
CHECK(read.geometry_references.size() == 1);
// written again from what was read, the same file
CHECK(eu07m::write(read) == file);
// without the store the file is refused
eu07m::model refused;
auto const result{eu07m::read(file, refused, {})};
CHECK_FALSE(result.ok);
CHECK(result.error.find("geometry store") != std::string::npos);
}
TEST_CASE("gref: a block which isn't the one referred to is refused") {
auto model{variant(30, 0.f, 2.f)};
auto block{eu07m::make_block(model, model.primitives[0])};
eu07m::geometry_reference_record reference;
reference.primitive = 0;
reference.hash = eu07m::block_hash(block);
reference.vertex_count = model.primitives[0].vertex_count;
reference.index_count = model.primitives[0].index_count;
model.geometry_references.push_back(reference);
auto const file{eu07m::write(model)};
// a vertex of the block changed: the hash doesn't match
block[sizeof(eu07m::block_header) + 4] ^= std::byte{0x40};
eu07m::geometry_store store;
std::string error;
REQUIRE(store.open(eu07m::write_store({block}), error));
eu07m::model read;
eu07m::read_options options;
options.blocks = [&store, &block](eu07m::geometry_reference_record const &, std::vector<std::byte> &Out) { return store.block(eu07m::block_hash(block), Out); };
CHECK_FALSE(eu07m::read(file, read, options).ok);
}
TEST_CASE("gref: a primitive sharing its vertices can't be kept in the store") {
auto model{variant(10, 0.f, 2.f)};
model.primitives[1].base_vertex = 0; // the quad on the vertices of the grid
CHECK_FALSE(eu07m::shareable(model, 0));
eu07m::geometry_reference_record reference;
reference.primitive = 0;
reference.vertex_count = model.primitives[0].vertex_count;
reference.index_count = model.primitives[0].index_count;
model.geometry_references.push_back(reference);
CHECK_FALSE(eu07m::validate(model, 16).empty());
}
TEST_CASE("gref: a folder packed and unpacked") {
auto const folder{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / "gref_family"};
std::filesystem::remove_all(folder);
std::filesystem::create_directories(folder);
// two variants with the same grid, one with a grid of its own; the quads differ
auto const a{variant(40, 0.f, 1.f)};
auto const b{variant(40, 0.f, 2.f)};
auto const c{variant(40, 5.f, 3.f)};
save(folder / "a.eu07m", eu07m::write(a));
save(folder / "b.eu07m", eu07m::write(b));
save(folder / "c.eu07m", eu07m::write(c));
std::ostringstream log;
auto const packed{eu07m::pack_folder(folder, 4096, log)};
CAPTURE(log.str());
REQUIRE(packed.ok);
CHECK(packed.models == 3);
CHECK(packed.blocks == 1); // the grid of a and b; the quads are too small, c's grid is its own
CHECK(packed.references == 2);
CHECK(packed.bytes_after < packed.bytes_before);
eu07m::geometry_store store;
std::string error;
REQUIRE(store.open(folder / eu07m::store_file_name, error));
eu07m::read_options options;
options.blocks = memory_source(store);
for (auto const &[name, original] : {std::pair{"a.eu07m", &a}, std::pair{"b.eu07m", &b}, std::pair{"c.eu07m", &c}}) {
eu07m::model read;
REQUIRE(eu07m::read(load(folder / name), read, options).ok);
CHECK(same_streams(read, *original));
}
// packed again, the same
std::ostringstream again;
CHECK(eu07m::pack_folder(folder, 4096, again).blocks == 1);
auto const unpacked{eu07m::unpack_folder(folder, log)};
REQUIRE(unpacked.ok);
eu07m::model whole;
REQUIRE(eu07m::read(load(folder / "b.eu07m"), whole, {}).ok);
CHECK(whole.geometry_references.empty());
CHECK(same_streams(whole, b));
std::filesystem::remove_all(folder);
}