]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.cpp
Better F6 profiler (#8750)
[minetest.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):
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: MapBlocks from cache [%]",
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         CachedMapBlockData *cached_block = nullptr;
166         std::map<v3s16, CachedMapBlockData*>::iterator it =
167                         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 =
189                                         new MapNode[MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE];
190                 memcpy(cached_block->data, b->getData(),
191                                 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE * sizeof(MapNode));
192         } else {
193                 delete[] cached_block->data;
194                 cached_block->data = nullptr;
195         }
196         return cached_block;
197 }
198
199 CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
200 {
201         std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p);
202         if (it != m_cache.end()) {
203                 return it->second;
204         }
205         return NULL;
206 }
207
208 void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
209 {
210         MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders,
211                         m_cache_use_tangent_vertices);
212         q->data = data;
213
214         data->fillBlockDataBegin(q->p);
215
216         std::time_t t_now = std::time(0);
217
218         // Collect data for 3*3*3 blocks from cache
219         v3s16 dp;
220         for (dp.X = -1; dp.X <= 1; dp.X++)
221         for (dp.Y = -1; dp.Y <= 1; dp.Y++)
222         for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
223                 v3s16 p = q->p + dp;
224                 CachedMapBlockData *cached_block = getCachedBlock(p);
225                 if (cached_block) {
226                         cached_block->refcount_from_queue--;
227                         cached_block->last_used_timestamp = t_now;
228                         if (cached_block->data)
229                                 data->fillBlockData(dp, cached_block->data);
230                 }
231         }
232
233         data->setCrack(q->crack_level, q->crack_pos);
234         data->setSmoothLighting(m_cache_smooth_lighting);
235 }
236
237 void MeshUpdateQueue::cleanupCache()
238 {
239         const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
240                         sizeof(MapNode) / 1000;
241         g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
242                         mapblock_kB * m_cache.size());
243
244         // The cache size is kept roughly below cache_soft_max_size, not letting
245         // anything get older than cache_seconds_max or deleted before 2 seconds.
246         const int cache_seconds_max = 10;
247         const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
248         int cache_seconds = MYMAX(2, cache_seconds_max -
249                         m_cache.size() / (cache_soft_max_size / cache_seconds_max));
250
251         int t_now = time(0);
252
253         for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin();
254                         it != m_cache.end(); ) {
255                 CachedMapBlockData *cached_block = it->second;
256                 if (cached_block->refcount_from_queue == 0 &&
257                                 cached_block->last_used_timestamp < t_now - cache_seconds) {
258                         m_cache.erase(it++);
259                         delete cached_block;
260                 } else {
261                         ++it;
262                 }
263         }
264 }
265
266 /*
267         MeshUpdateThread
268 */
269
270 MeshUpdateThread::MeshUpdateThread(Client *client):
271         UpdateThread("Mesh"),
272         m_queue_in(client)
273 {
274         m_generation_interval = g_settings->getU16("mesh_generation_interval");
275         m_generation_interval = rangelim(m_generation_interval, 0, 50);
276 }
277
278 void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
279                 bool urgent)
280 {
281         // Allow the MeshUpdateQueue to do whatever it wants
282         m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
283         deferUpdate();
284 }
285
286 void MeshUpdateThread::doUpdate()
287 {
288         QueuedMeshUpdate *q;
289         while ((q = m_queue_in.pop())) {
290                 if (m_generation_interval)
291                         sleep_ms(m_generation_interval);
292                 ScopeProfiler sp(g_profiler, "Client: Mesh making (sum)");
293
294                 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
295
296                 MeshUpdateResult r;
297                 r.p = q->p;
298                 r.mesh = mesh_new;
299                 r.ack_block_to_server = q->ack_block_to_server;
300
301                 m_queue_out.push_back(r);
302
303                 delete q;
304         }
305 }