]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.cpp
Remove mapblock cache for mesh generation. (#13124)
[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 #include "util/directiontables.h"
27
28 /*
29         QueuedMeshUpdate
30 */
31
32 QueuedMeshUpdate::~QueuedMeshUpdate()
33 {
34         delete data;
35 }
36
37 /*
38         MeshUpdateQueue
39 */
40
41 MeshUpdateQueue::MeshUpdateQueue(Client *client):
42         m_client(client)
43 {
44         m_cache_enable_shaders = g_settings->getBool("enable_shaders");
45         m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
46         m_meshgen_block_cache_size = g_settings->getS32("meshgen_block_cache_size");
47 }
48
49 MeshUpdateQueue::~MeshUpdateQueue()
50 {
51         MutexAutoLock lock(m_mutex);
52
53         for (QueuedMeshUpdate *q : m_queue) {
54                 for (auto block : q->map_blocks)
55                         if (block)
56                                 block->refDrop();
57                 delete q;
58         }
59 }
60
61 bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
62 {
63         MapBlock *main_block = map->getBlockNoCreateNoEx(p);
64         if (!main_block)
65                 return false;
66
67         MutexAutoLock lock(m_mutex);
68
69         /*
70                 Mark the block as urgent if requested
71         */
72         if (urgent)
73                 m_urgents.insert(p);
74
75         /*
76                 Find if block is already in queue.
77                 If it is, update the data and quit.
78         */
79         for (QueuedMeshUpdate *q : m_queue) {
80                 if (q->p == p) {
81                         // NOTE: We are not adding a new position to the queue, thus
82                         //       refcount_from_queue stays the same.
83                         if(ack_block_to_server)
84                                 q->ack_block_to_server = true;
85                         q->crack_level = m_client->getCrackLevel();
86                         q->crack_pos = m_client->getCrackPos();
87                         q->urgent |= urgent;
88                         for (std::size_t i = 1; i < q->map_blocks.size(); i++) {
89                                 if (!q->map_blocks[i]) {
90                                         MapBlock *block = map->getBlockNoCreateNoEx(q->p + g_26dirs[i - 1]);
91                                         if (block) {
92                                                 block->refGrab();
93                                                 q->map_blocks[i] = block;
94                                         }
95                                 }
96                         }
97                         return true;
98                 }
99         }
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<MapBlock *> cached_blocks;
106         cached_blocks.reserve(3*3*3);
107         cached_blocks.push_back(main_block);
108         main_block->refGrab();
109         for (v3s16 dp : g_26dirs) {
110                 MapBlock *block = map->getBlockNoCreateNoEx(p + dp);
111                 cached_blocks.push_back(block);
112                 if (block)
113                         block->refGrab();
114         }
115
116         /*
117                 Add the block
118         */
119         QueuedMeshUpdate *q = new QueuedMeshUpdate;
120         q->p = p;
121         q->ack_block_to_server = ack_block_to_server;
122         q->crack_level = m_client->getCrackLevel();
123         q->crack_pos = m_client->getCrackPos();
124         q->urgent = urgent;
125         q->map_blocks = std::move(cached_blocks);
126         m_queue.push_back(q);
127
128         return true;
129 }
130
131 // Returned pointer must be deleted
132 // Returns NULL if queue is empty
133 QueuedMeshUpdate *MeshUpdateQueue::pop()
134 {
135         QueuedMeshUpdate *result = NULL;
136         {
137                 MutexAutoLock lock(m_mutex);
138
139                 bool must_be_urgent = !m_urgents.empty();
140                 for (std::vector<QueuedMeshUpdate*>::iterator i = m_queue.begin();
141                                 i != m_queue.end(); ++i) {
142                         QueuedMeshUpdate *q = *i;
143                         if (must_be_urgent && m_urgents.count(q->p) == 0)
144                                 continue;
145                         // Make sure no two threads are processing the same mapblock, as that causes racing conditions
146                         if (m_inflight_blocks.find(q->p) != m_inflight_blocks.end())
147                                 continue;
148                         m_queue.erase(i);
149                         m_urgents.erase(q->p);
150                         m_inflight_blocks.insert(q->p);
151                         result = q;
152                         break;
153                 }
154         }
155
156         if (result)
157                 fillDataFromMapBlocks(result);
158
159         return result;
160 }
161
162 void MeshUpdateQueue::done(v3s16 pos)
163 {
164         MutexAutoLock lock(m_mutex);
165         m_inflight_blocks.erase(pos);
166 }
167
168
169 void MeshUpdateQueue::fillDataFromMapBlocks(QueuedMeshUpdate *q)
170 {
171         MeshMakeData *data = new MeshMakeData(m_client, m_cache_enable_shaders);
172         q->data = data;
173
174         data->fillBlockDataBegin(q->p);
175
176         for (MapBlock *block : q->map_blocks)
177                 if (block)
178                         data->fillBlockData(block->getPos() - q->p, block->getData());
179
180         data->setCrack(q->crack_level, q->crack_pos);
181         data->setSmoothLighting(m_cache_smooth_lighting);
182 }
183
184 /*
185         MeshUpdateWorkerThread
186 */
187
188 MeshUpdateWorkerThread::MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset) :
189                 UpdateThread("Mesh"), m_queue_in(queue_in), m_manager(manager), m_camera_offset(camera_offset)
190 {
191         m_generation_interval = g_settings->getU16("mesh_generation_interval");
192         m_generation_interval = rangelim(m_generation_interval, 0, 50);
193 }
194
195 void MeshUpdateWorkerThread::doUpdate()
196 {
197         QueuedMeshUpdate *q;
198         while ((q = m_queue_in->pop())) {
199                 if (m_generation_interval)
200                         sleep_ms(m_generation_interval);
201                 ScopeProfiler sp(g_profiler, "Client: Mesh making (sum)");
202
203                 MapBlockMesh *mesh_new = new MapBlockMesh(q->data, *m_camera_offset);
204
205                 
206
207                 MeshUpdateResult r;
208                 r.p = q->p;
209                 r.mesh = mesh_new;
210                 r.solid_sides = get_solid_sides(q->data);
211                 r.ack_block_to_server = q->ack_block_to_server;
212                 r.urgent = q->urgent;
213                 r.map_blocks = q->map_blocks;
214
215                 m_manager->putResult(r);
216                 m_queue_in->done(q->p);
217                 delete q;
218         }
219 }
220
221 /*
222         MeshUpdateManager
223 */
224
225 MeshUpdateManager::MeshUpdateManager(Client *client):
226         m_queue_in(client)
227 {
228         int number_of_threads = rangelim(g_settings->getS32("mesh_generation_threads"), 0, 8);
229
230         // Automatically use 33% of the system cores for mesh generation, max 4
231         if (number_of_threads == 0)
232                 number_of_threads = MYMIN(4, Thread::getNumberOfProcessors() / 3);
233         
234         // use at least one thread
235         number_of_threads = MYMAX(1, number_of_threads);
236         infostream << "MeshUpdateManager: using " << number_of_threads << " threads" << std::endl;
237
238         for (int i = 0; i < number_of_threads; i++)
239                 m_workers.push_back(std::make_unique<MeshUpdateWorkerThread>(&m_queue_in, this, &m_camera_offset));
240 }
241
242 void MeshUpdateManager::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
243                 bool urgent, bool update_neighbors)
244 {
245         static thread_local const bool many_neighbors =
246                         g_settings->getBool("smooth_lighting")
247                         && !g_settings->getFlag("performance_tradeoffs");
248         if (!m_queue_in.addBlock(map, p, ack_block_to_server, urgent)) {
249                 warningstream << "Update requested for non-existent block at ("
250                                 << p.X << ", " << p.Y << ", " << p.Z << ")" << std::endl;
251                 return;
252         }
253         if (update_neighbors) {
254                 if (many_neighbors) {
255                         for (v3s16 dp : g_26dirs)
256                                 m_queue_in.addBlock(map, p + dp, false, urgent);
257                 } else {
258                         for (v3s16 dp : g_6dirs)
259                                 m_queue_in.addBlock(map, p + dp, false, urgent);
260                 }
261         }
262         deferUpdate();
263 }
264
265 void MeshUpdateManager::putResult(const MeshUpdateResult &result)
266 {
267         if (result.urgent)
268                 m_queue_out_urgent.push_back(result);
269         else
270                 m_queue_out.push_back(result);
271 }
272
273 bool MeshUpdateManager::getNextResult(MeshUpdateResult &r)
274 {
275         if (!m_queue_out_urgent.empty()) {
276                 r = m_queue_out_urgent.pop_frontNoEx();
277                 return true;
278         }
279
280         if (!m_queue_out.empty()) {
281                 r = m_queue_out.pop_frontNoEx();
282                 return true;
283         }
284
285         return false;
286 }
287
288 void MeshUpdateManager::deferUpdate()
289 {
290         for (auto &thread : m_workers)
291                 thread->deferUpdate();
292 }
293
294 void MeshUpdateManager::start()
295 {
296         for (auto &thread: m_workers)
297                 thread->start();
298 }
299
300 void MeshUpdateManager::stop()
301 {
302         for (auto &thread: m_workers)
303                 thread->stop();
304 }
305
306 void MeshUpdateManager::wait()
307 {
308         for (auto &thread: m_workers)
309                 thread->wait();
310 }
311
312 bool MeshUpdateManager::isRunning()
313 {
314         for (auto &thread: m_workers)
315                 if (thread->isRunning())
316                         return true;
317         return false;
318 }