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"
31 CachedMapBlockData::~CachedMapBlockData()
33 assert(refcount_from_queue == 0);
42 QueuedMeshUpdate::~QueuedMeshUpdate()
51 MeshUpdateQueue::MeshUpdateQueue(Client *client) : m_client(client)
53 m_cache_enable_shaders = g_settings->getBool("enable_shaders");
54 m_cache_use_tangent_vertices =
55 m_cache_enable_shaders &&
56 (g_settings->getBool("enable_bumpmapping") ||
57 g_settings->getBool("enable_parallax_occlusion"));
58 m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
59 m_meshgen_block_cache_size = g_settings->getS32("meshgen_block_cache_size");
62 MeshUpdateQueue::~MeshUpdateQueue()
64 MutexAutoLock lock(m_mutex);
66 for (auto &i : m_cache) {
70 for (QueuedMeshUpdate *q : m_queue) {
75 void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
77 MutexAutoLock lock(m_mutex);
82 Cache the block data (force-update the center block, don't update the
83 neighbors but get them if they aren't already cached)
85 std::vector<CachedMapBlockData *> cached_blocks;
86 size_t cache_hit_counter = 0;
87 cached_blocks.reserve(3 * 3 * 3);
89 for (dp.X = -1; dp.X <= 1; dp.X++)
90 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
91 for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
93 CachedMapBlockData *cached_block;
94 if (dp == v3s16(0, 0, 0))
95 cached_block = cacheBlock(map, p1, FORCE_UPDATE);
97 cached_block = cacheBlock(map, p1,
98 SKIP_UPDATE_IF_ALREADY_CACHED,
100 cached_blocks.push_back(cached_block);
102 g_profiler->avg("MeshUpdateQueue: MapBlocks from cache [%]",
103 100.0f * cache_hit_counter / cached_blocks.size());
106 Mark the block as urgent if requested
112 Find if block is already in queue.
113 If it is, update the data and quit.
115 for (QueuedMeshUpdate *q : m_queue) {
117 // NOTE: We are not adding a new position to the queue, thus
118 // refcount_from_queue stays the same.
119 if (ack_block_to_server)
120 q->ack_block_to_server = true;
121 q->crack_level = m_client->getCrackLevel();
122 q->crack_pos = m_client->getCrackPos();
130 QueuedMeshUpdate *q = new QueuedMeshUpdate;
132 q->ack_block_to_server = ack_block_to_server;
133 q->crack_level = m_client->getCrackLevel();
134 q->crack_pos = m_client->getCrackPos();
135 m_queue.push_back(q);
137 // This queue entry is a new reference to the cached blocks
138 for (CachedMapBlockData *cached_block : cached_blocks) {
139 cached_block->refcount_from_queue++;
143 // Returned pointer must be deleted
144 // Returns NULL if queue is empty
145 QueuedMeshUpdate *MeshUpdateQueue::pop()
147 MutexAutoLock lock(m_mutex);
149 bool must_be_urgent = !m_urgents.empty();
150 for (std::vector<QueuedMeshUpdate *>::iterator i = m_queue.begin();
151 i != m_queue.end(); ++i) {
152 QueuedMeshUpdate *q = *i;
153 if (must_be_urgent && m_urgents.count(q->p) == 0)
156 m_urgents.erase(q->p);
157 fillDataFromMapBlockCache(q);
163 CachedMapBlockData *MeshUpdateQueue::cacheBlock(
164 Map *map, v3s16 p, UpdateMode mode, size_t *cache_hit_counter)
166 CachedMapBlockData *cached_block = nullptr;
167 std::map<v3s16, CachedMapBlockData *>::iterator it = m_cache.find(p);
169 if (it != m_cache.end()) {
170 cached_block = it->second;
172 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
173 if (cache_hit_counter)
174 (*cache_hit_counter)++;
181 cached_block = new CachedMapBlockData();
182 m_cache[p] = cached_block;
185 MapBlock *b = map->getBlockNoCreateNoEx(p);
187 if (!cached_block->data)
188 cached_block->data = new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE *
190 memcpy(cached_block->data, b->getData(),
191 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
194 delete[] cached_block->data;
195 cached_block->data = nullptr;
200 CachedMapBlockData *MeshUpdateQueue::getCachedBlock(const v3s16 &p)
202 std::map<v3s16, CachedMapBlockData *>::iterator it = m_cache.find(p);
203 if (it != m_cache.end()) {
209 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
211 MeshMakeData *data = new MeshMakeData(
212 m_client, m_cache_enable_shaders, m_cache_use_tangent_vertices);
215 data->fillBlockDataBegin(q->p);
217 std::time_t t_now = std::time(0);
219 // Collect data for 3*3*3 blocks from cache
221 for (dp.X = -1; dp.X <= 1; dp.X++)
222 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
223 for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
225 CachedMapBlockData *cached_block = getCachedBlock(p);
227 cached_block->refcount_from_queue--;
228 cached_block->last_used_timestamp = t_now;
229 if (cached_block->data)
231 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 // The cache size is kept roughly below cache_soft_max_size, not letting
247 // anything get older than cache_seconds_max or deleted before 2 seconds.
248 const int cache_seconds_max = 10;
249 const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
250 int cache_seconds = MYMAX(2,
253 (cache_soft_max_size /
258 for (std::map<v3s16, CachedMapBlockData *>::iterator it = m_cache.begin();
259 it != m_cache.end();) {
260 CachedMapBlockData *cached_block = it->second;
261 if (cached_block->refcount_from_queue == 0 &&
262 cached_block->last_used_timestamp <
263 t_now - cache_seconds) {
276 MeshUpdateThread::MeshUpdateThread(Client *client) :
277 UpdateThread("Mesh"), m_queue_in(client)
279 m_generation_interval = g_settings->getU16("mesh_generation_interval");
280 m_generation_interval = rangelim(m_generation_interval, 0, 50);
283 void MeshUpdateThread::updateBlock(
284 Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
286 // Allow the MeshUpdateQueue to do whatever it wants
287 m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
291 void MeshUpdateThread::doUpdate()
294 while ((q = m_queue_in.pop())) {
295 if (m_generation_interval)
296 sleep_ms(m_generation_interval);
297 ScopeProfiler sp(g_profiler, "Client: Mesh making (sum)");
299 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
304 r.ack_block_to_server = q->ack_block_to_server;
306 m_queue_out.push_back(r);