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"
32 struct CachedMapBlockData
34 v3s16 p = v3s16(-1337, -1337, -1337);
35 MapNode *data = nullptr; // A copy of the MapBlock's data member
36 int refcount_from_queue = 0;
37 std::time_t last_used_timestamp = std::time(0);
39 CachedMapBlockData() = default;
40 ~CachedMapBlockData();
43 struct QueuedMeshUpdate
45 v3s16 p = v3s16(-1337, -1337, -1337);
46 bool ack_block_to_server = false;
49 MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
52 QueuedMeshUpdate() = default;
57 A thread-safe queue of mesh update tasks and a cache of MapBlock data
64 SKIP_UPDATE_IF_ALREADY_CACHED,
68 MeshUpdateQueue(Client *client);
72 // Caches the block at p and its neighbors (if needed) and queues a mesh
73 // update for the block at p
74 bool addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
76 // Returned pointer must be deleted
77 // Returns NULL if queue is empty
78 QueuedMeshUpdate *pop();
80 // Marks a position as finished, unblocking the next update
85 MutexAutoLock lock(m_mutex);
86 return m_queue.size();
91 std::vector<QueuedMeshUpdate *> m_queue;
92 std::unordered_set<v3s16> m_urgents;
93 std::unordered_map<v3s16, CachedMapBlockData *> m_cache;
94 std::unordered_set<v3s16> m_inflight_blocks;
95 u64 m_next_cache_cleanup; // milliseconds
98 // TODO: Add callback to update these when g_settings changes
99 bool m_cache_enable_shaders;
100 bool m_cache_smooth_lighting;
101 int m_meshgen_block_cache_size;
103 CachedMapBlockData *cacheBlock(Map *map, v3s16 p, UpdateMode mode,
104 size_t *cache_hit_counter = NULL);
105 CachedMapBlockData *getCachedBlock(const v3s16 &p);
106 void fillDataFromMapBlockCache(QueuedMeshUpdate *q);
110 struct MeshUpdateResult
112 v3s16 p = v3s16(-1338, -1338, -1338);
113 MapBlockMesh *mesh = nullptr;
114 bool ack_block_to_server = false;
117 MeshUpdateResult() = default;
120 class MeshUpdateManager;
122 class MeshUpdateWorkerThread : public UpdateThread
125 MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset);
128 virtual void doUpdate();
131 MeshUpdateQueue *m_queue_in;
132 MeshUpdateManager *m_manager;
133 v3s16 *m_camera_offset;
135 // TODO: Add callback to update these when g_settings changes
136 int m_generation_interval;
139 class MeshUpdateManager
142 MeshUpdateManager(Client *client);
144 // Caches the block at p and its neighbors (if needed) and queues a mesh
145 // update for the block at p
146 void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent,
147 bool update_neighbors = false);
148 void putResult(const MeshUpdateResult &r);
149 bool getNextResult(MeshUpdateResult &r);
152 v3s16 m_camera_offset;
164 MeshUpdateQueue m_queue_in;
165 MutexedQueue<MeshUpdateResult> m_queue_out;
166 MutexedQueue<MeshUpdateResult> m_queue_out_urgent;
168 std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers;