]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.cpp
Use multiple threads for mesh generation (#13062)
[minetest.git] / src / client / mesh_generator_thread.cpp
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 #include "mesh_generator_thread.h"
21 #include "settings.h"
22 #include "profiler.h"
23 #include "client.h"
24 #include "mapblock.h"
25 #include "map.h"
26 #include "util/directiontables.h"
27
28 /*
29         CachedMapBlockData
30 */
31
32 CachedMapBlockData::~CachedMapBlockData()
33 {
34         assert(refcount_from_queue == 0);
35
36         delete[] data;
37 }
38
39 /*
40         QueuedMeshUpdate
41 */
42
43 QueuedMeshUpdate::~QueuedMeshUpdate()
44 {
45         delete data;
46 }
47
48 /*
49         MeshUpdateQueue
50 */
51
52 MeshUpdateQueue::MeshUpdateQueue(Client *client):
53         m_client(client),
54         m_next_cache_cleanup(0)
55 {
56         m_cache_enable_shaders = g_settings->getBool("enable_shaders");
57         m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
58         m_meshgen_block_cache_size = g_settings->getS32("meshgen_block_cache_size");
59 }
60
61 MeshUpdateQueue::~MeshUpdateQueue()
62 {
63         MutexAutoLock lock(m_mutex);
64
65         for (auto &i : m_cache) {
66                 delete i.second;
67         }
68
69         for (QueuedMeshUpdate *q : m_queue) {
70                 delete q;
71         }
72 }
73
74 bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
75 {
76         MutexAutoLock lock(m_mutex);
77
78         cleanupCache();
79
80         /*
81                 Cache the block data (force-update the center block, don't update the
82                 neighbors but get them if they aren't already cached)
83         */
84         std::vector<CachedMapBlockData*> cached_blocks;
85         size_t cache_hit_counter = 0;
86         CachedMapBlockData *cached_block = cacheBlock(map, p, FORCE_UPDATE);
87         if (!cached_block->data)
88                 return false; // nothing to update
89         cached_blocks.reserve(3*3*3);
90         cached_blocks.push_back(cached_block);
91         for (v3s16 dp : g_26dirs)
92                 cached_blocks.push_back(cacheBlock(map, p + dp,
93                                 SKIP_UPDATE_IF_ALREADY_CACHED,
94                                 &cache_hit_counter));
95         g_profiler->avg("MeshUpdateQueue: MapBlocks from cache [%]",
96                         100.0f * cache_hit_counter / cached_blocks.size());
97
98         /*
99                 Mark the block as urgent if requested
100         */
101         if (urgent)
102                 m_urgents.insert(p);
103
104         /*
105                 Find if block is already in queue.
106                 If it is, update the data and quit.
107         */
108         for (QueuedMeshUpdate *q : m_queue) {
109                 if (q->p == p) {
110                         // NOTE: We are not adding a new position to the queue, thus
111                         //       refcount_from_queue stays the same.
112                         if(ack_block_to_server)
113                                 q->ack_block_to_server = true;
114                         q->crack_level = m_client->getCrackLevel();
115                         q->crack_pos = m_client->getCrackPos();
116                         q->urgent |= urgent;
117                         return true;
118                 }
119         }
120
121         /*
122                 Add the block
123         */
124         QueuedMeshUpdate *q = new QueuedMeshUpdate;
125         q->p = p;
126         q->ack_block_to_server = ack_block_to_server;
127         q->crack_level = m_client->getCrackLevel();
128         q->crack_pos = m_client->getCrackPos();
129         q->urgent = urgent;
130         m_queue.push_back(q);
131
132         // This queue entry is a new reference to the cached blocks
133         for (CachedMapBlockData *cached_block : cached_blocks) {
134                 cached_block->refcount_from_queue++;
135         }
136         return true;
137 }
138
139 // Returned pointer must be deleted
140 // Returns NULL if queue is empty
141 QueuedMeshUpdate *MeshUpdateQueue::pop()
142 {
143         MutexAutoLock lock(m_mutex);
144
145         bool must_be_urgent = !m_urgents.empty();
146         for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
147                         i != m_queue.end(); ++i) {
148                 QueuedMeshUpdate *q = *i;
149                 if (must_be_urgent && m_urgents.count(q->p) == 0)
150                         continue;
151                 // Make sure no two threads are processing the same mapblock, as that causes racing conditions
152                 if (m_inflight_blocks.find(q->p) != m_inflight_blocks.end())
153                         continue;
154                 m_queue.erase(i);
155                 m_urgents.erase(q->p);
156                 m_inflight_blocks.insert(q->p);
157                 fillDataFromMapBlockCache(q);
158                 return q;
159         }
160         return NULL;
161 }
162
163 void MeshUpdateQueue::done(v3s16 pos)
164 {
165         MutexAutoLock lock(m_mutex);
166         m_inflight_blocks.erase(pos);
167 }
168
169 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
170                         size_t *cache_hit_counter)
171 {
172         CachedMapBlockData *cached_block = nullptr;
173         auto it = m_cache.find(p);
174
175         if (it != m_cache.end()) {
176                 cached_block = it->second;
177
178                 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
179                         if (cache_hit_counter)
180                                 (*cache_hit_counter)++;
181                         return cached_block;
182                 }
183         }
184
185         if (!cached_block) {
186                 // Not yet in cache
187                 cached_block = new CachedMapBlockData();
188                 m_cache[p] = cached_block;
189         }
190
191         MapBlock *b = map->getBlockNoCreateNoEx(p);
192         if (b) {
193                 if (!cached_block->data)
194                         cached_block->data =
195                                         new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
196                 memcpy(cached_block->data, b->getData(),
197                                 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
198         } else {
199                 delete[] cached_block->data;
200                 cached_block->data = nullptr;
201         }
202         return cached_block;
203 }
204
205 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
206 {
207         auto it = m_cache.find(p);
208         if (it != m_cache.end()) {
209                 return it->second;
210         }
211         return NULL;
212 }
213
214 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
215 {
216         MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders);
217         q->data = data;
218
219         data->fillBlockDataBegin(q->p);
220
221         std::time_t t_now = std::time(0);
222
223         // Collect data for 3*3*3 blocks from cache
224         for (v3s16 dp : g_27dirs) {
225                 v3s16 p = q->p + dp;
226                 CachedMapBlockData *cached_block = getCachedBlock(p);
227                 if (cached_block) {
228                         cached_block->refcount_from_queue--;
229                         cached_block->last_used_timestamp = t_now;
230                         if (cached_block->data)
231                                 data->fillBlockData(dp, cached_block->data);
232                 }
233         }
234
235         data->setCrack(q->crack_level, q->crack_pos);
236         data->setSmoothLighting(m_cache_smooth_lighting);
237 }
238
239 void MeshUpdateQueue::cleanupCache()
240 {
241         const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
242                         sizeof(MapNode) / 1000;
243         g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
244                         mapblock_kB * m_cache.size());
245
246         // Iterating the entire cache can get pretty expensive so don't do it too often
247         {
248                 constexpr int cleanup_interval = 250;
249                 const u64 now = porting::getTimeMs();
250                 if (m_next_cache_cleanup > now)
251                         return;
252                 m_next_cache_cleanup = now + cleanup_interval;
253         }
254
255         // The cache size is kept roughly below cache_soft_max_size, not letting
256         // anything get older than cache_seconds_max or deleted before 2 seconds.
257         const int cache_seconds_max = 10;
258         const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
259         int cache_seconds = MYMAX(2, cache_seconds_max -
260                         m_cache.size() / (cache_soft_max_size / cache_seconds_max));
261
262         int t_now = time(0);
263
264         for (auto it = m_cache.begin(); it != m_cache.end(); ) {
265                 CachedMapBlockData *cached_block = it->second;
266                 if (cached_block->refcount_from_queue == 0 &&
267                                 cached_block->last_used_timestamp < t_now - cache_seconds) {
268                         it = m_cache.erase(it);
269                         delete cached_block;
270                 } else {
271                         ++it;
272                 }
273         }
274 }
275
276 /*
277         MeshUpdateWorkerThread
278 */
279
280 MeshUpdateWorkerThread::MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset) :
281                 UpdateThread("Mesh"), m_queue_in(queue_in), m_manager(manager), m_camera_offset(camera_offset)
282 {
283         m_generation_interval = g_settings->getU16("mesh_generation_interval");
284         m_generation_interval = rangelim(m_generation_interval, 0, 50);
285 }
286
287 void MeshUpdateWorkerThread::doUpdate()
288 {
289         QueuedMeshUpdate *q;
290         while ((q = m_queue_in->pop())) {
291                 if (m_generation_interval)
292                         sleep_ms(m_generation_interval);
293                 ScopeProfiler sp(g_profiler, "Client: Mesh making (sum)");
294
295                 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, *m_camera_offset);
296
297                 
298
299                 MeshUpdateResult r;
300                 r.p = q->p;
301                 r.mesh = mesh_new;
302                 r.ack_block_to_server = q->ack_block_to_server;
303                 r.urgent = q->urgent;
304
305                 m_manager->putResult(r);
306                 m_queue_in->done(q->p);
307                 delete q;
308         }
309 }
310
311 /*
312         MeshUpdateManager
313 */
314
315 MeshUpdateManager::MeshUpdateManager(Client *client):
316         m_queue_in(client)
317 {
318         int number_of_threads = rangelim(g_settings->getS32("mesh_generation_threads"), 0, 8);
319
320         // Automatically use 33% of the system cores for mesh generation, max 4
321         if (number_of_threads == 0)
322                 number_of_threads = MYMIN(4, Thread::getNumberOfProcessors() / 3);
323         
324         // use at least one thread
325         number_of_threads = MYMAX(1, number_of_threads);
326         infostream << "MeshUpdateManager: using " << number_of_threads << " threads" << std::endl;
327
328         for (int i = 0; i < number_of_threads; i++)
329                 m_workers.push_back(std::make_unique<MeshUpdateWorkerThread>(&m_queue_in, this, &m_camera_offset));
330 }
331
332 void MeshUpdateManager::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
333                 bool urgent, bool update_neighbors)
334 {
335         static thread_local const bool many_neighbors =
336                         g_settings->getBool("smooth_lighting")
337                         && !g_settings->getFlag("performance_tradeoffs");
338         if (!m_queue_in.addBlock(map, p, ack_block_to_server, urgent)) {
339                 warningstream << "Update requested for non-existent block at ("
340                                 << p.X << ", " << p.Y << ", " << p.Z << ")" << std::endl;
341                 return;
342         }
343         if (update_neighbors) {
344                 if (many_neighbors) {
345                         for (v3s16 dp : g_26dirs)
346                                 m_queue_in.addBlock(map, p + dp, false, urgent);
347                 } else {
348                         for (v3s16 dp : g_6dirs)
349                                 m_queue_in.addBlock(map, p + dp, false, urgent);
350                 }
351         }
352         deferUpdate();
353 }
354
355 void MeshUpdateManager::putResult(const MeshUpdateResult &result)
356 {
357         if (result.urgent)
358                 m_queue_out_urgent.push_back(result);
359         else
360                 m_queue_out.push_back(result);
361 }
362
363 bool MeshUpdateManager::getNextResult(MeshUpdateResult &r)
364 {
365         if (!m_queue_out_urgent.empty()) {
366                 r = m_queue_out_urgent.pop_frontNoEx();
367                 return true;
368         }
369
370         if (!m_queue_out.empty()) {
371                 r = m_queue_out.pop_frontNoEx();
372                 return true;
373         }
374
375         return false;
376 }
377
378 void MeshUpdateManager::deferUpdate()
379 {
380         for (auto &thread : m_workers)
381                 thread->deferUpdate();
382 }
383
384 void MeshUpdateManager::start()
385 {
386         for (auto &thread: m_workers)
387                 thread->start();
388 }
389
390 void MeshUpdateManager::stop()
391 {
392         for (auto &thread: m_workers)
393                 thread->stop();
394 }
395
396 void MeshUpdateManager::wait()
397 {
398         for (auto &thread: m_workers)
399                 thread->wait();
400 }
401
402 bool MeshUpdateManager::isRunning()
403 {
404         for (auto &thread: m_workers)
405                 if (thread->isRunning())
406                         return true;
407         return false;
408 }