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::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
87 i != m_queue.end(); ++i) {
88 QueuedMeshUpdate *q = *i;
93 void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
95 DSTACK(FUNCTION_NAME);
97 MutexAutoLock lock(m_mutex);
102 Cache the block data (force-update the center block, don't update the
103 neighbors but get them if they aren't already cached)
105 std::vector<CachedMapBlockData*> cached_blocks;
106 size_t cache_hit_counter = 0;
107 cached_blocks.reserve(3*3*3);
109 for (dp.X = -1; dp.X <= 1; dp.X++)
110 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
111 for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
113 CachedMapBlockData *cached_block;
114 if (dp == v3s16(0, 0, 0))
115 cached_block = cacheBlock(map, p1, FORCE_UPDATE);
117 cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
119 cached_blocks.push_back(cached_block);
121 g_profiler->avg("MeshUpdateQueue MapBlock cache hit %",
122 100.0f * cache_hit_counter / cached_blocks.size());
125 Mark the block as urgent if requested
131 Find if block is already in queue.
132 If it is, update the data and quit.
134 for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
135 i != m_queue.end(); ++i) {
136 QueuedMeshUpdate *q = *i;
138 // NOTE: We are not adding a new position to the queue, thus
139 // refcount_from_queue stays the same.
140 if(ack_block_to_server)
141 q->ack_block_to_server = true;
142 q->crack_level = m_client->getCrackLevel();
143 q->crack_pos = m_client->getCrackPos();
151 QueuedMeshUpdate *q = new QueuedMeshUpdate;
153 q->ack_block_to_server = ack_block_to_server;
154 q->crack_level = m_client->getCrackLevel();
155 q->crack_pos = m_client->getCrackPos();
156 m_queue.push_back(q);
158 // This queue entry is a new reference to the cached blocks
159 for (size_t i=0; i<cached_blocks.size(); i++) {
160 cached_blocks[i]->refcount_from_queue++;
164 // Returned pointer must be deleted
165 // Returns NULL if queue is empty
166 QueuedMeshUpdate *MeshUpdateQueue::pop()
168 MutexAutoLock lock(m_mutex);
170 bool must_be_urgent = !m_urgents.empty();
171 for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
172 i != m_queue.end(); ++i) {
173 QueuedMeshUpdate *q = *i;
174 if(must_be_urgent && m_urgents.count(q->p) == 0)
177 m_urgents.erase(q->p);
178 fillDataFromMapBlockCache(q);
184 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
185 size_t *cache_hit_counter)
187 std::map<v3s16, CachedMapBlockData*>::iterator it =
189 if (it != m_cache.end()) {
191 CachedMapBlockData *cached_block = it->second;
192 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
193 if (cache_hit_counter)
194 (*cache_hit_counter)++;
197 MapBlock *b = map->getBlockNoCreateNoEx(p);
199 if (cached_block->data == NULL)
201 new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
202 memcpy(cached_block->data, b->getData(),
203 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
205 delete[] cached_block->data;
206 cached_block->data = NULL;
211 CachedMapBlockData *cached_block = new CachedMapBlockData();
212 m_cache[p] = cached_block;
213 MapBlock *b = map->getBlockNoCreateNoEx(p);
216 new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
217 memcpy(cached_block->data, b->getData(),
218 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
224 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
226 std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
227 if (it != m_cache.end()) {
233 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
235 MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
236 m_cache_use_tangent_vertices);
239 data->fillBlockDataBegin(q->p);
243 // Collect data for 3*3*3 blocks from cache
245 for (dp.X = -1; dp.X <= 1; dp.X++)
246 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
247 for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
249 CachedMapBlockData *cached_block = getCachedBlock(p);
251 cached_block->refcount_from_queue--;
252 cached_block->last_used_timestamp = t_now;
253 if (cached_block->data)
254 data->fillBlockData(dp, cached_block->data);
258 data->setCrack(q->crack_level, q->crack_pos);
259 data->setSmoothLighting(m_cache_smooth_lighting);
262 void MeshUpdateQueue::cleanupCache()
264 const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
265 sizeof(MapNode) / 1000;
266 g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
267 mapblock_kB * m_cache.size());
269 // The cache size is kept roughly below cache_soft_max_size, not letting
270 // anything get older than cache_seconds_max or deleted before 2 seconds.
271 const int cache_seconds_max = 10;
272 const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
273 int cache_seconds = MYMAX(2, cache_seconds_max -
274 m_cache.size() / (cache_soft_max_size / cache_seconds_max));
278 for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin();
279 it != m_cache.end(); ) {
280 CachedMapBlockData *cached_block = it->second;
281 if (cached_block->refcount_from_queue == 0 &&
282 cached_block->last_used_timestamp < t_now - cache_seconds) {
294 MeshUpdateThread::MeshUpdateThread(Client *client):
295 UpdateThread("Mesh"),
298 m_generation_interval = g_settings->getU16("mesh_generation_interval");
299 m_generation_interval = rangelim(m_generation_interval, 0, 50);
302 void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
305 // Allow the MeshUpdateQueue to do whatever it wants
306 m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
310 void MeshUpdateThread::doUpdate()
313 while ((q = m_queue_in.pop())) {
314 if (m_generation_interval)
315 sleep_ms(m_generation_interval);
316 ScopeProfiler sp(g_profiler, "Client: Mesh making");
318 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
323 r.ack_block_to_server = q->ack_block_to_server;
325 m_queue_out.push_back(r);