]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.h
8x block meshes (#13133)
[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 #include <unordered_map>
32
33 struct QueuedMeshUpdate
34 {
35         v3s16 p = v3s16(-1337, -1337, -1337);
36         std::vector<v3s16> ack_list;
37         int crack_level = -1;
38         v3s16 crack_pos;
39         MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
40         std::vector<MapBlock *> map_blocks;
41         bool urgent = false;
42
43         QueuedMeshUpdate() = default;
44         ~QueuedMeshUpdate();
45 };
46
47 /*
48         A thread-safe queue of mesh update tasks and a cache of MapBlock data
49 */
50 class MeshUpdateQueue
51 {
52         enum UpdateMode
53         {
54                 FORCE_UPDATE,
55                 SKIP_UPDATE_IF_ALREADY_CACHED,
56         };
57
58 public:
59         MeshUpdateQueue(Client *client);
60
61         ~MeshUpdateQueue();
62
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);
66
67         // Returned pointer must be deleted
68         // Returns NULL if queue is empty
69         QueuedMeshUpdate *pop();
70
71         // Marks a position as finished, unblocking the next update
72         void done(v3s16 pos);
73
74         u32 size()
75         {
76                 MutexAutoLock lock(m_mutex);
77                 return m_queue.size();
78         }
79
80 private:
81         Client *m_client;
82         std::vector<QueuedMeshUpdate *> m_queue;
83         std::unordered_set<v3s16> m_urgents;
84         std::unordered_set<v3s16> m_inflight_blocks;
85         std::mutex m_mutex;
86
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;
91
92         void fillDataFromMapBlocks(QueuedMeshUpdate *q);
93         void cleanupCache();
94 };
95
96 struct MeshUpdateResult
97 {
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;
102         bool urgent = false;
103         std::vector<MapBlock *> map_blocks;
104
105         MeshUpdateResult() = default;
106 };
107
108 class MeshUpdateManager;
109
110 class MeshUpdateWorkerThread : public UpdateThread
111 {
112 public:
113         MeshUpdateWorkerThread(MeshUpdateQueue *queue_in, MeshUpdateManager *manager, v3s16 *camera_offset);
114
115 protected:
116         virtual void doUpdate();
117
118 private:
119         MeshUpdateQueue *m_queue_in;
120         MeshUpdateManager *m_manager;
121         v3s16 *m_camera_offset;
122
123         // TODO: Add callback to update these when g_settings changes
124         int m_generation_interval;
125 };
126
127 class MeshUpdateManager
128 {
129 public:
130         MeshUpdateManager(Client *client);
131
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);
138
139
140         v3s16 m_camera_offset;
141
142         void start();
143         void stop();
144         void wait();
145
146         bool isRunning();
147
148 private:
149         void deferUpdate();
150
151
152         MeshUpdateQueue m_queue_in;
153         MutexedQueue<MeshUpdateResult> m_queue_out;
154         MutexedQueue<MeshUpdateResult> m_queue_out_urgent;
155
156         std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers;
157 };