mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 03:52:52 +01:00
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.
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
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/common/modelprefetch.h"
|
||||
|
||||
#include "model/AnimModel.h"
|
||||
#include "model/Model3d.h"
|
||||
#include "world/Track.h"
|
||||
#include "rendering/renderer.h"
|
||||
#include "scene/scene.h"
|
||||
#include "simulation/simulation.h"
|
||||
#include "utilities/Globals.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
|
||||
namespace gfx {
|
||||
|
||||
namespace {
|
||||
|
||||
// the way the data has to arrive from, before the model is drawn (m)
|
||||
constexpr double prefetch_margin { 500.0 };
|
||||
// the cells gone through at a frame
|
||||
constexpr std::size_t cells_per_frame { 32 };
|
||||
constexpr std::size_t cells_per_section { ( scene::EU07_SECTIONSIZE / scene::EU07_CELLSIZE ) * ( scene::EU07_SECTIONSIZE / scene::EU07_CELLSIZE ) };
|
||||
|
||||
// what the instance will need when it's drawn from within the margin: the geometry of a streamed model, asked for by its distance, and
|
||||
// with Textures the data of its textures, read once until the instance is out of reach again
|
||||
bool prefetch_instance( TAnimModel &Instance, glm::dvec3 const &Camera, double const Drawrange, bool const Textures ) {
|
||||
|
||||
auto *model { Instance.Model() };
|
||||
if( ( model == nullptr ) || ( false == Instance.visible() ) ) { return false; }
|
||||
// as far as the walk of the scene draws it (scene_walk::Render( TAnimModel * )), and the margin
|
||||
auto const reach { std::min( std::sqrt( Instance.m_rangesquaredmax * Global.fDistanceFactor ), Drawrange ) + prefetch_margin };
|
||||
auto const distance { glm::length( Instance.location() - Camera ) };
|
||||
if( distance > reach ) {
|
||||
// out of reach the data may be let go of: read again the next time
|
||||
if( Textures && ( distance > reach + prefetch_margin ) ) {
|
||||
Instance.textures_prefetched( false );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if( model->streamed() ) {
|
||||
GfxRenderer->Touch_Bank( model->m_geometrybank, static_cast<float>( distance ) );
|
||||
}
|
||||
if( ( false == Textures ) || Instance.textures_prefetched() ) { return false; }
|
||||
Instance.textures_prefetched( true );
|
||||
GfxRenderer->Prefetch_Textures( model, Instance.Material(), Camera );
|
||||
return true;
|
||||
}
|
||||
|
||||
// the textures of the shapes and the tracks of the cell read ahead (gfx.textures.streaming), as the opengl renderer has them read
|
||||
// (Update_Texture_Prefetch()); asked for each time the cell is gone through, which costs nothing for the data read already
|
||||
void prefetch_scenery( scene::basic_cell const &Cell, glm::dvec3 const &Camera ) {
|
||||
|
||||
for( auto const *list : { &Cell.m_shapesopaque, &Cell.m_shapestranslucent } ) {
|
||||
for( auto const &shape : *list ) {
|
||||
GfxRenderer->Prefetch_Material( shape.data().material, Camera );
|
||||
}
|
||||
}
|
||||
for( auto const *track : Cell.m_paths ) {
|
||||
GfxRenderer->Prefetch_Material( track->m_material1, Camera );
|
||||
GfxRenderer->Prefetch_Material( track->m_material2, Camera );
|
||||
GfxRenderer->Prefetch_Material( track->m_sleeper_skin, Camera );
|
||||
if( track->SwitchExtension != nullptr ) {
|
||||
GfxRenderer->Prefetch_Material( track->SwitchExtension->m_material3, Camera );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// returns: the instances whose textures were asked for
|
||||
std::uint64_t prefetch_instances( scene::basic_cell::instance_sequence const &Instances, glm::dvec3 const &Camera, double const Drawrange, bool const Textures ) {
|
||||
|
||||
std::uint64_t textures { 0 };
|
||||
for( auto *instance : Instances ) {
|
||||
if( ( instance != nullptr ) && prefetch_instance( *instance, Camera, Drawrange, Textures ) ) {
|
||||
++textures;
|
||||
}
|
||||
}
|
||||
return textures;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
model_prefetch &
|
||||
model_prefetch::instance() {
|
||||
|
||||
static model_prefetch prefetch;
|
||||
return prefetch;
|
||||
}
|
||||
|
||||
void
|
||||
model_prefetch::update( glm::dvec3 const &Camera, bool const Whole ) {
|
||||
|
||||
auto *region { simulation::Region };
|
||||
if( region == nullptr ) { return; }
|
||||
auto const start { std::chrono::steady_clock::now() };
|
||||
// as far as the walk draws anything (scene_walk::Render( TAnimModel * )): the draw range and the 250 m it adds
|
||||
auto const drawrange { static_cast<double>( Global.BaseDrawRange ) * Global.fDistanceFactor + 250.0 };
|
||||
auto const range { drawrange + prefetch_margin };
|
||||
region->sections( Camera, static_cast<float>( range ), m_sections );
|
||||
auto const cellcount { m_sections.size() * cells_per_section };
|
||||
if( cellcount == 0 ) { return; }
|
||||
auto const steps { Whole ? cellcount : std::min( cells_per_frame, cellcount ) };
|
||||
auto const textures { GfxRenderer->Prefetches_Textures() };
|
||||
for( std::size_t step = 0; step < steps; ++step, ++m_cursor ) {
|
||||
auto const index { m_cursor % cellcount };
|
||||
auto *section { m_sections[ index / cells_per_section ] };
|
||||
if( section == nullptr ) { continue; }
|
||||
if( textures && ( index % cells_per_section == 0 ) && ( glm::length( section->m_area.center - Camera ) - section->m_area.radius <= range ) ) {
|
||||
// the shapes of the section, the terrain
|
||||
for( auto const &shape : section->m_shapes ) {
|
||||
GfxRenderer->Prefetch_Material( shape.data().material, Camera );
|
||||
}
|
||||
}
|
||||
auto const &cell { section->m_cells[ index % cells_per_section ] };
|
||||
if( ( false == cell.m_active ) || ( glm::length( cell.m_area.center - Camera ) - cell.m_area.radius > range ) ) { continue; }
|
||||
if( textures ) {
|
||||
prefetch_scenery( cell, Camera );
|
||||
}
|
||||
m_timing.textures += prefetch_instances( cell.m_instancesopaque, Camera, drawrange, textures );
|
||||
m_timing.textures += prefetch_instances( cell.m_instancetranslucent, Camera, drawrange, textures );
|
||||
}
|
||||
auto const spent { std::chrono::duration<double, std::milli>( std::chrono::steady_clock::now() - start ).count() };
|
||||
++m_timing.passes;
|
||||
m_timing.total += spent;
|
||||
m_timing.longest = std::max( m_timing.longest, spent );
|
||||
}
|
||||
|
||||
model_prefetch::timing
|
||||
model_prefetch::take_timing() {
|
||||
|
||||
auto const result { m_timing };
|
||||
m_timing = {};
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace gfx
|
||||
Reference in New Issue
Block a user