18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 06:12:41 +01:00
Files
maszyna/tests/unit/test_eu07m_streaming.cpp
maj00r 7009b9b81b Merge branch 'feat/eu07m-gref' into feat/eu07m-geometry-streaming
Brings the .ng3d extension and modelformat ng3d (8637101d9). Where both sides changed a line, this branch's text is kept with the names changed; what this branch adds is renamed with it: the model files .ng3d, the family store .ng3p.
2026-11-30 22:27:11 +01:00

219 lines
8.7 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "scratch.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_dump.h"
#include "model/eu07m/eu07m_geometry.h"
#include "model/eu07m/eu07m_io.h"
#include <cstring>
#include <string_view>
// the parts of EU07M the streaming of the geometry (modelstreaming) stands on: the structure of a model read without its streams, the
// streams read later from the same file, and the chunks the game makes of the primitives
namespace {
// a model of two nodes with geometry and one without: a quad with indices and user data, and a strip without indices
eu07m::model streamed_model() {
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 quad{node("quad", static_cast<std::int32_t>(root))};
auto const strip{node("strip", static_cast<std::int32_t>(root))};
model.nodes[root].subtree_end = 3;
model.nodes[quad].subtree_end = 2;
model.nodes[strip].subtree_end = 3;
model.nodes[quad].translation = {10.f, 0.f, 0.f};
eu07m::material_record material;
material.name = model.intern("wall.mat");
model.materials.push_back(material);
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}}}) {
model.positions.push_back({corner[0], corner[1], 0.f});
eu07m::attributes attribute;
attribute.qtangent = eu07m::encode_qtangent({{0.f, 0.f, 1.f}, {1.f, 0.f, 0.f}, 1.f});
attribute.uv = {corner[0], corner[1]};
model.vertex_attributes.push_back(attribute);
model.vertex_userdata.push_back({corner[0], corner[1], 0.f, 1.f});
}
for (int idx = 0; idx < 3; ++idx) {
model.positions.push_back({0.f, static_cast<float>(idx), 2.f});
model.vertex_attributes.push_back({});
model.vertex_userdata.push_back({});
}
model.indices16 = {0, 1, 2, 0, 2, 3};
eu07m::primitive_record first;
first.index_count = 6;
first.vertex_count = 4;
first.node = quad;
first.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
model.primitives.push_back(first);
eu07m::primitive_record second;
second.base_vertex = 4;
second.vertex_count = 3;
second.index_count = 3;
second.node = strip;
second.flags = static_cast<std::uint32_t>(eu07m::topology::triangle_strip) | eu07m::primitive_flags::opaque;
model.primitives.push_back(second);
model.meshes.push_back({0, 1, -1, 0});
model.meshes.push_back({1, 1, -1, 0});
model.nodes[quad].mesh = 0;
model.nodes[strip].mesh = 1;
eu07m::build_name_index(model);
eu07m::compute_bounds(model);
return model;
}
std::string_view as_text(std::vector<std::byte> const &File) {
return {reinterpret_cast<char const *>(File.data()), File.size()};
}
eu07m::read_options game_options(bool const Streams) {
eu07m::read_options options{false, false};
options.streams = Streams;
return options;
}
} // namespace
TEST_CASE("eu07m streaming: a file read from the disk is the model read from memory")
{
test::scratch_folder folder{"eu07m_streaming_read"};
auto const original{streamed_model()};
REQUIRE(eu07m::validate(original).empty());
for (auto const compress : {false, true}) {
CAPTURE(compress);
auto const file{eu07m::write(original, {compress, 9})};
auto const path{folder.write("model.ng3d", as_text(file))};
eu07m::model frommemory;
auto const memory{eu07m::read(file, frommemory, game_options(true))};
REQUIRE_MESSAGE(memory.ok, memory.error);
eu07m::model fromdisk;
auto const disk{eu07m::read_file(path, fromdisk, game_options(true))};
REQUIRE_MESSAGE(disk.ok, disk.error);
CHECK(eu07m::dump(fromdisk) == eu07m::dump(frommemory));
CHECK(fromdisk.indices16 == frommemory.indices16);
CHECK(fromdisk.positions.size() == 7);
CHECK(disk.content_hash == memory.content_hash);
CHECK(disk.content_hash != 0);
CHECK(disk.stream_bytes == memory.stream_bytes);
CHECK(disk.stream_bytes > 0);
}
}
TEST_CASE("eu07m streaming: without the streams the structure alone is read and checked")
{
test::scratch_folder folder{"eu07m_streaming_structure"};
auto const original{streamed_model()};
auto const path{folder.write("model.ng3d", as_text(eu07m::write(original)))};
eu07m::model structure;
auto const result{eu07m::read_file(path, structure, game_options(false))};
REQUIRE_MESSAGE(result.ok, result.error);
CHECK(structure.positions.empty());
CHECK(structure.vertex_attributes.empty());
CHECK(structure.vertex_userdata.empty());
CHECK(structure.indices16.empty());
CHECK(structure.nodes.size() == original.nodes.size());
CHECK(structure.primitives.size() == original.primitives.size());
CHECK(structure.bounds.size() == original.bounds.size());
CHECK(structure.strings == original.strings);
CHECK(eu07m::validate(structure, 16, eu07m::validate_scope::structure).empty());
// the whole check misses the streams
CHECK_FALSE(eu07m::validate(structure).empty());
// the same from memory
eu07m::model frommemory;
auto const memory{eu07m::read(eu07m::write(original), frommemory, game_options(false))};
REQUIRE_MESSAGE(memory.ok, memory.error);
CHECK(eu07m::dump(frommemory) == eu07m::dump(structure));
}
TEST_CASE("eu07m streaming: the structure is read without touching the streams")
{
test::scratch_folder folder{"eu07m_streaming_untouched"};
// the positions marked as packed with zstd, which they aren't: whatever reads them fails
auto file{eu07m::write(streamed_model(), {false, 9})};
auto const header{[&file] {
eu07m::file_header value;
std::memcpy(&value, file.data(), sizeof(value));
return value;
}()};
for (std::size_t idx = 0; idx < header.section_count; ++idx) {
eu07m::section_entry entry;
auto const offset{header.toc_offset + idx * sizeof(entry)};
std::memcpy(&entry, file.data() + offset, sizeof(entry));
if (entry.id != eu07m::section::positions)
continue;
REQUIRE((entry.flags & eu07m::section_flags::zstd) == 0);
entry.flags |= eu07m::section_flags::zstd;
std::memcpy(file.data() + offset, &entry, sizeof(entry));
}
auto const path{folder.write("model.ng3d", as_text(file))};
eu07m::model model;
auto const structure{eu07m::read_file(path, model, game_options(false))};
CHECK_MESSAGE(structure.ok, structure.error);
auto const whole{eu07m::read_file(path, model, game_options(true))};
CHECK_FALSE(whole.ok);
CHECK(model.nodes.empty());
}
TEST_CASE("eu07m streaming: a file cut short or missing is refused with a reason")
{
test::scratch_folder folder{"eu07m_streaming_broken"};
auto const file{eu07m::write(streamed_model())};
eu07m::model model;
auto const missing{eu07m::read_file(folder.path("none.ng3d"), model, game_options(false))};
CHECK_FALSE(missing.ok);
CHECK_FALSE(missing.error.empty());
auto const shortpath{folder.write("short.ng3d", as_text(file).substr(0, file.size() - 8))};
auto const cut{eu07m::read_file(shortpath, model, game_options(false))};
CHECK_FALSE(cut.ok);
CHECK(cut.error.find("cut short") != std::string::npos);
auto const tinypath{folder.write("tiny.ng3d", as_text(file).substr(0, 10))};
auto const tiny{eu07m::read_file(tinypath, model, game_options(false))};
CHECK_FALSE(tiny.ok);
CHECK(tiny.error.find("shorter than the header") != std::string::npos);
}
TEST_CASE("eu07m streaming: the chunks are the first primitives of the nodes with geometry, in their order")
{
auto const model{streamed_model()};
auto const primitives{eu07m::drawn_primitives(model)};
REQUIRE(primitives == std::vector<std::uint32_t>{0, 1});
auto const quad{eu07m::geometry_of(model, 0)};
CHECK(quad.indices == std::vector<std::uint32_t>{0, 1, 2, 0, 2, 3});
REQUIRE(quad.positions.size() == 4);
CHECK(quad.positions[2] == eu07m::position{1.f, 1.f, 0.f});
REQUIRE(quad.vertex_userdata.size() == 4);
CHECK(quad.vertex_userdata[1] == eu07m::userdata{1.f, 0.f, 0.f, 1.f});
auto const strip{eu07m::geometry_of(model, 1)};
CHECK(strip.indices.empty());
REQUIRE(strip.positions.size() == 3);
CHECK(strip.positions[0] == eu07m::position{0.f, 0.f, 2.f});
CHECK(strip.vertex_attributes.size() == 3);
// out of range: nothing
CHECK(eu07m::geometry_of(model, 7).positions.empty());
}
TEST_CASE("eu07m streaming: what keeps a model from being streamed")
{
auto model{streamed_model()};
CHECK(eu07m::not_streamable(model).empty());
auto withoutbounds{model};
withoutbounds.bounds.clear();
CHECK_FALSE(eu07m::not_streamable(withoutbounds).empty());
auto withskin{model};
withskin.skins.push_back({0, 0, -1, 0});
CHECK_FALSE(eu07m::not_streamable(withskin).empty());
auto withstars{model};
withstars.add_component(eu07m::component::legacy_stars, eu07m::target_kind::node, 1, eu07m::payload_writer{}.put(std::uint32_t{0}).data());
CHECK_FALSE(eu07m::not_streamable(withstars).empty());
}