18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-06 00:45:19 +01:00
Files
maszyna/application/scenarioloadermode.cpp
T
maj00r d8c41a7ded fix(model): the review of #596: nothing streamed with modelformat legacy, a replaced streamed file not retried, lit instances not sheared
- Streams_Geometry() is true only with modelformat ng3d and modelstreaming, on both renderers; the geometry streamer, the
  prefetch and the wait of the loading screen run only when geometry is streamed. The texture prefetch goes by its own
  setting (gfx.textures.streaming).
- A streamed .ng3d replaced while the game runs fails for good (geometry_source::FailsForGood()), with a log line saying so,
  instead of being retried every 5 s to 5 min.
- The CPU instanced path zeroes the light word in the w of the first column before view * root, as instance_cull.comp does.
- Only the bank of the skin cache gets the storage usage on Vulkan (Create_Skinning_Bank()).
- lightsframe is atomic: relaxed loads outside the lock of the walks, the check and the store under it.
- A mesh of several primitives is logged once (only the first is drawn, as before).
- The structure-only read checks the counts of the primitives against the sizes of the streams and the int offsets.
- positions() loads a streamed bank as vertices() does; load_now() is non-const, no const_cast.
- The channel of the animation event gets the missing-value check of the clip.
- Sonar S2259: Prefetch_Material() checks the material against the number of materials.
2026-12-05 12:19:36 +01:00

