]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.cpp
345f44eb9f9eb48d93c8b322d438373a6bbf84e6
[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.solid_sides = get_solid_sides(q->data);
303                 r.ack_block_to_server = q->ack_block_to_server;
304                 r.urgent = q->urgent;
305
306                 m_manager->putResult(r);
307                 m_queue_in->done(q->p);
308                 delete q;
309         }
310 }
311
312 /*
313         MeshUpdateManager
314 */
315
316 MeshUpdateManager::MeshUpdateManager(Client *client):
317         m_queue_in(client)
318 {
319         int number_of_threads = rangelim(g_settings->getS32("mesh_generation_threads"), 0, 8);
320
321         // Automatically use 33% of the system cores for mesh generation, max 4
322         if (number_of_threads == 0)
323                 number_of_threads = MYMIN(4, Thread::getNumberOfProcessors() / 3);
324         
325         // use at least one thread
326         number_of_threads = MYMAX(1, number_of_threads);
327         infostream << "MeshUpdateManager: using " << number_of_threads << " threads" << std::endl;
328
329         for (int i = 0; i < number_of_threads; i++)
330                 m_workers.push_back(std::make_unique<MeshUpdateWorkerThread>(&m_queue_in, this, &m_camera_offset));
331 }
332
333 void MeshUpdateManager::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
334                 bool urgent, bool update_neighbors)
335 {
336         static thread_local const bool many_neighbors =
337                         g_settings->getBool("smooth_lighting")
338                         && !g_settings->getFlag("performance_tradeoffs");
339         if (!m_queue_in.addBlock(map, p, ack_block_to_server, urgent)) {
340                 warningstream << "Update requested for non-existent block at ("
341                                 << p.X << ", " << p.Y << ", " << p.Z << ")" << std::endl;
342                 return;
343         }
344         if (update_neighbors) {
345                 if (many_neighbors) {
346                         for (v3s16 dp : g_26dirs)
347                                 m_queue_in.addBlock(map, p + dp, false, urgent);
348                 } else {
349                         for (v3s16 dp : g_6dirs)
350                                 m_queue_in.addBlock(map, p + dp, false, urgent);
351                 }
352         }
353         deferUpdate();
354 }
355
356 void MeshUpdateManager::putResult(const MeshUpdateResult &result)
357 {
358         if (result.urgent)
359                 m_queue_out_urgent.push_back(result);
360         else
361                 m_queue_out.push_back(result);
362 }
363
364 bool MeshUpdateManager::getNextResult(MeshUpdateResult &r)
365 {
366         if (!m_queue_out_urgent.empty()) {
367                 r = m_queue_out_urgent.pop_frontNoEx();
368                 return true;
369         }
370
371         if (!m_queue_out.empty()) {
372                 r = m_queue_out.pop_frontNoEx();
373                 return true;
374         }
375
376         return false;
377 }
378
379 void MeshUpdateManager::deferUpdate()
380 {
381         for (auto &thread : m_workers)
382                 thread->deferUpdate();
383 }
384
385 void MeshUpdateManager::start()
386 {
387         for (auto &thread: m_workers)
388                 thread->start();
389 }
390
391 void MeshUpdateManager::stop()
392 {
393         for (auto &thread: m_workers)
394                 thread->stop();
395 }
396
397 void MeshUpdateManager::wait()
398 {
399         for (auto &thread: m_workers)
400                 thread->wait();
401 }
402
403 bool MeshUpdateManager::isRunning()
404 {
405         for (auto &thread: m_workers)
406                 if (thread->isRunning())
407                         return true;
408         return false;
409 }