]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.h
Occlusion culling algorithm based on recursive descend (#13104)
[minetest.git] / src / client / mesh_generator_thread.h
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 #pragma once
21
22 #include <ctime>
23 #include <mutex>
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"
29 #include <vector>
30 #include <memory>
31
32 struct CachedMapBlockData
33 {
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);
38
39         CachedMapBlockData() = default;
40         ~CachedMapBlockData();
41 };
42
43 struct QueuedMeshUpdate
44 {
45         v3s16 p = v3s16(-1337, -1337, -1337);
46         bool ack_block_to_server = false;
47         int crack_level = -1;
48         v3s16 crack_pos;
49         MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
50         bool urgent = false;
51
52         QueuedMeshUpdate() = default;
53         ~QueuedMeshUpdate();
54 };
55
56 /*
57         A thread-safe queue of mesh update tasks and a cache of MapBlock data
58 */
59 class MeshUpdateQueue
60 {
61         enum UpdateMode
62         {
63                 FORCE_UPDATE,
64                 SKIP_UPDATE_IF_ALREADY_CACHED,
65         };
66
67 public:
68         MeshUpdateQueue(Client *client);
69
70         ~MeshUpdateQueue();
71
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);
75
76         // Returned pointer must be deleted
77         // Returns NULL if queue is empty
78         QueuedMeshUpdate *pop();
79
80         // Marks a position as finished, unblocking the next update
81         void done(v3s16 pos);
82
83         u32 size()
84         {
85                 MutexAutoLock lock(m_mutex);
86                 return m_queue.size();
87         }
88
89 private:
90         Client *m_client;
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
96         std::mutex m_mutex;
97
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;
102
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);
107         void cleanupCache();
108 };
109
110 struct MeshUpdateResult
111 {
112         v3s16 p = v3s16(-1338, -1338, -1338);
113         MapBlockMesh *mesh = nullptr;
114         u8 solid_sides = 0;
115         bool ack_block_to_server = false;
116         bool urgent = false;
117
118         MeshUpdateResult() = default;
119 };
120
121 class MeshUpdateManager;
122
123 class MeshUpdateWorkerThread : public UpdateThread
124 {
125 public:
126         MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset);
127
128 protected:
129         virtual void doUpdate();
130
131 private:
132         MeshUpdateQueue *m_queue_in;
133         MeshUpdateManager *m_manager;
134         v3s16 *m_camera_offset;
135
136         // TODO: Add callback to update these when g_settings changes
137         int m_generation_interval;
138 };
139
140 class MeshUpdateManager
141 {
142 public:
143         MeshUpdateManager(Client *client);
144
145         // Caches the block at p and its neighbors (if needed) and queues a mesh
146         // update for the block at p
147         void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent,
148                         bool update_neighbors = false);
149         void putResult(const MeshUpdateResult &r);
150         bool getNextResult(MeshUpdateResult &r);
151
152
153         v3s16 m_camera_offset;
154
155         void start();
156         void stop();
157         void wait();
158
159         bool isRunning();
160
161 private:
162         void deferUpdate();
163
164
165         MeshUpdateQueue m_queue_in;
166         MutexedQueue<MeshUpdateResult> m_queue_out;
167         MutexedQueue<MeshUpdateResult> m_queue_out_urgent;
168
169         std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers;
170 };