]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.h
Use multiple threads for mesh generation (#13062)
[minetest.git] / src / client / mesh_generator_thread.h
1 /*
2 Minetest
3 Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <ctime>
23 #include <mutex>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include "mapblock_mesh.h"
27 #include "threading/mutex_auto_lock.h"
28 #include "util/thread.h"
29 #include <vector>
30 #include <memory>
31
32 struct CachedMapBlockData
33 {
34         v3s16 p = v3s16(-1337, -1337, -1337);
35         MapNode *data = nullptr; // A copy of the MapBlock's data member
36         int refcount_from_queue = 0;
37         std::time_t last_used_timestamp = std::time(0);
38
39         CachedMapBlockData() = default;
40         ~CachedMapBlockData();
41 };
42
43 struct QueuedMeshUpdate
44 {
45         v3s16 p = v3s16(-1337, -1337, -1337);
46         bool ack_block_to_server = false;
47         int crack_level = -1;
48         v3s16 crack_pos;
49         MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
50         bool urgent = false;
51
52         QueuedMeshUpdate() = default;
53         ~QueuedMeshUpdate();
54 };
55
56 /*
57         A thread-safe queue of mesh update tasks and a cache of MapBlock data
58 */
59 class MeshUpdateQueue
60 {
61         enum UpdateMode
62         {
63                 FORCE_UPDATE,
64                 SKIP_UPDATE_IF_ALREADY_CACHED,
65         };
66
67 public:
68         MeshUpdateQueue(Client *client);
69
70         ~MeshUpdateQueue();
71
72         // Caches the block at p and its neighbors (if needed) and queues a mesh
73         // update for the block at p
74         bool addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
75
76         // Returned pointer must be deleted
77         // Returns NULL if queue is empty
78         QueuedMeshUpdate *pop();
79
80         // Marks a position as finished, unblocking the next update
81         void done(v3s16 pos);
82
83         u32 size()
84         {
85                 MutexAutoLock lock(m_mutex);
86                 return m_queue.size();
87         }
88
89 private:
90         Client *m_client;
91         std::vector<QueuedMeshUpdate *> m_queue;
92         std::unordered_set<v3s16> m_urgents;
93         std::unordered_map<v3s16, CachedMapBlockData *> m_cache;
94         std::unordered_set<v3s16> m_inflight_blocks;
95         u64 m_next_cache_cleanup; // milliseconds
96         std::mutex m_mutex;
97
98         // TODO: Add callback to update these when g_settings changes
99         bool m_cache_enable_shaders;
100         bool m_cache_smooth_lighting;
101         int m_meshgen_block_cache_size;
102
103         CachedMapBlockData *cacheBlock(Map *map, v3s16 p, UpdateMode mode,
104                         size_t *cache_hit_counter = NULL);
105         CachedMapBlockData *getCachedBlock(const v3s16 &p);
106         void fillDataFromMapBlockCache(QueuedMeshUpdate *q);
107         void cleanupCache();
108 };
109
110 struct MeshUpdateResult
111 {
112         v3s16 p = v3s16(-1338, -1338, -1338);
113         MapBlockMesh *mesh = nullptr;
114         bool ack_block_to_server = false;
115         bool urgent = false;
116
117         MeshUpdateResult() = default;
118 };
119
120 class MeshUpdateManager;
121
122 class MeshUpdateWorkerThread : public UpdateThread
123 {
124 public:
125         MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset);
126
127 protected:
128         virtual void doUpdate();
129
130 private:
131         MeshUpdateQueue *m_queue_in;
132         MeshUpdateManager *m_manager;
133         v3s16 *m_camera_offset;
134
135         // TODO: Add callback to update these when g_settings changes
136         int m_generation_interval;
137 };
138
139 class MeshUpdateManager
140 {
141 public:
142         MeshUpdateManager(Client *client);
143
144         // Caches the block at p and its neighbors (if needed) and queues a mesh
145         // update for the block at p
146         void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent,
147                         bool update_neighbors = false);
148         void putResult(const MeshUpdateResult &r);
149         bool getNextResult(MeshUpdateResult &r);
150
151
152         v3s16 m_camera_offset;
153
154         void start();
155         void stop();
156         void wait();
157
158         bool isRunning();
159
160 private:
161         void deferUpdate();
162
163
164         MeshUpdateQueue m_queue_in;
165         MutexedQueue<MeshUpdateResult> m_queue_out;
166         MutexedQueue<MeshUpdateResult> m_queue_out_urgent;
167
168         std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers;
169 };