]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/mesh_generator_thread.cpp
69d61d5e677b0dcefe1fe9892c14dc9dcaa2b458
[dragonfireclient.git] / src / client / 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 {
33         assert(refcount_from_queue == 0);
34
35         delete[] data;
36 }
37
38 /*
39         QueuedMeshUpdate
40 */
41
42 QueuedMeshUpdate::~QueuedMeshUpdate()
43 {
44         delete data;
45 }
46
47 /*
48         MeshUpdateQueue
49 */
50
51 MeshUpdateQueue::MeshUpdateQueue(Client *client) : m_client(client)
52 {
53         m_cache_enable_shaders = g_settings->getBool("enable_shaders");
54         m_cache_use_tangent_vertices =
55                         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");
60 }
61
62 MeshUpdateQueue::~MeshUpdateQueue()
63 {
64         MutexAutoLock lock(m_mutex);
65
66         for (auto &i : m_cache) {
67                 delete i.second;
68         }
69
70         for (QueuedMeshUpdate *q : m_queue) {
71                 delete q;
72         }
73 }
74
75 void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
76 {
77         MutexAutoLock lock(m_mutex);
78
79         cleanupCache();
80
81         /*
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)
84         */
85         std::vector<CachedMapBlockData *> cached_blocks;
86         size_t cache_hit_counter = 0;
87         cached_blocks.reserve(3 * 3 * 3);
88         v3s16 dp;
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++) {
92                                 v3s16 p1 = p + dp;
93                                 CachedMapBlockData *cached_block;
94                                 if (dp == v3s16(0, 0, 0))
95                                         cached_block = cacheBlock(map, p1, FORCE_UPDATE);
96                                 else
97                                         cached_block = cacheBlock(map, p1,
98                                                         SKIP_UPDATE_IF_ALREADY_CACHED,
99                                                         &cache_hit_counter);
100                                 cached_blocks.push_back(cached_block);
101                         }
102         g_profiler->avg("MeshUpdateQueue: MapBlocks from cache [%]",
103                         100.0f * cache_hit_counter / cached_blocks.size());
104
105         /*
106                 Mark the block as urgent if requested
107         */
108         if (urgent)
109                 m_urgents.insert(p);
110
111         /*
112                 Find if block is already in queue.
113                 If it is, update the data and quit.
114         */
115         for (QueuedMeshUpdate *q : m_queue) {
116                 if (q->p == p) {
117                         // NOTE: We are not adding a new position to the queue, thus
118                         //       refcount_from_queue stays the same.
119                         if (ack_block_to_server)
120                                 q->ack_block_to_server = true;
121                         q->crack_level = m_client->getCrackLevel();
122                         q->crack_pos = m_client->getCrackPos();
123                         return;
124                 }
125         }
126
127         /*
128                 Add the block
129         */
130         QueuedMeshUpdate *q = new QueuedMeshUpdate;
131         q->p = p;
132         q->ack_block_to_server = ack_block_to_server;
133         q->crack_level = m_client->getCrackLevel();
134         q->crack_pos = m_client->getCrackPos();
135         m_queue.push_back(q);
136
137         // This queue entry is a new reference to the cached blocks
138         for (CachedMapBlockData *cached_block : cached_blocks) {
139                 cached_block->refcount_from_queue++;
140         }
141 }
142
143 // Returned pointer must be deleted
144 // Returns NULL if queue is empty
145 QueuedMeshUpdate *MeshUpdateQueue::pop()
146 {
147         MutexAutoLock lock(m_mutex);
148
149         bool must_be_urgent = !m_urgents.empty();
150         for (std::vector<QueuedMeshUpdate *>::iterator i = m_queue.begin();
151                         i != m_queue.end(); ++i) {
152                 QueuedMeshUpdate *q = *i;
153                 if (must_be_urgent && m_urgents.count(q->p) == 0)
154                         continue;
155                 m_queue.erase(i);
156                 m_urgents.erase(q->p);
157                 fillDataFromMapBlockCache(q);
158                 return q;
159         }
160         return NULL;
161 }
162
163 CachedMapBlockData *MeshUpdateQueue::cacheBlock(
164                 Map *map, v3s16 p, UpdateMode mode, size_t *cache_hit_counter)
165 {
166         CachedMapBlockData *cached_block = nullptr;
167         std::map<v3s16, CachedMapBlockData *>::iterator it = m_cache.find(p);
168
169         if (it != m_cache.end()) {
170                 cached_block = it->second;
171
172                 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
173                         if (cache_hit_counter)
174                                 (*cache_hit_counter)++;
175                         return cached_block;
176                 }
177         }
178
179         if (!cached_block) {
180                 // Not yet in cache
181                 cached_block = new CachedMapBlockData();
182                 m_cache[p] = cached_block;
183         }
184
185         MapBlock *b = map->getBlockNoCreateNoEx(p);
186         if (b) {
187                 if (!cached_block->data)
188                         cached_block->data = new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE *
189                                                          MAP_BLOCKSIZE];
190                 memcpy(cached_block->data, b->getData(),
191                                 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
192                                                 sizeof(MapNode));
193         } else {
194                 delete[] cached_block->data;
195                 cached_block->data = nullptr;
196         }
197         return cached_block;
198 }
199
200 CachedMapBlockData *MeshUpdateQueue::getCachedBlock(const v3s16 &p)
201 {
202         std::map<v3s16, CachedMapBlockData *>::iterator it = m_cache.find(p);
203         if (it != m_cache.end()) {
204                 return it->second;
205         }
206         return NULL;
207 }
208
209 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
210 {
211         MeshMakeData *data = new MeshMakeData(
212                         m_client, m_cache_enable_shaders, m_cache_use_tangent_vertices);
213         q->data = data;
214
215         data->fillBlockDataBegin(q->p);
216
217         std::time_t t_now = std::time(0);
218
219         // Collect data for 3*3*3 blocks from cache
220         v3s16 dp;
221         for (dp.X = -1; dp.X <= 1; dp.X++)
222                 for (dp.Y = -1; dp.Y <= 1; dp.Y++)
223                         for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
224                                 v3s16 p = q->p + dp;
225                                 CachedMapBlockData *cached_block = getCachedBlock(p);
226                                 if (cached_block) {
227                                         cached_block->refcount_from_queue--;
228                                         cached_block->last_used_timestamp = t_now;
229                                         if (cached_block->data)
230                                                 data->fillBlockData(
231                                                                 dp, cached_block->data);
232                                 }
233                         }
234
235         data->setCrack(q->crack_level, q->crack_pos);
236         data->setSmoothLighting(m_cache_smooth_lighting);
237 }
238
239 void MeshUpdateQueue::cleanupCache()
240 {
241         const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
242                                 sizeof(MapNode) / 1000;
243         g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
244                         mapblock_kB * m_cache.size());
245
246         // The cache size is kept roughly below cache_soft_max_size, not letting
247         // anything get older than cache_seconds_max or deleted before 2 seconds.
248         const int cache_seconds_max = 10;
249         const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
250         int cache_seconds = MYMAX(2,
251                         cache_seconds_max -
252                                         m_cache.size() /
253                                                         (cache_soft_max_size /
254                                                                         cache_seconds_max));
255
256         int t_now = time(0);
257
258         for (std::map<v3s16, CachedMapBlockData *>::iterator it = m_cache.begin();
259                         it != m_cache.end();) {
260                 CachedMapBlockData *cached_block = it->second;
261                 if (cached_block->refcount_from_queue == 0 &&
262                                 cached_block->last_used_timestamp <
263                                                 t_now - cache_seconds) {
264                         m_cache.erase(it++);
265                         delete cached_block;
266                 } else {
267                         ++it;
268                 }
269         }
270 }
271
272 /*
273         MeshUpdateThread
274 */
275
276 MeshUpdateThread::MeshUpdateThread(Client *client) :
277                 UpdateThread("Mesh"), m_queue_in(client)
278 {
279         m_generation_interval = g_settings->getU16("mesh_generation_interval");
280         m_generation_interval = rangelim(m_generation_interval, 0, 50);
281 }
282
283 void MeshUpdateThread::updateBlock(
284                 Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
285 {
286         // Allow the MeshUpdateQueue to do whatever it wants
287         m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
288         deferUpdate();
289 }
290
291 void MeshUpdateThread::doUpdate()
292 {
293         QueuedMeshUpdate *q;
294         while ((q = m_queue_in.pop())) {
295                 if (m_generation_interval)
296                         sleep_ms(m_generation_interval);
297                 ScopeProfiler sp(g_profiler, "Client: Mesh making (sum)");
298
299                 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
300
301                 MeshUpdateResult r;
302                 r.p = q->p;
303                 r.mesh = mesh_new;
304                 r.ack_block_to_server = q->ack_block_to_server;
305
306                 m_queue_out.push_back(r);
307
308                 delete q;
309         }
310 }