]> git.lizzy.rs Git - minetest.git/blob - src/mesh_generator_thread.cpp
MeshUpdateQueue: Add a MapBlock cache that minimizes the amount of MapBlock copying...
[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::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
87                         i != m_queue.end(); ++i) {
88                 QueuedMeshUpdate *q = *i;
89                 delete q;
90         }
91 }
92
93 void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
94 {
95         DSTACK(FUNCTION_NAME);
96
97         MutexAutoLock lock(m_mutex);
98
99         cleanupCache();
100
101         /*
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)
104         */
105         std::vector<CachedMapBlockData*> cached_blocks;
106         size_t cache_hit_counter = 0;
107         cached_blocks.reserve(3*3*3);
108         v3s16 dp;
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++) {
112                 v3s16 p1 = p + dp;
113                 CachedMapBlockData *cached_block;
114                 if (dp == v3s16(0, 0, 0))
115                         cached_block = cacheBlock(map, p1, FORCE_UPDATE);
116                 else
117                         cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
118                                         &cache_hit_counter);
119                 cached_blocks.push_back(cached_block);
120         }
121         g_profiler->avg("MeshUpdateQueue MapBlock cache hit %",
122                         100.0f * cache_hit_counter / cached_blocks.size());
123
124         /*
125                 Mark the block as urgent if requested
126         */
127         if (urgent)
128                 m_urgents.insert(p);
129
130         /*
131                 Find if block is already in queue.
132                 If it is, update the data and quit.
133         */
134         for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
135                         i != m_queue.end(); ++i) {
136                 QueuedMeshUpdate *q = *i;
137                 if (q->p == p) {
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();
144                         return;
145                 }
146         }
147
148         /*
149                 Add the block
150         */
151         QueuedMeshUpdate *q = new QueuedMeshUpdate;
152         q->p = p;
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);
157
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++;
161         }
162 }
163
164 // Returned pointer must be deleted
165 // Returns NULL if queue is empty
166 QueuedMeshUpdate *MeshUpdateQueue::pop()
167 {
168         MutexAutoLock lock(m_mutex);
169
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)
175                         continue;
176                 m_queue.erase(i);
177                 m_urgents.erase(q->p);
178                 fillDataFromMapBlockCache(q);
179                 return q;
180         }
181         return NULL;
182 }
183
184 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
185                         size_t *cache_hit_counter)
186 {
187         std::map<v3s16, CachedMapBlockData*>::iterator it =
188                         m_cache.find(p);
189         if (it != m_cache.end()) {
190                 // Already in cache
191                 CachedMapBlockData *cached_block = it->second;
192                 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
193                         if (cache_hit_counter)
194                                 (*cache_hit_counter)++;
195                         return cached_block;
196                 }
197                 MapBlock *b = map->getBlockNoCreateNoEx(p);
198                 if (b) {
199                         if (cached_block->data == NULL)
200                                 cached_block->data =
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));
204                 } else {
205                         delete[] cached_block->data;
206                         cached_block->data = NULL;
207                 }
208                 return cached_block;
209         } else {
210                 // Not yet in cache
211                 CachedMapBlockData *cached_block = new CachedMapBlockData();
212                 m_cache[p] = cached_block;
213                 MapBlock *b = map->getBlockNoCreateNoEx(p);
214                 if (b) {
215                         cached_block->data =
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));
219                 }
220                 return cached_block;
221         }
222 }
223
224 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
225 {
226         std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
227         if (it != m_cache.end()) {
228                 return it->second;
229         }
230         return NULL;
231 }
232
233 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
234 {
235         MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
236                         m_cache_use_tangent_vertices);
237         q->data = data;
238
239         data->fillBlockDataBegin(q->p);
240
241         int t_now = time(0);
242
243         // Collect data for 3*3*3 blocks from cache
244         v3s16 dp;
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++) {
248                 v3s16 p = q->p + dp;
249                 CachedMapBlockData *cached_block = getCachedBlock(p);
250                 if (cached_block) {
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);
255                 }
256         }
257
258         data->setCrack(q->crack_level, q->crack_pos);
259         data->setSmoothLighting(m_cache_smooth_lighting);
260 }
261
262 void MeshUpdateQueue::cleanupCache()
263 {
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());
268
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));
275
276         int t_now = time(0);
277
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) {
283                         m_cache.erase(it++);
284                 } else {
285                         ++it;
286                 }
287         }
288 }
289
290 /*
291         MeshUpdateThread
292 */
293
294 MeshUpdateThread::MeshUpdateThread(Client *client):
295         UpdateThread("Mesh"),
296         m_queue_in(client)
297 {
298         m_generation_interval = g_settings->getU16("mesh_generation_interval");
299         m_generation_interval = rangelim(m_generation_interval, 0, 50);
300 }
301
302 void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
303                 bool urgent)
304 {
305         // Allow the MeshUpdateQueue to do whatever it wants
306         m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
307         deferUpdate();
308 }
309
310 void MeshUpdateThread::doUpdate()
311 {
312         QueuedMeshUpdate *q;
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");
317
318                 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
319
320                 MeshUpdateResult r;
321                 r.p = q->p;
322                 r.mesh = mesh_new;
323                 r.ack_block_to_server = q->ack_block_to_server;
324
325                 m_queue_out.push_back(r);
326
327                 delete q;
328         }
329 }