]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mesh_generator_thread.cpp
Fix Android node selection distance (#6187)
[dragonfireclient.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 {
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):
52         m_client(client)
53 {
54         m_cache_enable_shaders = g_settings->getBool("enable_shaders");
55         m_cache_use_tangent_vertices = 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         DSTACK(FUNCTION_NAME);
78
79         MutexAutoLock lock(m_mutex);
80
81         cleanupCache();
82
83         /*
84                 Cache the block data (force-update the center block, don't update the
85                 neighbors but get them if they aren't already cached)
86         */
87         std::vector<CachedMapBlockData*> cached_blocks;
88         size_t cache_hit_counter = 0;
89         cached_blocks.reserve(3*3*3);
90         v3s16 dp;
91         for (dp.X = -1; dp.X <= 1; dp.X++)
92         for (dp.Y = -1; dp.Y <= 1; dp.Y++)
93         for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
94                 v3s16 p1 = p + dp;
95                 CachedMapBlockData *cached_block;
96                 if (dp == v3s16(0, 0, 0))
97                         cached_block = cacheBlock(map, p1, FORCE_UPDATE);
98                 else
99                         cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
100                                         &cache_hit_counter);
101                 cached_blocks.push_back(cached_block);
102         }
103         g_profiler->avg("MeshUpdateQueue MapBlock cache hit %",
104                         100.0f * cache_hit_counter / cached_blocks.size());
105
106         /*
107                 Mark the block as urgent if requested
108         */
109         if (urgent)
110                 m_urgents.insert(p);
111
112         /*
113                 Find if block is already in queue.
114                 If it is, update the data and quit.
115         */
116         for (QueuedMeshUpdate *q : m_queue) {
117                 if (q->p == p) {
118                         // NOTE: We are not adding a new position to the queue, thus
119                         //       refcount_from_queue stays the same.
120                         if(ack_block_to_server)
121                                 q->ack_block_to_server = true;
122                         q->crack_level = m_client->getCrackLevel();
123                         q->crack_pos = m_client->getCrackPos();
124                         return;
125                 }
126         }
127
128         /*
129                 Add the block
130         */
131         QueuedMeshUpdate *q = new QueuedMeshUpdate;
132         q->p = p;
133         q->ack_block_to_server = ack_block_to_server;
134         q->crack_level = m_client->getCrackLevel();
135         q->crack_pos = m_client->getCrackPos();
136         m_queue.push_back(q);
137
138         // This queue entry is a new reference to the cached blocks
139         for (CachedMapBlockData *cached_block : cached_blocks) {
140                 cached_block->refcount_from_queue++;
141         }
142 }
143
144 // Returned pointer must be deleted
145 // Returns NULL if queue is empty
146 QueuedMeshUpdate *MeshUpdateQueue::pop()
147 {
148         MutexAutoLock lock(m_mutex);
149
150         bool must_be_urgent = !m_urgents.empty();
151         for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
152                         i != m_queue.end(); ++i) {
153                 QueuedMeshUpdate *q = *i;
154                 if(must_be_urgent && m_urgents.count(q->p) == 0)
155                         continue;
156                 m_queue.erase(i);
157                 m_urgents.erase(q->p);
158                 fillDataFromMapBlockCache(q);
159                 return q;
160         }
161         return NULL;
162 }
163
164 CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mode,
165                         size_t *cache_hit_counter)
166 {
167         std::map<v3s16, CachedMapBlockData*>::iterator it =
168                         m_cache.find(p);
169         if (it != m_cache.end()) {
170                 // Already in cache
171                 CachedMapBlockData *cached_block = it->second;
172                 if (mode == SKIP_UPDATE_IF_ALREADY_CACHED) {
173                         if (cache_hit_counter)
174                                 (*cache_hit_counter)++;
175                         return cached_block;
176                 }
177                 MapBlock *b = map->getBlockNoCreateNoEx(p);
178                 if (b) {
179                         if (cached_block->data == NULL)
180                                 cached_block->data =
181                                                 new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
182                         memcpy(cached_block->data, b->getData(),
183                                         MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
184                 } else {
185                         delete[] cached_block->data;
186                         cached_block->data = NULL;
187                 }
188                 return cached_block;
189         }
190
191         // Not yet in cache
192         CachedMapBlockData *cached_block = new CachedMapBlockData();
193         m_cache[p] = cached_block;
194         MapBlock *b = map->getBlockNoCreateNoEx(p);
195         if (b) {
196                 cached_block->data =
197                                 new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
198                 memcpy(cached_block->data, b->getData(),
199                                 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
200         }
201         return cached_block;
202 }
203
204 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
205 {
206         std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
207         if (it != m_cache.end()) {
208                 return it->second;
209         }
210         return NULL;
211 }
212
213 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
214 {
215         MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
216                         m_cache_use_tangent_vertices);
217         q->data = data;
218
219         data->fillBlockDataBegin(q->p);
220
221         std::time_t t_now = std::time(0);
222
223         // Collect data for 3*3*3 blocks from cache
224         v3s16 dp;
225         for (dp.X = -1; dp.X <= 1; dp.X++)
226         for (dp.Y = -1; dp.Y <= 1; dp.Y++)
227         for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
228                 v3s16 p = q->p + dp;
229                 CachedMapBlockData *cached_block = getCachedBlock(p);
230                 if (cached_block) {
231                         cached_block->refcount_from_queue--;
232                         cached_block->last_used_timestamp = t_now;
233                         if (cached_block->data)
234                                 data->fillBlockData(dp, cached_block->data);
235                 }
236         }
237
238         data->setCrack(q->crack_level, q->crack_pos);
239         data->setSmoothLighting(m_cache_smooth_lighting);
240 }
241
242 void MeshUpdateQueue::cleanupCache()
243 {
244         const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
245                         sizeof(MapNode) / 1000;
246         g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
247                         mapblock_kB * m_cache.size());
248
249         // The cache size is kept roughly below cache_soft_max_size, not letting
250         // anything get older than cache_seconds_max or deleted before 2 seconds.
251         const int cache_seconds_max = 10;
252         const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
253         int cache_seconds = MYMAX(2, cache_seconds_max -
254                         m_cache.size() / (cache_soft_max_size / 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 < t_now - cache_seconds) {
263                         m_cache.erase(it++);
264                         delete cached_block;
265                 } else {
266                         ++it;
267                 }
268         }
269 }
270
271 /*
272         MeshUpdateThread
273 */
274
275 MeshUpdateThread::MeshUpdateThread(Client *client):
276         UpdateThread("Mesh"),
277         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(Map *map, v3s16 p, bool ack_block_to_server,
284                 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");
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 }