mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 09:36:48 +01:00
89405a6eaf
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.
192 lines
7.2 KiB
C++
192 lines
7.2 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/.
|
|
*/
|
|
|
|
/*
|
|
MaSzyna EU07 locomotive simulator
|
|
Copyright (C) 2001-2004 Marcin Wozniak and others
|
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "model/MdlMngr.h"
|
|
|
|
#include "model/Model3d.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/Logs.h"
|
|
#include "utilities/utilities.h"
|
|
|
|
// wczytanie modelu do kontenerka
|
|
TModel3d *
|
|
TMdlContainer::LoadModel(std::string const &Name, bool const Dynamic, bool const Streamable) {
|
|
|
|
Model = std::make_shared<TModel3d>();
|
|
Model->m_streamable = Streamable;
|
|
if( true == Model->LoadFromFile( Name, Dynamic ) ) {
|
|
m_name = Name;
|
|
return Model.get();
|
|
}
|
|
else {
|
|
m_name.clear();
|
|
Model = nullptr;
|
|
return nullptr;
|
|
}
|
|
};
|
|
|
|
TModelsManager::modelcontainer_sequence TModelsManager::m_models { 1, TMdlContainer{} };
|
|
TModelsManager::stringmodelcontainerindex_map TModelsManager::m_modelsmap;
|
|
|
|
// what the loaded models keep in memory, for the debug panel
|
|
TModelsManager::memory_data
|
|
TModelsManager::memory() {
|
|
|
|
memory_data result;
|
|
std::vector<TSubModel const *> pending;
|
|
for( auto const &container : m_models ) {
|
|
if( container.Model == nullptr ) { continue; }
|
|
++result.models;
|
|
pending.assign( 1, container.Model->Root );
|
|
while( false == pending.empty() ) {
|
|
auto const *submodel { pending.back() };
|
|
pending.pop_back();
|
|
if( submodel == nullptr ) { continue; }
|
|
++result.submodels.count;
|
|
result.submodels.bytes += memory_of( submodel->Vertices ) + memory_of( submodel->Userdata ) + memory_of( submodel->Indices );
|
|
pending.push_back( submodel->Child );
|
|
pending.push_back( submodel->Next );
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// wczytanie modelu do tablicy
|
|
TModel3d *
|
|
TModelsManager::LoadModel(std::string const &Name, std::string const &virtualName, bool dynamic, bool const Streamable) {
|
|
|
|
m_models.emplace_back();
|
|
auto model = m_models.back().LoadModel( Name, dynamic, Streamable );
|
|
if( model != nullptr ) {
|
|
m_modelsmap.try_emplace( virtualName, m_models.size() - 1 );
|
|
}
|
|
else {
|
|
m_models.pop_back();
|
|
m_modelsmap.try_emplace( virtualName, null_handle );
|
|
}
|
|
return model;
|
|
}
|
|
|
|
TModel3d *
|
|
TModelsManager::GetModel(std::string const &Name, bool const Dynamic, bool const Logerrors, int uid, bool const Streamable )
|
|
{ // model może być we wpisie "node...model" albo "node...dynamic", a także być dodatkowym w dynamic
|
|
// (kabina, wnętrze, ładunek)
|
|
// dla "node...dynamic" mamy podaną ścieżkę w "\dynamic\" i musi być co najmniej 1 poziom, zwkle
|
|
// są 2
|
|
// dla "node...model" może być typowy model statyczny ze ścieżką, domyślnie w "\scenery\" albo
|
|
// "\models"
|
|
// albo może być model z "\dynamic\", jeśli potrzebujemy wstawić auto czy wagon nieruchomo
|
|
// - ze ścieżki z której jest wywołany, np. dir="scenery\bud\" albo dir="dynamic\pkp\st44_v1\"
|
|
// plus name="model.t3d"
|
|
// - z domyślnej ścieżki dla modeli, np. "scenery\" albo "models\" plus name="bud\dombale.t3d"
|
|
// (dir="")
|
|
// - konkretnie podanej ścieżki np. name="\scenery\bud\dombale.t3d" (dir="")
|
|
// wywołania:
|
|
// - konwersja wszystkiego do E3D, podawana dokładna ścieżka, tekstury tam, gdzie plik
|
|
// - wczytanie kabiny, dokładna ścieżka, tekstury z katalogu modelu
|
|
// - wczytanie ładunku, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - wczytanie modelu, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - wczytanie przedsionków, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - wczytanie uproszczonego wnętrza, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - niebo animowane, ścieżka brana ze wpisu, tekstury nieokreślone
|
|
// - wczytanie modelu animowanego - Init() - sprawdzić
|
|
std::string const buftp { Global.asCurrentTexturePath }; // zapamiętanie aktualnej ścieżki do tekstur,
|
|
std::string filename { Name };
|
|
if( false == Dynamic
|
|
&& contains(Name, '/') ) {
|
|
// pobieranie tekstur z katalogu, w którym jest model
|
|
// when loading vehicles the path is set by the calling routine, so we can skip it here
|
|
Global.asCurrentTexturePath += Name;
|
|
Global.asCurrentTexturePath.erase( Global.asCurrentTexturePath.rfind( '/' ) + 1 );
|
|
}
|
|
erase_extension( filename );
|
|
filename = ToLower( filename );
|
|
std::string postfix;
|
|
if (uid != 0)
|
|
postfix = "^^" + std::to_string(uid);
|
|
|
|
// see if we have it in the databank
|
|
auto [banklookupfound, banklookupmodel]{ find_in_databank( filename + postfix ) };
|
|
TModel3d *model { banklookupmodel };
|
|
if( true == banklookupfound ) {
|
|
Global.asCurrentTexturePath = buftp;
|
|
// a model streamed for the scenery, wanted now by something which reads its vertices (a vehicle): its geometry kept for good
|
|
if( ( model != nullptr ) && ( false == Streamable ) ) {
|
|
model->pin_geometry();
|
|
}
|
|
return model;
|
|
}
|
|
|
|
// first load attempt, check if it's on disk
|
|
|
|
if( std::string disklookup { find_on_disk( filename ) }; false == disklookup.empty() ) {
|
|
model = LoadModel( disklookup, disklookup + postfix, Dynamic, Streamable ); // model nie znaleziony, to wczytać
|
|
}
|
|
else {
|
|
// there's nothing matching in the databank nor on the disk, report failure...
|
|
if( Logerrors ) {
|
|
ErrorLog( "Bad file: failed to locate 3d model file \"" + filename + "\"", logtype::file );
|
|
}
|
|
// ...and link it with the error model slot
|
|
m_modelsmap.try_emplace( filename + postfix, null_handle );
|
|
}
|
|
Global.asCurrentTexturePath = buftp; // odtworzenie ścieżki do tekstur
|
|
return model; // NULL jeśli błąd
|
|
};
|
|
|
|
std::pair<bool, TModel3d *>
|
|
TModelsManager::find_in_databank( std::string const &Name ) {
|
|
|
|
std::vector<std::string> filenames {
|
|
Name,
|
|
paths::models + Name };
|
|
|
|
for( auto const &filename : filenames ) {
|
|
auto const lookup { m_modelsmap.find( filename ) };
|
|
if( lookup != m_modelsmap.end() ) {
|
|
return { true, m_models[ lookup->second ].Model.get() };
|
|
}
|
|
}
|
|
|
|
return { false, nullptr };
|
|
}
|
|
|
|
// checks whether specified file exists. returns name of the located file, or empty string.
|
|
// the file is found in any case, as the scenery made on Windows names it, but the name is given back as asked for: it's the key of
|
|
// the bank, and TModel3d::LoadFromFile() opens the file as it's named on the disk
|
|
std::string
|
|
TModelsManager::find_on_disk( std::string const &Name ) {
|
|
|
|
std::vector<std::string> extensions { { ".e3d" }, { ".t3d" } };
|
|
if( Global.modelformat_eu07m ) {
|
|
// a model may come as an .eu07m alone
|
|
extensions.emplace_back( ".eu07m" );
|
|
}
|
|
for( auto const &extension : extensions ) {
|
|
|
|
if( false == FileCased( Name + extension ).empty() ) {
|
|
return Name;
|
|
}
|
|
if( false == FileCased( paths::models + Name + extension ).empty() ) {
|
|
return paths::models + Name;
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|