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