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):
54 m_cache_enable_shaders = g_settings->getBool("enable_shaders");
55 m_cache_use_tangent_vertices = 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, SKIP_UPDATE_IF_ALREADY_CACHED,
99 cached_blocks.push_back(cached_block);
101 g_profiler->avg("MeshUpdateQueue MapBlock cache hit %",
102 100.0f * cache_hit_counter / cached_blocks.size());
105 Mark the block as urgent if requested
111 Find if block is already in queue.
112 If it is, update the data and quit.
114 for (QueuedMeshUpdate *q : m_queue) {
116 // NOTE: We are not adding a new position to the queue, thus
117 // refcount_from_queue stays the same.
118 if(ack_block_to_server)
119 q->ack_block_to_server = true;
120 q->crack_level = m_client->getCrackLevel();
121 q->crack_pos = m_client->getCrackPos();
129 QueuedMeshUpdate *q = new QueuedMeshUpdate;
131 q->ack_block_to_server = ack_block_to_server;
132 q->crack_level = m_client->getCrackLevel();
133 q->crack_pos = m_client->getCrackPos();
134 m_queue.push_back(q);
136 // This queue entry is a new reference to the cached blocks
137 for (CachedMapBlockData *cached_block : cached_blocks) {
138 cached_block->refcount_from_queue++;
142 // Returned pointer must be deleted
143 // Returns NULL if queue is empty
144 QueuedMeshUpdate *MeshUpdateQueue::pop()
146 MutexAutoLock lock(m_mutex);
148 bool must_be_urgent = !m_urgents.empty();
149 for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
150 i != m_queue.end(); ++i) {
151 QueuedMeshUpdate *q = *i;
152 if(must_be_urgent && m_urgents.count(q->p) == 0)
155 m_urgents.erase(q->p);
156 fillDataFromMapBlockCache(q);
162 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
163 size_t *cache_hit_counter)
165 std::map<v3s16, CachedMapBlockData*>::iterator it =
167 if (it != m_cache.end()) {
169 CachedMapBlockData *cached_block = it->second;
170 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
171 if (cache_hit_counter)
172 (*cache_hit_counter)++;
175 MapBlock *b = map->getBlockNoCreateNoEx(p);
177 if (cached_block->data == NULL)
179 new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
180 memcpy(cached_block->data, b->getData(),
181 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
183 delete[] cached_block->data;
184 cached_block->data = NULL;
190 CachedMapBlockData *cached_block = new CachedMapBlockData();
191 m_cache[p] = cached_block;
192 MapBlock *b = map->getBlockNoCreateNoEx(p);
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));
202 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
204 std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
205 if (it != m_cache.end()) {
211 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
213 MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
214 m_cache_use_tangent_vertices);
217 data->fillBlockDataBegin(q->p);
219 std::time_t t_now = std::time(0);
221 // Collect data for 3*3*3 blocks from cache
223 for (dp.X = -1; dp.X <= 1; dp.X++)
224 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
225 for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
227 CachedMapBlockData *cached_block = getCachedBlock(p);
229 cached_block->refcount_from_queue--;
230 cached_block->last_used_timestamp = t_now;
231 if (cached_block->data)
232 data->fillBlockData(dp, cached_block->data);
236 data->setCrack(q->crack_level, q->crack_pos);
237 data->setSmoothLighting(m_cache_smooth_lighting);
240 void MeshUpdateQueue::cleanupCache()
242 const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
243 sizeof(MapNode) / 1000;
244 g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
245 mapblock_kB * m_cache.size());
247 // The cache size is kept roughly below cache_soft_max_size, not letting
248 // anything get older than cache_seconds_max or deleted before 2 seconds.
249 const int cache_seconds_max = 10;
250 const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
251 int cache_seconds = MYMAX(2, cache_seconds_max -
252 m_cache.size() / (cache_soft_max_size / cache_seconds_max));
256 for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin();
257 it != m_cache.end(); ) {
258 CachedMapBlockData *cached_block = it->second;
259 if (cached_block->refcount_from_queue == 0 &&
260 cached_block->last_used_timestamp < t_now - cache_seconds) {
273 MeshUpdateThread::MeshUpdateThread(Client *client):
274 UpdateThread("Mesh"),
277 m_generation_interval = g_settings->getU16("mesh_generation_interval");
278 m_generation_interval = rangelim(m_generation_interval, 0, 50);
281 void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
284 // Allow the MeshUpdateQueue to do whatever it wants
285 m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
289 void MeshUpdateThread::doUpdate()
292 while ((q = m_queue_in.pop())) {
293 if (m_generation_interval)
294 sleep_ms(m_generation_interval);
295 ScopeProfiler sp(g_profiler, "Client: Mesh making");
297 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
302 r.ack_block_to_server = q->ack_block_to_server;
304 m_queue_out.push_back(r);