diff options
author | x2048 <codeforsmile@gmail.com> | 2022-12-27 18:44:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-27 18:44:18 +0100 |
commit | 89e7f72c929fbeef8ad755bc85db22ae6102787d (patch) | |
tree | e5de1b6586eb18c400582cbb9002da4874876e6b /src/client/mesh_generator_thread.h | |
parent | 03e710160f5c573b74097173b8202f209e5106ad (diff) | |
download | minetest-89e7f72c929fbeef8ad755bc85db22ae6102787d.tar.xz |
Use multiple threads for mesh generation (#13062)
Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'src/client/mesh_generator_thread.h')
-rw-r--r-- | src/client/mesh_generator_thread.h | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/src/client/mesh_generator_thread.h b/src/client/mesh_generator_thread.h index 09400196d..bbb84b74a 100644 --- a/src/client/mesh_generator_thread.h +++ b/src/client/mesh_generator_thread.h @@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapblock_mesh.h" #include "threading/mutex_auto_lock.h" #include "util/thread.h" +#include <vector> +#include <memory> struct CachedMapBlockData { @@ -75,6 +77,9 @@ public: // Returns NULL if queue is empty QueuedMeshUpdate *pop(); + // Marks a position as finished, unblocking the next update + void done(v3s16 pos); + u32 size() { MutexAutoLock lock(m_mutex); @@ -86,6 +91,7 @@ private: std::vector<QueuedMeshUpdate *> m_queue; std::unordered_set<v3s16> m_urgents; std::unordered_map<v3s16, CachedMapBlockData *> m_cache; + std::unordered_set<v3s16> m_inflight_blocks; u64 m_next_cache_cleanup; // milliseconds std::mutex m_mutex; @@ -111,25 +117,53 @@ struct MeshUpdateResult MeshUpdateResult() = default; }; -class MeshUpdateThread : public UpdateThread +class MeshUpdateManager; + +class MeshUpdateWorkerThread : public UpdateThread +{ +public: + MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset); + +protected: + virtual void doUpdate(); + +private: + MeshUpdateQueue *m_queue_in; + MeshUpdateManager *m_manager; + v3s16 *m_camera_offset; + + // TODO: Add callback to update these when g_settings changes + int m_generation_interval; +}; + +class MeshUpdateManager { public: - MeshUpdateThread(Client *client); + MeshUpdateManager(Client *client); // Caches the block at p and its neighbors (if needed) and queues a mesh // update for the block at p void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent, bool update_neighbors = false); + void putResult(const MeshUpdateResult &r); + bool getNextResult(MeshUpdateResult &r); + v3s16 m_camera_offset; - MutexedQueue<MeshUpdateResult> m_queue_out; + + void start(); + void stop(); + void wait(); + + bool isRunning(); private: - MeshUpdateQueue m_queue_in; + void deferUpdate(); - // TODO: Add callback to update these when g_settings changes - int m_generation_interval; -protected: - virtual void doUpdate(); + MeshUpdateQueue m_queue_in; + MutexedQueue<MeshUpdateResult> m_queue_out; + MutexedQueue<MeshUpdateResult> m_queue_out_urgent; + + std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers; }; |