18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 14:16:48 +01:00
Files
maszyna/tests/unit/test_eu07m_streaming.cpp
T
maj00r 89405a6eaf feat(render): the geometry of the scenery models and the data of the textures streamed, on by default
The streaming of the geometry of EU07M models (modelstreaming) and the streaming of the textures (gfx.textures.streaming) are now
both on by default; either is turned off in eu07.ini with "no", and turned off the game does what it did before.

Geometry (modelstreaming yes, with modelformat eu07m):
- A scenery model (node ... model) of an .eu07m file of at least modelstreaming.minsize KiB (256), without skins, stars and texts of
  E3D and with BNDS, has only its structure read at the load: one read of the beginning of the file (eu07m::read_file() with
  read_options::streams false, validate_scope::structure). Vehicles, cabs, loads and every other model load whole, as before; a
  streamed model asked for by anything else gets its geometry loaded and kept (TModelsManager::GetModel(..., Streamable)).
- Its geometry bank keeps its chunks empty (gfx::residency). A bank without its data draws nothing and is asked of the streamer
  (rendering/geometrystreamer.h): threads read and unpack the streams and the GREF blocks, nearest first, and the main thread hands
  them to the banks, 64 MB or 3 ms a frame. A bank unused for modelstreaming.evictafter s (30) lets go of its data in memory and on
  the GPU; above modelstreaming.budget MB (0, no limit) sooner. Whatever reads the vertices (picking, editor, sky occlusion) gets
  them loaded at once. The walks kept by the GPU culling are made anew when a bank got its data.
- The nodes, materials, components, clips and channels stay loaded: the walk goes through an instance without its geometry as
  through any other (RaAnimate(), RaPrepare()), only its draws are left out, so animations, lights and the events at their ends
  keep their timing. The radius of a streamed model and the bounds of its branches come from BNDS instead of the vertices.
- rendering/common/modelprefetch.h goes through the cells within the draw range and 500 m, 32 a frame, and asks for the geometry
  of the streamed models ahead of their drawing.

Textures (gfx.textures.streaming yes):
- The same pass has the data of the textures of the instances, shapes and tracks in reach read ahead by the texture workers in the
  Vulkan renderer (the OpenGL renderer has its own pass); the read-ahead data not drawn for 5 minutes or left behind is dropped.
- The loading screen waits, two minutes at most, for the geometry and the textures around the start and, in Vulkan, for their
  images made on the GPU (vulkan14_texture::request_background(false), made()).

Measured on Tomaszewo, Vulkan, editor self-test flight start to Kaletnik, four runs on an idle machine, alternated:
- peak committed memory 11.0 GB without streaming, 7.0 GB with textures, 6.2 GB with both; working set 9.0 / 4.8 / 4.2 GB
  (the copies of the texture data were 4.5 GB, the geometry banks 1.55 GB);
- scenario load 104-145 s without, 95-97 s with;
- frame time over the flight the same (11.8-12.5 ms), draws without their texture at the start the same as without streaming and
  fewer in the flight (380 against 636); the prefetch costs the main thread 0.1-0.2 ms a frame.

Also: the self-test commands "modelstream evict|wait|stats" and "memory <label>" (rendering/common/memoryreport.h, a breakdown of
the memory of the process), file_parts (utilities/wholefile.h) for reads of parts of a file, and the docs in docs/eu07m-format.md
("Streaming of the geometry"). The format doesn't change and no asset change is needed. Unit tests: test_eu07m_streaming.cpp.
2026-11-29 20:39:47 +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.eu07m", 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.eu07m", 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.eu07m", 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.eu07m"), model, game_options(false))};
CHECK_FALSE(missing.ok);
CHECK_FALSE(missing.error.empty());
auto const shortpath{folder.write("short.eu07m", 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.eu07m", 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());
}