/* 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/. */ #pragma once // the streaming of the geometry of the scenery models (modelstreaming): a streamed geometry bank is made with its chunks empty, and // its data is read and unpacked by the threads of the streamer when the bank is about to be drawn (or ahead of it, the prefetch), // handed to the bank by the main thread a few at a frame, and let go of once the bank isn't drawn for a while. what reads the vertices // of a bank without its data loads them at once (geometry_bank::load_now()) #include "rendering/geometrybank.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace gfx { class geometry_streamer { public: geometry_streamer() = default; ~geometry_streamer(); geometry_streamer( geometry_streamer const & ) = delete; geometry_streamer &operator=( geometry_streamer const & ) = delete; static auto instance() -> geometry_streamer &; // the clock the banks note their use with, in milliseconds; moved on by update() static auto now() -> std::uint32_t; // a load of the bank asked for, or asked for sooner (the lower the Priority, the sooner). any thread void request( geometry_bank &Bank, float const Priority ); // the main thread, once a frame, while no walk draws: the loads done handed to their banks as far as the budget of the frame goes, // and the banks unused for a while let go of void update(); // the loads done handed over at once, the ones under way waited for, till none is left or Timeout passes (the loading screen). // returns: the loads left auto finish( std::chrono::milliseconds const Timeout ) -> std::size_t; // the loads asked for and not handed over yet auto pending() const -> std::size_t; // a bank which got its data by itself (geometry_bank::load_now()), let go of with the others void register_resident( geometry_bank &Bank ); // the data of every streamed bank which isn't kept for good let go of (the self-test: modelstream evict). the main thread auto evict_all() -> std::size_t; // a load which failed, told in the log by the main thread. any thread void report_failure( std::string_view const Name, std::string_view const Error ); // the threads ended and the loads dropped, before the banks go (the renderer is closed) void stop(); // the loads taken again after stop(), by a renderer made after another one closed (vulkan falling back to opengl as it starts, // before any bank is made); the threads start with the first request void resume(); // how many times banks were given their data. the walks kept by the gpu culling, made while a bank had none, are made anew when // it changes auto adoptions() const -> std::uint64_t { return m_adoptions.load( std::memory_order_acquire ); } struct statistics { std::size_t resident { 0 }; // banks with their data std::size_t bytes { 0 }; // of their data std::size_t pending { 0 }; std::uint64_t loaded { 0 }; // since the start std::uint64_t evicted { 0 }; std::uint64_t failed { 0 }; }; auto stats() const -> statistics; private: // types // a bank asked for: by the priority, then in the order of the requests using queue_key = std::pair; using queue_map = std::map; struct result { geometry_bank *bank { nullptr }; std::uint32_t epoch { 0 }; bank_data data; bool ok { false }; std::string error; }; // methods void start_threads(); void work( std::stop_token const Stop ); // the results handed to their banks, at most Bytes of them (0: all). returns: the bytes handed over auto hand_over( std::size_t const Bytes, std::chrono::steady_clock::duration const Time ) -> std::size_t; void hand_over_one( result &Result ); // a part of the banks with their data gone through, the ones unused for a while let go of void sweep(); void report_failures(); // members mutable std::mutex m_mutex; std::condition_variable_any m_wake; // a request for the threads std::condition_variable m_idle; // a load done queue_map m_queue; std::unordered_map m_queued; std::uint64_t m_order { 0 }; std::size_t m_working { 0 }; // loads under way std::deque m_done; std::vector m_threads; bool m_stopped { false }; std::vector m_resident; // the banks with their data, for the sweep std::size_t m_sweepcursor { 0 }; std::vector m_failures; std::atomic m_adoptions { 0 }; std::chrono::steady_clock::time_point const m_start { std::chrono::steady_clock::now() }; std::atomic m_now { 0 }; // now(), since m_start std::atomic m_loaded { 0 }; std::atomic m_evicted { 0 }; std::atomic m_failed { 0 }; }; } // namespace gfx