/* 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 #include #include 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( 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( Global.BaseDrawRange ) * Global.fDistanceFactor + 250.0 }; auto const range { drawrange + prefetch_margin }; region->sections( Camera, static_cast( 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( 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