mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 02:42:46 +01:00
65a7bed64e
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.
341 lines
10 KiB
C++
341 lines
10 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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "rendering/geometrystreamer.h"
|
|
|
|
#include "rendering/common/walksync.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/Logs.h"
|
|
|
|
#include <algorithm>
|
|
#include <format>
|
|
|
|
namespace gfx {
|
|
|
|
namespace {
|
|
|
|
// what a frame hands to the banks at most: the bytes of their data (the vulkan renderer sends them to the gpu the next time it draws
|
|
// them) and the time
|
|
constexpr std::size_t frame_bytes { std::size_t { 64 } << 20 };
|
|
constexpr std::chrono::milliseconds frame_time { 3 };
|
|
// the banks with their data gone through by the sweep of a frame
|
|
constexpr std::size_t sweep_count { 256 };
|
|
// over the budget of the memory (modelstreaming.budget), a bank unused for this long is let go of before its time
|
|
constexpr std::uint32_t over_budget_unused_ms { 2000 };
|
|
|
|
|
|
} // namespace
|
|
|
|
geometry_streamer &
|
|
geometry_streamer::instance() {
|
|
|
|
static geometry_streamer streamer;
|
|
return streamer;
|
|
}
|
|
|
|
geometry_streamer::~geometry_streamer() {
|
|
|
|
stop();
|
|
}
|
|
|
|
std::uint32_t
|
|
geometry_streamer::now() {
|
|
|
|
return instance().m_now.load( std::memory_order_relaxed );
|
|
}
|
|
|
|
void
|
|
geometry_streamer::start_threads() {
|
|
|
|
if( ( false == m_threads.empty() ) || m_stopped ) { return; }
|
|
// the loads read the disk and unpack: a few threads, not to take the cores the frame needs
|
|
auto const count { std::clamp( std::thread::hardware_concurrency() / 4u, 1u, 3u ) };
|
|
for( unsigned idx = 0; idx < count; ++idx ) {
|
|
m_threads.emplace_back( [ this ]( std::stop_token const Stop ) { work( Stop ); } );
|
|
}
|
|
}
|
|
|
|
void
|
|
geometry_streamer::request( geometry_bank &Bank, float const Priority ) {
|
|
|
|
std::scoped_lock lock { m_mutex };
|
|
if( m_stopped ) { return; }
|
|
if( auto const queued { m_queued.find( &Bank ) }; queued != m_queued.end() ) {
|
|
// asked for sooner: put ahead
|
|
if( Priority < queued->second->first.first ) {
|
|
m_queue.erase( queued->second );
|
|
queued->second = m_queue.emplace( queue_key { Priority, m_order++ }, &Bank ).first;
|
|
}
|
|
return;
|
|
}
|
|
if( false == Bank.begin_load( Priority ) ) { return; }
|
|
m_queued.emplace( &Bank, m_queue.emplace( queue_key { Priority, m_order++ }, &Bank ).first );
|
|
start_threads();
|
|
m_wake.notify_one();
|
|
}
|
|
|
|
void
|
|
geometry_streamer::work( std::stop_token const Stop ) {
|
|
|
|
while( false == Stop.stop_requested() ) {
|
|
geometry_bank *bank { nullptr };
|
|
{
|
|
std::unique_lock lock { m_mutex };
|
|
if( false == m_wake.wait( lock, Stop, [ this ]() { return false == m_queue.empty(); } ) ) {
|
|
return; // stopped
|
|
}
|
|
auto const first { m_queue.begin() };
|
|
bank = first->second;
|
|
m_queued.erase( bank );
|
|
m_queue.erase( first );
|
|
++m_working;
|
|
}
|
|
// the epoch the load is of: a bank loaded or let go of meanwhile doesn't take it (geometry_bank::adopt())
|
|
result done;
|
|
done.bank = bank;
|
|
done.epoch = bank->epoch();
|
|
done.ok = bank->source()->load( done.data, bank->plain_layout(), done.error );
|
|
{
|
|
std::scoped_lock lock { m_mutex };
|
|
m_done.emplace_back( std::move( done ) );
|
|
--m_working;
|
|
}
|
|
m_idle.notify_all();
|
|
}
|
|
}
|
|
|
|
void
|
|
geometry_streamer::hand_over_one( result &Result ) {
|
|
|
|
auto &bank { *Result.bank };
|
|
if( false == Result.ok ) {
|
|
if( false == bank.fail_load( Result.epoch ) ) {
|
|
return; // loaded meanwhile by the thread which read it
|
|
}
|
|
++m_failed;
|
|
if( bank.failures() == 1 ) {
|
|
ErrorLog( std::format( "Bad model: the geometry of \"{}\" can't be loaded: {}", bank.source()->name(), Result.error ), logtype::model );
|
|
}
|
|
return;
|
|
}
|
|
if( false == bank.adopt( Result.data, Result.epoch ) ) {
|
|
// loaded meanwhile by the thread which read it, or let go of
|
|
return;
|
|
}
|
|
++m_loaded;
|
|
m_adoptions.fetch_add( 1, std::memory_order_acq_rel );
|
|
std::scoped_lock lock { m_mutex };
|
|
m_resident.push_back( &bank );
|
|
}
|
|
|
|
std::size_t
|
|
geometry_streamer::hand_over( std::size_t const Bytes, std::chrono::steady_clock::duration const Time ) {
|
|
|
|
auto const start { std::chrono::steady_clock::now() };
|
|
std::size_t handed { 0 };
|
|
while( true ) {
|
|
result done;
|
|
{
|
|
std::scoped_lock lock { m_mutex };
|
|
if( m_done.empty() ) { break; }
|
|
done = std::move( m_done.front() );
|
|
m_done.pop_front();
|
|
}
|
|
{
|
|
// the chunks of the bank change while no walk of the passes draws (gfx::walk_sync)
|
|
gfx::walk_sync::change change;
|
|
hand_over_one( done );
|
|
}
|
|
handed += done.bank->data_bytes();
|
|
if( ( Bytes > 0 ) && ( ( handed >= Bytes ) || ( std::chrono::steady_clock::now() - start >= Time ) ) ) {
|
|
break;
|
|
}
|
|
}
|
|
return handed;
|
|
}
|
|
|
|
void
|
|
geometry_streamer::update() {
|
|
|
|
m_now.store( static_cast<std::uint32_t>( std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - m_start ).count() ), std::memory_order_relaxed );
|
|
hand_over( frame_bytes, frame_time );
|
|
report_failures();
|
|
sweep();
|
|
}
|
|
|
|
std::size_t
|
|
geometry_streamer::finish( std::chrono::milliseconds const Timeout ) {
|
|
|
|
auto const deadline { std::chrono::steady_clock::now() + Timeout };
|
|
while( true ) {
|
|
hand_over( 0, {} );
|
|
report_failures();
|
|
std::unique_lock lock { m_mutex };
|
|
auto const left { m_queue.size() + m_working + m_done.size() };
|
|
if( ( left == 0 ) || ( std::chrono::steady_clock::now() >= deadline ) ) {
|
|
return left;
|
|
}
|
|
m_idle.wait_for( lock, std::chrono::milliseconds { 50 } );
|
|
}
|
|
}
|
|
|
|
std::size_t
|
|
geometry_streamer::pending() const {
|
|
|
|
std::scoped_lock lock { m_mutex };
|
|
return m_queue.size() + m_working + m_done.size();
|
|
}
|
|
|
|
void
|
|
geometry_streamer::register_resident( geometry_bank &Bank ) {
|
|
|
|
m_adoptions.fetch_add( 1, std::memory_order_acq_rel );
|
|
std::scoped_lock lock { m_mutex };
|
|
++m_loaded;
|
|
m_resident.push_back( &Bank );
|
|
}
|
|
|
|
void
|
|
geometry_streamer::report_failure( std::string_view const Name, std::string_view const Error ) {
|
|
|
|
std::scoped_lock lock { m_mutex };
|
|
++m_failed;
|
|
m_failures.emplace_back( std::format( "Bad model: the geometry of \"{}\" can't be loaded: {}", Name, Error ) );
|
|
}
|
|
|
|
void
|
|
geometry_streamer::report_failures() {
|
|
|
|
std::vector<std::string> failures;
|
|
{
|
|
std::scoped_lock lock { m_mutex };
|
|
failures.swap( m_failures );
|
|
}
|
|
for( auto const &failure : failures ) {
|
|
ErrorLog( failure, logtype::model );
|
|
}
|
|
}
|
|
|
|
void
|
|
geometry_streamer::sweep() {
|
|
|
|
std::vector<geometry_bank *> unused;
|
|
{
|
|
std::scoped_lock lock { m_mutex };
|
|
if( m_resident.empty() ) { return; }
|
|
auto const clock { now() };
|
|
auto const lifetime { static_cast<std::uint32_t>( std::max( 0.f, Global.modelstreaming_evictafter ) * 1000.f ) };
|
|
std::size_t bytes { 0 };
|
|
if( Global.modelstreaming_budget > 0 ) {
|
|
for( auto const *bank : m_resident ) {
|
|
bytes += bank->data_bytes();
|
|
}
|
|
}
|
|
auto const overbudget { ( Global.modelstreaming_budget > 0 ) && ( bytes > ( static_cast<std::size_t>( Global.modelstreaming_budget ) << 20 ) ) };
|
|
auto const limit { overbudget ? std::min( lifetime, over_budget_unused_ms ) : lifetime };
|
|
for( std::size_t step = 0; ( step < sweep_count ) && ( false == m_resident.empty() ); ++step ) {
|
|
if( m_sweepcursor >= m_resident.size() ) {
|
|
m_sweepcursor = 0;
|
|
}
|
|
auto *bank { m_resident[ m_sweepcursor ] };
|
|
auto const gone { bank->state() != residency::resident };
|
|
auto const stale { ( false == gone ) && ( false == bank->pinned() ) && ( clock - bank->last_used() > limit ) };
|
|
if( false == ( gone || stale ) ) {
|
|
++m_sweepcursor;
|
|
continue;
|
|
}
|
|
if( stale ) {
|
|
unused.push_back( bank );
|
|
}
|
|
m_resident[ m_sweepcursor ] = m_resident.back();
|
|
m_resident.pop_back();
|
|
}
|
|
}
|
|
if( unused.empty() ) { return; }
|
|
// the chunks of the banks change while no walk of the passes draws (gfx::walk_sync)
|
|
gfx::walk_sync::change change;
|
|
for( auto *bank : unused ) {
|
|
bank->evict();
|
|
}
|
|
m_evicted += unused.size();
|
|
}
|
|
|
|
std::size_t
|
|
geometry_streamer::evict_all() {
|
|
|
|
std::vector<geometry_bank *> banks;
|
|
{
|
|
std::scoped_lock lock { m_mutex };
|
|
banks.swap( m_resident );
|
|
m_sweepcursor = 0;
|
|
}
|
|
std::size_t evicted { 0 };
|
|
gfx::walk_sync::change change;
|
|
for( auto *bank : banks ) {
|
|
if( bank->pinned() || ( bank->state() != residency::resident ) ) {
|
|
if( bank->state() == residency::resident ) {
|
|
std::scoped_lock lock { m_mutex };
|
|
m_resident.push_back( bank );
|
|
}
|
|
continue;
|
|
}
|
|
bank->evict();
|
|
++evicted;
|
|
}
|
|
m_evicted += evicted;
|
|
return evicted;
|
|
}
|
|
|
|
void
|
|
geometry_streamer::stop() {
|
|
|
|
std::vector<std::jthread> threads;
|
|
{
|
|
std::scoped_lock lock { m_mutex };
|
|
m_stopped = true;
|
|
threads.swap( m_threads );
|
|
}
|
|
for( auto &thread : threads ) {
|
|
thread.request_stop();
|
|
}
|
|
m_wake.notify_all();
|
|
threads.clear(); // joined here
|
|
std::scoped_lock lock { m_mutex };
|
|
m_queue.clear();
|
|
m_queued.clear();
|
|
m_done.clear();
|
|
m_resident.clear();
|
|
}
|
|
|
|
void
|
|
geometry_streamer::resume() {
|
|
|
|
std::scoped_lock lock { m_mutex };
|
|
m_stopped = false;
|
|
}
|
|
|
|
geometry_streamer::statistics
|
|
geometry_streamer::stats() const {
|
|
|
|
statistics values;
|
|
std::scoped_lock lock { m_mutex };
|
|
values.resident = m_resident.size();
|
|
for( auto const *bank : m_resident ) {
|
|
values.bytes += bank->data_bytes();
|
|
}
|
|
values.pending = m_queue.size() + m_working + m_done.size();
|
|
values.loaded = m_loaded;
|
|
values.evicted = m_evicted;
|
|
values.failed = m_failed;
|
|
return values;
|
|
}
|
|
|
|
} // namespace gfx
|