3 Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "mesh_generator_thread.h"
26 #include "util/directiontables.h"
32 CachedMapBlockData::~CachedMapBlockData()
34 assert(refcount_from_queue == 0);
43 QueuedMeshUpdate::~QueuedMeshUpdate()
52 MeshUpdateQueue::MeshUpdateQueue(Client *client):
54 m_next_cache_cleanup(0)
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");
61 MeshUpdateQueue::~MeshUpdateQueue()
63 MutexAutoLock lock(m_mutex);
65 for (auto &i : m_cache) {
69 for (QueuedMeshUpdate *q : m_queue) {
74 bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
76 MutexAutoLock lock(m_mutex);
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)
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,
95 g_profiler->avg("MeshUpdateQueue: MapBlocks from cache [%]",
96 100.0f * cache_hit_counter / cached_blocks.size());
99 Mark the block as urgent if requested
105 Find if block is already in queue.
106 If it is, update the data and quit.
108 for (QueuedMeshUpdate *q : m_queue) {
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();
124 QueuedMeshUpdate *q = new QueuedMeshUpdate;
126 q->ack_block_to_server = ack_block_to_server;
127 q->crack_level = m_client->getCrackLevel();
128 q->crack_pos = m_client->getCrackPos();
130 m_queue.push_back(q);
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++;
139 // Returned pointer must be deleted
140 // Returns NULL if queue is empty
141 QueuedMeshUpdate *MeshUpdateQueue::pop()
143 MutexAutoLock lock(m_mutex);
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)
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())
155 m_urgents.erase(q->p);
156 m_inflight_blocks.insert(q->p);
157 fillDataFromMapBlockCache(q);
163 void MeshUpdateQueue::done(v3s16 pos)
165 MutexAutoLock lock(m_mutex);
166 m_inflight_blocks.erase(pos);
169 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
170 size_t *cache_hit_counter)
172 CachedMapBlockData *cached_block = nullptr;
173 auto it = m_cache.find(p);
175 if (it != m_cache.end()) {
176 cached_block = it->second;
178 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
179 if (cache_hit_counter)
180 (*cache_hit_counter)++;
187 cached_block = new CachedMapBlockData();
188 m_cache[p] = cached_block;
191 MapBlock *b = map->getBlockNoCreateNoEx(p);
193 if (!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));
199 delete[] cached_block->data;
200 cached_block->data = nullptr;
205 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
207 auto it = m_cache.find(p);
208 if (it != m_cache.end()) {
214 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
216 MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders);
219 data->fillBlockDataBegin(q->p);
221 std::time_t t_now = std::time(0);
223 // Collect data for 3*3*3 blocks from cache
224 for (v3s16 dp : g_27dirs) {
226 CachedMapBlockData *cached_block = getCachedBlock(p);
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);
235 data->setCrack(q->crack_level, q->crack_pos);
236 data->setSmoothLighting(m_cache_smooth_lighting);
239 void MeshUpdateQueue::cleanupCache()
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());
246 // Iterating the entire cache can get pretty expensive so don't do it too often
248 constexpr int cleanup_interval = 250;
249 const u64 now = porting::getTimeMs();
250 if (m_next_cache_cleanup > now)
252 m_next_cache_cleanup = now + cleanup_interval;
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));
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);
277 MeshUpdateWorkerThread
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)
283 m_generation_interval = g_settings->getU16("mesh_generation_interval");
284 m_generation_interval = rangelim(m_generation_interval, 0, 50);
287 void MeshUpdateWorkerThread::doUpdate()
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)");
295 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, *m_camera_offset);
302 r.ack_block_to_server = q->ack_block_to_server;
303 r.urgent = q->urgent;
305 m_manager->putResult(r);
306 m_queue_in->done(q->p);
315 MeshUpdateManager::MeshUpdateManager(Client *client):
318 int number_of_threads = rangelim(g_settings->getS32("mesh_generation_threads"), 0, 8);
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);
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;
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));
332 void MeshUpdateManager::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
333 bool urgent, bool update_neighbors)
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;
343 if (update_neighbors) {
344 if (many_neighbors) {
345 for (v3s16 dp : g_26dirs)
346 m_queue_in.addBlock(map, p + dp, false, urgent);
348 for (v3s16 dp : g_6dirs)
349 m_queue_in.addBlock(map, p + dp, false, urgent);
355 void MeshUpdateManager::putResult(const MeshUpdateResult &result)
358 m_queue_out_urgent.push_back(result);
360 m_queue_out.push_back(result);
363 bool MeshUpdateManager::getNextResult(MeshUpdateResult &r)
365 if (!m_queue_out_urgent.empty()) {
366 r = m_queue_out_urgent.pop_frontNoEx();
370 if (!m_queue_out.empty()) {
371 r = m_queue_out.pop_frontNoEx();
378 void MeshUpdateManager::deferUpdate()
380 for (auto &thread : m_workers)
381 thread->deferUpdate();
384 void MeshUpdateManager::start()
386 for (auto &thread: m_workers)
390 void MeshUpdateManager::stop()
392 for (auto &thread: m_workers)
396 void MeshUpdateManager::wait()
398 for (auto &thread: m_workers)
402 bool MeshUpdateManager::isRunning()
404 for (auto &thread: m_workers)
405 if (thread->isRunning())