Tooltips: Unify the tooltip[] and list[] description tooltip display functions (...
[oweals/minetest.git] / src / mesh_generator_thread.cpp
1 /*
2 Minetest
3 Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "mesh_generator_thread.h"
21 #include "settings.h"
22 #include "profiler.h"
23 #include "client.h"
24 #include "mapblock.h"
25 #include "map.h"
26
27 /*
28         CachedMapBlockData
29 */
30
31 CachedMapBlockData::CachedMapBlockData():
32         p(-1337,-1337,-1337),
33         data(NULL),
34         refcount_from_queue(0),
35         last_used_timestamp(time(0))
36 {
37 }
38
39 CachedMapBlockData::~CachedMapBlockData()
40 {
41         assert(refcount_from_queue == 0);
42
43         if (data)
44                 delete[] data;
45 }
46
47 /*
48         QueuedMeshUpdate
49 */
50
51 QueuedMeshUpdate::QueuedMeshUpdate():
52         p(-1337,-1337,-1337),
53         ack_block_to_server(false),
54         urgent(false),
55         crack_level(-1),
56         crack_pos(0,0,0),
57         data(NULL)
58 {
59 }
60
61 QueuedMeshUpdate::~QueuedMeshUpdate()
62 {
63         if (data)
64                 delete data;
65 }
66
67 /*
68         MeshUpdateQueue
69 */
70
71 MeshUpdateQueue::MeshUpdateQueue(Client *client):
72         m_client(client)
73 {
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");
80 }
81
82 MeshUpdateQueue::~MeshUpdateQueue()
83 {
84         MutexAutoLock lock(m_mutex);
85
86         for (std::map<v3s16, CachedMapBlockData *>::iterator i = m_cache.begin();
87                         i != m_cache.end(); ++i) {
88                 delete i->second;
89         }
90
91         for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
92                         i != m_queue.end(); ++i) {
93                 QueuedMeshUpdate *q = *i;
94                 delete q;
95         }
96 }
97
98 void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
99 {
100         DSTACK(FUNCTION_NAME);
101
102         MutexAutoLock lock(m_mutex);
103
104         cleanupCache();
105
106         /*
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)
109         */
110         std::vector<CachedMapBlockData*> cached_blocks;
111         size_t cache_hit_counter = 0;
112         cached_blocks.reserve(3*3*3);
113         v3s16 dp;
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++) {
117                 v3s16 p1 = p + dp;
118                 CachedMapBlockData *cached_block;
119                 if (dp == v3s16(0, 0, 0))
120                         cached_block = cacheBlock(map, p1, FORCE_UPDATE);
121                 else
122                         cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
123                                         &cache_hit_counter);
124                 cached_blocks.push_back(cached_block);
125         }
126         g_profiler->avg("MeshUpdateQueue MapBlock cache hit %",
127                         100.0f * cache_hit_counter / cached_blocks.size());
128
129         /*
130                 Mark the block as urgent if requested
131         */
132         if (urgent)
133                 m_urgents.insert(p);
134
135         /*
136                 Find if block is already in queue.
137                 If it is, update the data and quit.
138         */
139         for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
140                         i != m_queue.end(); ++i) {
141                 QueuedMeshUpdate *q = *i;
142                 if (q->p == p) {
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();
149                         return;
150                 }
151         }
152
153         /*
154                 Add the block
155         */
156         QueuedMeshUpdate *q = new QueuedMeshUpdate;
157         q->p = p;
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);
162
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++;
166         }
167 }
168
169 // Returned pointer must be deleted
170 // Returns NULL if queue is empty
171 QueuedMeshUpdate *MeshUpdateQueue::pop()
172 {
173         MutexAutoLock lock(m_mutex);
174
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)
180                         continue;
181                 m_queue.erase(i);
182                 m_urgents.erase(q->p);
183                 fillDataFromMapBlockCache(q);
184                 return q;
185         }
186         return NULL;
187 }
188
189 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
190                         size_t *cache_hit_counter)
191 {
192         std::map<v3s16, CachedMapBlockData*>::iterator it =
193                         m_cache.find(p);
194         if (it != m_cache.end()) {
195                 // Already in cache
196                 CachedMapBlockData *cached_block = it->second;
197                 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
198                         if (cache_hit_counter)
199                                 (*cache_hit_counter)++;
200                         return cached_block;
201                 }
202                 MapBlock *b = map->getBlockNoCreateNoEx(p);
203                 if (b) {
204                         if (cached_block->data == NULL)
205                                 cached_block->data =
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));
209                 } else {
210                         delete[] cached_block->data;
211                         cached_block->data = NULL;
212                 }
213                 return cached_block;
214         } else {
215                 // Not yet in cache
216                 CachedMapBlockData *cached_block = new CachedMapBlockData();
217                 m_cache[p] = cached_block;
218                 MapBlock *b = map->getBlockNoCreateNoEx(p);
219                 if (b) {
220                         cached_block->data =
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));
224                 }
225                 return cached_block;
226         }
227 }
228
229 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
230 {
231         std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
232         if (it != m_cache.end()) {
233                 return it->second;
234         }
235         return NULL;
236 }
237
238 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
239 {
240         MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
241                         m_cache_use_tangent_vertices);
242         q->data = data;
243
244         data->fillBlockDataBegin(q->p);
245
246         int t_now = time(0);
247
248         // Collect data for 3*3*3 blocks from cache
249         v3s16 dp;
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++) {
253                 v3s16 p = q->p + dp;
254                 CachedMapBlockData *cached_block = getCachedBlock(p);
255                 if (cached_block) {
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);
260                 }
261         }
262
263         data->setCrack(q->crack_level, q->crack_pos);
264         data->setSmoothLighting(m_cache_smooth_lighting);
265 }
266
267 void MeshUpdateQueue::cleanupCache()
268 {
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());
273
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));
280
281         int t_now = time(0);
282
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) {
288                         m_cache.erase(it++);
289                         delete cached_block;
290                 } else {
291                         ++it;
292                 }
293         }
294 }
295
296 /*
297         MeshUpdateThread
298 */
299
300 MeshUpdateThread::MeshUpdateThread(Client *client):
301         UpdateThread("Mesh"),
302         m_queue_in(client)
303 {
304         m_generation_interval = g_settings->getU16("mesh_generation_interval");
305         m_generation_interval = rangelim(m_generation_interval, 0, 50);
306 }
307
308 void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
309                 bool urgent)
310 {
311         // Allow the MeshUpdateQueue to do whatever it wants
312         m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
313         deferUpdate();
314 }
315
316 void MeshUpdateThread::doUpdate()
317 {
318         QueuedMeshUpdate *q;
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");
323
324                 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
325
326                 MeshUpdateResult r;
327                 r.p = q->p;
328                 r.mesh = mesh_new;
329                 r.ack_block_to_server = q->ack_block_to_server;
330
331                 m_queue_out.push_back(r);
332
333                 delete q;
334         }
335 }