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():
34 refcount_from_queue(0),
35 last_used_timestamp(time(0))
39 CachedMapBlockData::~CachedMapBlockData()
41 assert(refcount_from_queue == 0);
51 QueuedMeshUpdate::QueuedMeshUpdate():
53 ack_block_to_server(false),
61 QueuedMeshUpdate::~QueuedMeshUpdate()
71 MeshUpdateQueue::MeshUpdateQueue(Client *client):
74 m_cache_enable_shaders = g_settings->getBool("enable_shaders");
75 m_cache_use_tangent_vertices = m_cache_enable_shaders && (
76 g_settings->getBool("enable_bumpmapping") ||
77 g_settings->getBool("enable_parallax_occlusion"));
78 m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
79 m_meshgen_block_cache_size = g_settings->getS32("meshgen_block_cache_size");
82 MeshUpdateQueue::~MeshUpdateQueue()
84 MutexAutoLock lock(m_mutex);
86 for (std::map<v3s16, CachedMapBlockData *>::iterator i = m_cache.begin();
87 i != m_cache.end(); ++i) {
91 for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
92 i != m_queue.end(); ++i) {
93 QueuedMeshUpdate *q = *i;
98 void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
100 DSTACK(FUNCTION_NAME);
102 MutexAutoLock lock(m_mutex);
107 Cache the block data (force-update the center block, don't update the
108 neighbors but get them if they aren't already cached)
110 std::vector<CachedMapBlockData*> cached_blocks;
111 size_t cache_hit_counter = 0;
112 cached_blocks.reserve(3*3*3);
114 for (dp.X = -1; dp.X <= 1; dp.X++)
115 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
116 for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
118 CachedMapBlockData *cached_block;
119 if (dp == v3s16(0, 0, 0))
120 cached_block = cacheBlock(map, p1, FORCE_UPDATE);
122 cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
124 cached_blocks.push_back(cached_block);
126 g_profiler->avg("MeshUpdateQueue MapBlock cache hit %",
127 100.0f * cache_hit_counter / cached_blocks.size());
130 Mark the block as urgent if requested
136 Find if block is already in queue.
137 If it is, update the data and quit.
139 for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
140 i != m_queue.end(); ++i) {
141 QueuedMeshUpdate *q = *i;
143 // NOTE: We are not adding a new position to the queue, thus
144 // refcount_from_queue stays the same.
145 if(ack_block_to_server)
146 q->ack_block_to_server = true;
147 q->crack_level = m_client->getCrackLevel();
148 q->crack_pos = m_client->getCrackPos();
156 QueuedMeshUpdate *q = new QueuedMeshUpdate;
158 q->ack_block_to_server = ack_block_to_server;
159 q->crack_level = m_client->getCrackLevel();
160 q->crack_pos = m_client->getCrackPos();
161 m_queue.push_back(q);
163 // This queue entry is a new reference to the cached blocks
164 for (size_t i=0; i<cached_blocks.size(); i++) {
165 cached_blocks[i]->refcount_from_queue++;
169 // Returned pointer must be deleted
170 // Returns NULL if queue is empty
171 QueuedMeshUpdate *MeshUpdateQueue::pop()
173 MutexAutoLock lock(m_mutex);
175 bool must_be_urgent = !m_urgents.empty();
176 for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
177 i != m_queue.end(); ++i) {
178 QueuedMeshUpdate *q = *i;
179 if(must_be_urgent && m_urgents.count(q->p) == 0)
182 m_urgents.erase(q->p);
183 fillDataFromMapBlockCache(q);
189 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
190 size_t *cache_hit_counter)
192 std::map<v3s16, CachedMapBlockData*>::iterator it =
194 if (it != m_cache.end()) {
196 CachedMapBlockData *cached_block = it->second;
197 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
198 if (cache_hit_counter)
199 (*cache_hit_counter)++;
202 MapBlock *b = map->getBlockNoCreateNoEx(p);
204 if (cached_block->data == NULL)
206 new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
207 memcpy(cached_block->data, b->getData(),
208 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
210 delete[] cached_block->data;
211 cached_block->data = NULL;
216 CachedMapBlockData *cached_block = new CachedMapBlockData();
217 m_cache[p] = cached_block;
218 MapBlock *b = map->getBlockNoCreateNoEx(p);
221 new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
222 memcpy(cached_block->data, b->getData(),
223 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
229 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
231 std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
232 if (it != m_cache.end()) {
238 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
240 MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
241 m_cache_use_tangent_vertices);
244 data->fillBlockDataBegin(q->p);
248 // Collect data for 3*3*3 blocks from cache
250 for (dp.X = -1; dp.X <= 1; dp.X++)
251 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
252 for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
254 CachedMapBlockData *cached_block = getCachedBlock(p);
256 cached_block->refcount_from_queue--;
257 cached_block->last_used_timestamp = t_now;
258 if (cached_block->data)
259 data->fillBlockData(dp, cached_block->data);
263 data->setCrack(q->crack_level, q->crack_pos);
264 data->setSmoothLighting(m_cache_smooth_lighting);
267 void MeshUpdateQueue::cleanupCache()
269 const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
270 sizeof(MapNode) / 1000;
271 g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
272 mapblock_kB * m_cache.size());
274 // The cache size is kept roughly below cache_soft_max_size, not letting
275 // anything get older than cache_seconds_max or deleted before 2 seconds.
276 const int cache_seconds_max = 10;
277 const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
278 int cache_seconds = MYMAX(2, cache_seconds_max -
279 m_cache.size() / (cache_soft_max_size / cache_seconds_max));
283 for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin();
284 it != m_cache.end(); ) {
285 CachedMapBlockData *cached_block = it->second;
286 if (cached_block->refcount_from_queue == 0 &&
287 cached_block->last_used_timestamp < t_now - cache_seconds) {
300 MeshUpdateThread::MeshUpdateThread(Client *client):
301 UpdateThread("Mesh"),
304 m_generation_interval = g_settings->getU16("mesh_generation_interval");
305 m_generation_interval = rangelim(m_generation_interval, 0, 50);
308 void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
311 // Allow the MeshUpdateQueue to do whatever it wants
312 m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
316 void MeshUpdateThread::doUpdate()
319 while ((q = m_queue_in.pop())) {
320 if (m_generation_interval)
321 sleep_ms(m_generation_interval);
322 ScopeProfiler sp(g_profiler, "Client: Mesh making");
324 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
329 r.ack_block_to_server = q->ack_block_to_server;
331 m_queue_out.push_back(r);