3 Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
24 #include <unordered_map>
25 #include <unordered_set>
26 #include "mapblock_mesh.h"
27 #include "threading/mutex_auto_lock.h"
28 #include "util/thread.h"
31 #include <unordered_map>
33 struct QueuedMeshUpdate
35 v3s16 p = v3s16(-1337, -1337, -1337);
36 std::vector<v3s16> ack_list;
39 MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
40 std::vector<MapBlock *> map_blocks;
43 QueuedMeshUpdate() = default;
48 A thread-safe queue of mesh update tasks and a cache of MapBlock data
55 SKIP_UPDATE_IF_ALREADY_CACHED,
59 MeshUpdateQueue(Client *client);
63 // Caches the block at p and its neighbors (if needed) and queues a mesh
64 // update for the block at p
65 bool addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
67 // Returned pointer must be deleted
68 // Returns NULL if queue is empty
69 QueuedMeshUpdate *pop();
71 // Marks a position as finished, unblocking the next update
76 MutexAutoLock lock(m_mutex);
77 return m_queue.size();
82 std::vector<QueuedMeshUpdate *> m_queue;
83 std::unordered_set<v3s16> m_urgents;
84 std::unordered_set<v3s16> m_inflight_blocks;
87 // TODO: Add callback to update these when g_settings changes
88 bool m_cache_enable_shaders;
89 bool m_cache_smooth_lighting;
90 int m_meshgen_block_cache_size;
92 void fillDataFromMapBlocks(QueuedMeshUpdate *q);
96 struct MeshUpdateResult
98 v3s16 p = v3s16(-1338, -1338, -1338);
99 MapBlockMesh *mesh = nullptr;
100 std::unordered_map<v3s16, u8> solid_sides;
101 std::vector<v3s16> ack_list;
103 std::vector<MapBlock *> map_blocks;
105 MeshUpdateResult() = default;
108 class MeshUpdateManager;
110 class MeshUpdateWorkerThread : public UpdateThread
113 MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset);
116 virtual void doUpdate();
119 MeshUpdateQueue *m_queue_in;
120 MeshUpdateManager *m_manager;
121 v3s16 *m_camera_offset;
123 // TODO: Add callback to update these when g_settings changes
124 int m_generation_interval;
127 class MeshUpdateManager
130 MeshUpdateManager(Client *client);
132 // Caches the block at p and its neighbors (if needed) and queues a mesh
133 // update for the block at p
134 void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent,
135 bool update_neighbors = false);
136 void putResult(const MeshUpdateResult &r);
137 bool getNextResult(MeshUpdateResult &r);
140 v3s16 m_camera_offset;
152 MeshUpdateQueue m_queue_in;
153 MutexedQueue<MeshUpdateResult> m_queue_out;
154 MutexedQueue<MeshUpdateResult> m_queue_out_urgent;
156 std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers;