18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 03:52:52 +01:00
Files
maszyna/rendering/geometrystreamer.h
T
maj00r 65a7bed64e fix(render): texture streaming by default with Vulkan only, the streamer after a fallback, failed banks tried again
After review of #555:

- gfx.textures.streaming is on by default with the Vulkan renderer only, the one it was measured with; with OpenGL it stays
  off as before. A value set in eu07.ini is kept for either renderer. A Vulkan renderer falling back to OpenGL as it starts
  goes without it. The default turned on is logged, as the settings are logged before the renderer is chosen.
- The geometry streamer stopped by the shutdown of a Vulkan renderer that failed to start resumes for the OpenGL renderer
  made after it (geometry_streamer::resume()); before, every streamed model stayed undrawn for the session.
- A bank whose load failed is tried again: 5 s after the first failure, twice as long after each one that follows, up to
  5 min, by the streamer as it's drawn and by load_now(). Only the first failure in a row is logged. A transient read error
  no longer leaves the model undrawn till a restart.
2026-11-30 20:56:03 +01:00

130 lines
5.3 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();
// the loads taken again after stop(), by a renderer made after another one closed (vulkan falling back to opengl as it starts,
// before any bank is made); the threads start with the first request
void resume();
// 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