diff --git a/CMakeLists.txt b/CMakeLists.txt index eb71a765d..614958cb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -400,6 +400,7 @@ set(SOURCES "utilities/headtrack.cpp" "utilities/hardwareprobe.cpp" "rendering/geometrybank.cpp" +"rendering/geometrystreamer.cpp" "simulation/simulationsounds.cpp" "rendering/openglcamera.cpp" @@ -433,6 +434,8 @@ set(SOURCES "rendering/common/scenewalk.cpp" "rendering/common/scenewalkindirect.cpp" "rendering/common/walksync.cpp" +"rendering/common/modelprefetch.cpp" +"rendering/common/memoryreport.cpp" "rendering/common/instancecull.cpp" "rendering/common/shadowfit.cpp" "rendering/common/localshadows.cpp" @@ -871,6 +874,7 @@ set(CORE_SOURCES "model/eu07m/eu07m_dump.cpp" "model/eu07m/eu07m_store.cpp" "model/eu07m/eu07m_pack.cpp" +"model/eu07m/eu07m_geometry.cpp" "model/skinning.cpp" "model/clip.cpp" "${DEPS_DIR}/zstd/zstd.c" diff --git a/application/application.cpp b/application/application.cpp index fa74c3d7d..109f31da3 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -23,6 +23,8 @@ http://mozilla.org/MPL/2.0/. #include "utilities/dictionary.h" #include "scene/sceneeditor.h" #include "rendering/renderer.h" +#include "rendering/geometrystreamer.h" +#include "rendering/common/modelprefetch.h" #include "application/uilayer.h" #include "utilities/Logs.h" #include "utilities/logfiles.h" @@ -77,6 +79,18 @@ ui_layer uilayerstaticinitializer; namespace { +// gfx.textures.streaming not set in eu07.ini: the textures are streamed with the vulkan renderer, which was measured with it, and +// read whole with opengl as before +void default_texture_streaming() +{ + if (Global.gfx_texture_streaming_set) + return; + Global.gfx_texture_streaming = (Global.GfxRenderer == "vulkan"); + // the settings were logged before the renderer was chosen + if (Global.gfx_texture_streaming) + WriteLog("gfx.textures.streaming yes: the default of the vulkan renderer"); +} + #ifdef _WIN32 // windows wakes a sleeping thread of a process which didn't ask for a finer timer in steps of up to ~15.6 ms. // the wait of fpslimit sleeps, so a 10 ms frame slot of fpslimit 100 ended ~15.6 ms later, the pacer made it up @@ -836,6 +850,15 @@ int eu07_application::run() EditorTerrain.update(Global.pCamera.Pos); Timer::subsystem.loop_terrain.stop(); + // the geometry of the streamed scenery models (modelstreaming): asked for around the camera ahead of its drawing, handed to the + // banks it was read for, and let go of where it's unused + if ((Global.modelstreaming && GfxRenderer->Streams_Geometry()) || GfxRenderer->Prefetches_Textures()) + { + if (simulation::is_ready) + gfx::model_prefetch::instance().update(Global.pCamera.Pos, false); + gfx::geometry_streamer::instance().update(); + } + Timer::subsystem.loop_tasks.start(); m_taskqueue.update(); m_screenqueue.update(); @@ -1604,6 +1627,7 @@ void eu07_application::select_gfx_backend() { m_gfxbackend = factory->backend("modern"); } + default_texture_streaming(); crashreport_add_info("gfxrenderer", Global.GfxRenderer); WriteLog("renderer: " + (m_gfxbackend != nullptr ? m_gfxbackend->name : std::string("none"))); @@ -1686,6 +1710,7 @@ int eu07_application::fallback_to_opengl(std::string const &Reason) crashreport_add_info("gfxrenderer_fallback", Reason); Global.GfxRenderer = "default"; m_gfxbackend = gfx_renderer_factory::get_instance()->backend("modern"); + default_texture_streaming(); crashreport_add_info("gfxrenderer", Global.GfxRenderer); WriteLog("renderer: " + (m_gfxbackend != nullptr ? m_gfxbackend->name : std::string("none"))); @@ -1748,6 +1773,8 @@ int eu07_application::init_gfx() // the reason is in the log already GfxRenderer->Shutdown(); GfxRenderer.reset(); + // the shutdown of the vulkan renderer stopped the streaming of the geometry; opengl streams it too + gfx::geometry_streamer::instance().resume(); if (fallback_to_opengl("the renderer failed to start") != 0) { return -1; diff --git a/application/editormodeselftestscene.cpp b/application/editormodeselftestscene.cpp index 2f1c2554c..bf1a08e5c 100644 --- a/application/editormodeselftestscene.cpp +++ b/application/editormodeselftestscene.cpp @@ -28,12 +28,15 @@ http://mozilla.org/MPL/2.0/. #include "model/AnimModel.h" #include "model/Model3d.h" #include "rendering/renderer.h" +#include "rendering/geometrystreamer.h" +#include "rendering/common/modelprefetch.h" #include "world/Sweep.h" #include "input/keyboardinput.h" #include "launcher/textures_scanner.h" #include #include #include +#include #include "editor/editorFormat.hpp" #include "editor/editorAlignmentStore.hpp" #include "editor/editorSwitchStore.hpp" @@ -708,6 +711,34 @@ editor_mode::selftest_result editor_mode::selftest_scene(std::string const &comm WriteLog(format("SELFTEST clip %s %s: %s", node.c_str(), name.c_str(), started ? "playing" : "no such clip")); return selftest_result::handled; } + if (command == "memory") + { + // what the process and its parts keep in memory (rendering/common/memoryreport.h), a line of the log for each part + std::string label; + words >> label; + for (auto const &line : GfxRenderer->Memory_Report()) + WriteLog(std::format("SELFTEST memory {}: {}", label, line)); + return selftest_result::handled; + } + if (command == "modelstream") + { + // the streaming of the geometry (modelstreaming): evict lets go of the geometry of every streamed model which isn't kept for + // good, as if they were unused for long, wait hands over the loads asked for, stats tells what the streamer holds + std::string action; + words >> action; + auto &streamer{gfx::geometry_streamer::instance()}; + if (action == "evict") + WriteLog(std::format("SELFTEST modelstream evict: {} banks let go of", streamer.evict_all())); + else if (action == "wait") + WriteLog(std::format("SELFTEST modelstream wait: {} loads left", streamer.finish(std::chrono::seconds{30}))); + auto const prefetch{gfx::model_prefetch::instance().take_timing()}; + WriteLog(std::format("SELFTEST modelstream {}: prefetch {} passes, {:.1f} ms in all, the longest {:.2f} ms, textures asked for {} instances", action, prefetch.passes, + prefetch.total, prefetch.longest, prefetch.textures)); + auto const stats{streamer.stats()}; + WriteLog(std::format("SELFTEST modelstream {}: {} banks with {} bytes, {} pending, {} loaded, {} let go of, {} failed", action, stats.resident, stats.bytes, stats.pending, stats.loaded, + stats.evicted, stats.failed)); + return selftest_result::handled; + } if (command == "animate") { std::string node; diff --git a/application/scenarioloadermode.cpp b/application/scenarioloadermode.cpp index 85f81a337..ce986743a 100644 --- a/application/scenarioloadermode.cpp +++ b/application/scenarioloadermode.cpp @@ -18,6 +18,9 @@ http://mozilla.org/MPL/2.0/. #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" @@ -48,6 +51,9 @@ bool scenarioloader_mode::update() { m_userinterface->set_progress( static_cast( done ) * 100.f / static_cast( total ), 0.f ); return true; } + if( false == geometry_ready() ) { + return true; + } m_closing = 0; Application.pop_mode(); Application.push_mode( eu07_application::mode::driver ); @@ -122,6 +128,43 @@ bool scenarioloader_mode::update() { 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 { Global.modelstreaming && 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 = streamer.pending(); + } + // the geometry handed to the banks, the data of the textures read by their workers, then the textures made on the gpu + auto left { streamer.finish( std::chrono::milliseconds { 50 } ) + 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::steady_clock::now() - m_geometrystart ).count() ) ); + return true; + } + auto const done { m_geometrytotal - std::min( left, m_geometrytotal ) }; + m_userinterface->set_progress( static_cast( done ) * 100.f / static_cast( std::max( m_geometrytotal, 1 ) ), 0.f ); + return false; +} + bool scenarioloader_mode::is_command_processor() const { return false; } diff --git a/application/scenarioloadermode.h b/application/scenarioloadermode.h index 41ae75f68..ce09e2c64 100644 --- a/application/scenarioloadermode.h +++ b/application/scenarioloadermode.h @@ -17,6 +17,13 @@ class scenarioloader_mode : public application_mode { std::chrono::system_clock::time_point timestart; // the scenery is loaded: the loading screen kept while the renderer prepares the programs (gfx_renderer::Prewarm_Shaders()) int m_closing{0}; + // the geometry of the streamed models around the start asked for, when, and how many loads it took (modelstreaming) + bool m_geometryasked{false}; + std::chrono::steady_clock::time_point m_geometrystart; + std::size_t m_geometrytotal{0}; + + // the geometry of the streamed models around the start loaded before the simulation starts. returns: whether it's done + bool geometry_ready(); public: // constructors diff --git a/docs/eu07m-format.md b/docs/eu07m-format.md index e281e46bb..661bc87b1 100644 --- a/docs/eu07m-format.md +++ b/docs/eu07m-format.md @@ -459,6 +459,46 @@ own. It applies to EU07M and E3D models alike. - Measured (Vulkan, 200 heads `s4ipcpb_dd`): with `gfx.gpucull 0` the frame 9.2 ms → 1.5–2.5 ms and 2400 → 16 draw calls, with `gfx.gpucull 1` 9.1 ms → 3.0 ms and 2410 → 26 draw calls. +### Streaming of the geometry + +With `modelstreaming yes` in `eu07.ini` (the default; `no` turns it off), and `modelformat ng3d`, the vertex and index streams of the larger scenery +models are read when the models are about to be drawn and let go of when they're unused for a while. The table of sections makes it +possible: the loading of the scenery reads only the structure of such a model (everything but `VPOS VATT VSKN VSKW VUSR VBND IDX2 +IDX4`, one read of the beginning of the file), and the streams are read later from the same file, with the blocks of the store +(`GREF`). The format doesn't change for it. + +- **Which models.** A scenery model (`node … model`) of an `.ng3d` file of at least `modelstreaming.minsize` KiB (default 256), + without skins, stars and texts of E3D, with `BNDS`. The size of the file decides, so a smaller model is read once, whole, as + without the streaming. Vehicles, cabs, loads and every other model are loaded whole, as + before. A streamed model asked for by anything else than the scenery (a vehicle of the same file) has its geometry loaded and kept. +- **What stays.** The nodes, the materials, the components, the clips and the channels are loaded with the model and stay: the + animations, the lights, the `.mmd`, `GetFromName` and the events work on a model without its geometry. The geometry bank of the + model keeps its chunks, empty (`gfx::residency` in `rendering/geometrybank.h`). +- **Loading.** A bank without its data draws nothing and is asked of the streamer (`rendering/geometrystreamer.h`): its threads read + and unpack the streams, nearest first, and the main thread hands them to the banks, at most 64 MB or 3 ms in a frame. The cells + around the camera are gone through ahead of the drawing (`rendering/common/modelprefetch.h`, the draw range and 500 m), and the + loading screen waits, for two minutes at most, for the models around the start, the data of their textures and, in Vulkan, the + images of those textures made on the gpu. +- **Animations and events.** The walk of the scene goes through an instance without its geometry as through any other: its + animations, lights, clips and channels are worked out (`RaAnimate()`, `RaPrepare()`), only its draws are left out. The animations + with an event at their end (`evDone`) go on also when the model isn't drawn at all, as before. So the timing of the animations and + the events is the same as without the streaming. +- **Letting go.** A bank not drawn nor asked for within the prefetch for `modelstreaming.evictafter` seconds (default 30) lets go of + its data in memory and on the GPU. Above `modelstreaming.budget` MB (default 0, no limit) the banks unused for 2 seconds go sooner. +- **Reading the vertices.** What reads the vertices of a model (the picking, the editor, the occlusion of the sky) gets them: a bank + without its data loads it at once, on the thread which asks. +- **Bounds.** The radius of a streamed model and the bounds of its branches (the texture variants) come from `BNDS`, the corners of + the bounds of its nodes taken through their transforms, not from the vertices. The radius is so the true bound of the model around + its origin, which may differ a little from the one the vertices give. +- **Textures.** With `gfx.textures.streaming yes` (now the default; `no` keeps the copies of the data of the textures in memory, as + before) the same pass through the cells has the data of the textures of every instance in + reach read ahead by the workers of the textures (Vulkan; the OpenGL renderer has its own pass, `Update_Texture_Prefetch()`), and the + data is let go of after the upload, as the streaming of the textures does. The data read ahead and not drawn for 5 minutes, or + read for a place the camera went far from, is let go of too. +- The self-test commands `modelstream evict` (the geometry of every streamed model let go of), `modelstream wait` and + `modelstream stats`, and `memory