190 lines
7.8 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 "application/scenarioloadermode.h"
#include "utilities/Globals.h"
#include "simulation/simulation.h"
#include "simulation/simulationtime.h"
#include "simulation/simulationenvironment.h"
#include "simulation/simulationtest.h"
#include "application/application.h"
#include "application/scenarioloaderuilayer.h"
#include "rendering/renderer.h"
#include "rendering/geometrystreamer.h"
#include "rendering/common/modelprefetch.h"
#include "vehicle/DynObj.h"
#include "utilities/Logs.h"
#include "utilities/translation.h"
#include "audio/audiorenderer.h"
#include <format>
scenarioloader_mode::scenarioloader_mode() {
m_userinterface = std::make_shared<scenarioloader_ui>();
}
// initializes internal data structures of the mode. returns: true on success, false otherwise
bool scenarioloader_mode::init() {
// nothing to do here
return true;
}
// mode-specific update of simulation data. returns: false on error, true otherwise
bool scenarioloader_mode::update() {
if (!Global.ready_to_load)
// waiting for network connection
return true;
if (m_closing > 0) {
// the programs of the materials made ready a few at a frame, the loading screen with its bar kept on the screen and the
// window answering meanwhile; then on to the simulation
auto const [done, total] { GfxRenderer->Prewarm_Shaders( 50.0 ) };
if( done < total ) {
m_userinterface->set_progress( static_cast<float>( done ) * 100.f / static_cast<float>( total ), 0.f );
return true;
}
if( false == geometry_ready() ) {
return true;
}
m_closing = 0;
Application.pop_mode();
Application.push_mode( eu07_application::mode::driver );
return true;
}
if (!state) {
WriteLog("using simulation seed: " + std::to_string(Global.random_seed), logtype::generic);
WriteLog("using simulation starting timestamp: " + std::to_string(Global.starting_timestamp), logtype::generic);
// the command line gives the name lowercased, the file may be named in another case, as a scenery made on Windows;
// the scenery is saved back to its own file then
if (auto const cased{FileCased(Global.asCurrentSceneryPath + Global.SceneryFile)}; cased.size() > Global.asCurrentSceneryPath.size())
Global.SceneryFile = cased.substr(Global.asCurrentSceneryPath.size());
Application.set_title( Global.AppName + " (" + Global.SceneryFile + ")" );
WriteLog( "\nLoading scenario \"" + Global.SceneryFile + "\"..." );
timestart = std::chrono::system_clock::now();
try {
state = simulation::State.deserialize_begin(Global.SceneryFile);
}
// the file it can't read, as the parser and the scenery opening report it; invalid_scenery_exception is one of these, and so is
// what the file system throws
catch (std::runtime_error const &e) {
ErrorLog(std::format("Bad init: scenario \"{}\" couldn't be opened ({})", Global.SceneryFile, e.what()));
state.reset();
if (Global.test.enabled) {
simulation::test().finish(simulation::test_result::error, "scenario couldn't be opened");
return true;
}
Global.SceneryFile.clear();
Application.pop_mode();
Application.push_mode(eu07_application::mode::launcher);
return true;
}
}
try {
if (simulation::State.deserialize_continue(state))
return true;
}
catch (invalid_scenery_exception &e) {
// back to the launcher as when the scenario can't be opened, not on to the driver mode with the scenery half loaded
ErrorLog( "Bad init: scenario loading failed" );
state.reset();
if( Global.test.enabled ) {
// there's nothing to test; the exit code says the run went wrong
simulation::test().finish( simulation::test_result::error, "scenario loading failed" );
return true;
}
Global.SceneryFile.clear();
Application.pop_mode();
Application.push_mode(eu07_application::mode::launcher);
return true;
}
// the loading state holds the scenario file open, which on some systems keeps the file from being replaced
// when the scenery is saved in the editor. it'd also get in the way of another load
state.reset();
// sound files decoded in the background are all ready before the simulation starts
audio::renderer.complete_buffers();
WriteLog( "Scenario loading time: " + std::to_string( std::chrono::duration_cast<std::chrono::seconds>( std::chrono::system_clock::now() - timestart ).count() ) + " seconds" );
// TODO: implement and use next mode cue
// the loading screen stays, saying the graphics are being prepared, until the programs are ready (above).
// at the top of the screen: the bottom is taken by the trivia and by the texts some backgrounds carry. the interface
// of this mode is always the loader's, made in the constructor
m_userinterface->set_progress(std::string{});
static_cast<scenarioloader_ui &>(*m_userinterface).set_notice(STR("Preparing the graphics (takes longer at the first start of a new version)"));
m_closing = 1;
return true;
}
// the geometry of the streamed models around the start (modelstreaming) loaded with the loading screen kept, so the simulation starts with
// them drawn; two minutes at most, the rest comes as it's drawn
bool scenarioloader_mode::geometry_ready() {
auto const geometry { GfxRenderer->Streams_Geometry() };
if( ( false == geometry ) && ( false == GfxRenderer->Prefetches_Textures() ) ) {
return true;
}
auto &streamer { gfx::geometry_streamer::instance() };
if( false == m_geometryasked ) {
m_geometryasked = true;
m_geometrystart = std::chrono::steady_clock::now();
// where the simulation starts: the vehicle of the player, or the free camera
auto const *vehicle { Global.local_start_vehicle != "ghostview" ? simulation::Vehicles.find( Global.local_start_vehicle ) : nullptr };
auto const start { vehicle != nullptr ? vehicle->GetPosition() : glm::dvec3 { Global.FreeCameraInit[ 0 ] } };
GfxRenderer->Collect_Start_Textures( true );
gfx::model_prefetch::instance().update( start, true );
GfxRenderer->Collect_Start_Textures( false );
m_geometrytotal = geometry ? streamer.pending() : 0;
}
// the geometry handed to the banks, the data of the textures read by their workers, then the textures made on the gpu
auto left { ( geometry ? streamer.finish( std::chrono::milliseconds { 50 } ) : 0 ) + GfxRenderer->Textures_Being_Read() };
if( left == 0 ) {
left = GfxRenderer->Make_Start_Textures();
}
if( m_geometrytotal < left ) {
m_geometrytotal = left;
}
if( ( left == 0 ) || ( std::chrono::steady_clock::now() - m_geometrystart > std::chrono::minutes { 2 } ) ) {
WriteLog( std::format( "Geometry of the streamed models around the start loaded, {} of {} in {} ms", m_geometrytotal - std::min( left, m_geometrytotal ), m_geometrytotal,
std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - m_geometrystart ).count() ) );
return true;
}
auto const done { m_geometrytotal - std::min( left, m_geometrytotal ) };
m_userinterface->set_progress( static_cast<float>( done ) * 100.f / static_cast<float>( std::max<std::size_t>( m_geometrytotal, 1 ) ), 0.f );
return false;
}
bool scenarioloader_mode::is_command_processor() const {
return false;
}
// maintenance method, called when the mode is activated
void scenarioloader_mode::enter() {
// TBD: hide cursor in fullscreen mode?
Application.set_cursor( GLFW_CURSOR_NORMAL );
simulation::is_ready = false;
Application.set_title( Global.AppName + " (" + Global.SceneryFile + ")" );
m_userinterface->set_progress(STR("Loading scenery"));
// the notice of a load before is gone
static_cast<scenarioloader_ui &>(*m_userinterface).set_notice({});
}
// maintenance method, called when the mode is deactivated
void scenarioloader_mode::exit() {
simulation::Time.init( Global.starting_timestamp );
simulation::Environment.init();
}