18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 02:42:46 +01:00
Files
maszyna/rendering/geometrystreamer.h
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

127 lines
5.1 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
// the streaming of the geometry of the scenery models (modelstreaming): a streamed geometry bank is made with its chunks empty, and
// its data is read and unpacked by the threads of the streamer when the bank is about to be drawn (or ahead of it, the prefetch),
// handed to the bank by the main thread a few at a frame, and let go of once the bank isn't drawn for a while. what reads the vertices
// of a bank without its data loads them at once (geometry_bank::load_now())
#include "rendering/geometrybank.h"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <map>
#include <mutex>
#include <string>
#include <string_view>
#include <stop_token>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>
namespace gfx {
class geometry_streamer {
public:
geometry_streamer() = default;
~geometry_streamer();
geometry_streamer( geometry_streamer const & ) = delete;
geometry_streamer &operator=( geometry_streamer const & ) = delete;
static auto instance() -> geometry_streamer &;
// the clock the banks note their use with, in milliseconds; moved on by update()
static auto now() -> std::uint32_t;
// a load of the bank asked for, or asked for sooner (the lower the Priority, the sooner). any thread
void request( geometry_bank &Bank, float const Priority );
// the main thread, once a frame, while no walk draws: the loads done handed to their banks as far as the budget of the frame goes,
// and the banks unused for a while let go of
void update();
// the loads done handed over at once, the ones under way waited for, till none is left or Timeout passes (the loading screen).
// returns: the loads left
auto finish( std::chrono::milliseconds const Timeout ) -> std::size_t;
// the loads asked for and not handed over yet
auto pending() const -> std::size_t;
// a bank which got its data by itself (geometry_bank::load_now()), let go of with the others
void register_resident( geometry_bank &Bank );
// the data of every streamed bank which isn't kept for good let go of (the self-test: modelstream evict). the main thread
auto evict_all() -> std::size_t;
// a load which failed, told in the log by the main thread. any thread
void report_failure( std::string_view const Name, std::string_view const Error );
// the threads ended and the loads dropped, before the banks go (the renderer is closed)
void stop();
// how many times banks were given their data. the walks kept by the gpu culling, made while a bank had none, are made anew when
// it changes
auto adoptions() const -> std::uint64_t {
return m_adoptions.load( std::memory_order_acquire ); }
struct statistics {
std::size_t resident { 0 }; // banks with their data
std::size_t bytes { 0 }; // of their data
std::size_t pending { 0 };
std::uint64_t loaded { 0 }; // since the start
std::uint64_t evicted { 0 };
std::uint64_t failed { 0 };
};
auto stats() const -> statistics;
private:
// types
// a bank asked for: by the priority, then in the order of the requests
using queue_key = std::pair<float, std::uint64_t>;
using queue_map = std::map<queue_key, geometry_bank *>;
struct result {
geometry_bank *bank { nullptr };
std::uint32_t epoch { 0 };
bank_data data;
bool ok { false };
std::string error;
};
// methods
void start_threads();
void work( std::stop_token const Stop );
// the results handed to their banks, at most Bytes of them (0: all). returns: the bytes handed over
auto hand_over( std::size_t const Bytes, std::chrono::steady_clock::duration const Time ) -> std::size_t;
void hand_over_one( result &Result );
// a part of the banks with their data gone through, the ones unused for a while let go of
void sweep();
void report_failures();
// members
mutable std::mutex m_mutex;
std::condition_variable_any m_wake; // a request for the threads
std::condition_variable m_idle; // a load done
queue_map m_queue;
std::unordered_map<geometry_bank *, queue_map::iterator> m_queued;
std::uint64_t m_order { 0 };
std::size_t m_working { 0 }; // loads under way
std::deque<result> m_done;
std::vector<std::jthread> m_threads;
bool m_stopped { false };
std::vector<geometry_bank *> m_resident; // the banks with their data, for the sweep
std::size_t m_sweepcursor { 0 };
std::vector<std::string> m_failures;
std::atomic<std::uint64_t> m_adoptions { 0 };
std::chrono::steady_clock::time_point const m_start { std::chrono::steady_clock::now() };
std::atomic<std::uint32_t> m_now { 0 }; // now(), since m_start
std::atomic<std::uint64_t> m_loaded { 0 };
std::atomic<std::uint64_t> m_evicted { 0 };
std::atomic<std::uint64_t> m_failed { 0 };
};
} // namespace gfx