Use C++11 mutexes only (remove compat code) (#5922)
[oweals/minetest.git] / src / 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 #ifndef MESH_GENERATOR_THREAD_HEADER
21 #define MESH_GENERATOR_THREAD_HEADER
22
23 #include <mutex>
24 #include "mapblock_mesh.h"
25 #include "threading/mutex_auto_lock.h"
26 #include "util/thread.h"
27
28 struct CachedMapBlockData
29 {
30         v3s16 p;
31         MapNode *data; // A copy of the MapBlock's data member
32         int refcount_from_queue;
33         int last_used_timestamp;
34
35         CachedMapBlockData();
36         ~CachedMapBlockData();
37 };
38
39 struct QueuedMeshUpdate
40 {
41         v3s16 p;
42         bool ack_block_to_server;
43         bool urgent;
44         int crack_level;
45         v3s16 crack_pos;
46         MeshMakeData *data; // This is generated in MeshUpdateQueue::pop()
47
48         QueuedMeshUpdate();
49         ~QueuedMeshUpdate();
50 };
51
52 /*
53         A thread-safe queue of mesh update tasks and a cache of MapBlock data
54 */
55 class MeshUpdateQueue
56 {
57         enum UpdateMode
58         {
59                 FORCE_UPDATE,
60                 SKIP_UPDATE_IF_ALREADY_CACHED,
61         };
62
63 public:
64         MeshUpdateQueue(Client *client);
65
66         ~MeshUpdateQueue();
67
68         // Caches the block at p and its neighbors (if needed) and queues a mesh
69         // update for the block at p
70         void addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
71
72         // Returned pointer must be deleted
73         // Returns NULL if queue is empty
74         QueuedMeshUpdate *pop();
75
76         u32 size()
77         {
78                 MutexAutoLock lock(m_mutex);
79                 return m_queue.size();
80         }
81
82 private:
83         Client *m_client;
84         std::vector<QueuedMeshUpdate *> m_queue;
85         std::set<v3s16> m_urgents;
86         std::map<v3s16, CachedMapBlockData *> m_cache;
87         std::mutex m_mutex;
88
89         // TODO: Add callback to update these when g_settings changes
90         bool m_cache_enable_shaders;
91         bool m_cache_use_tangent_vertices;
92         bool m_cache_smooth_lighting;
93         int m_meshgen_block_cache_size;
94
95         CachedMapBlockData *cacheBlock(Map *map, v3s16 p, UpdateMode mode,
96                         size_t *cache_hit_counter = NULL);
97         CachedMapBlockData *getCachedBlock(const v3s16 &p);
98         void fillDataFromMapBlockCache(QueuedMeshUpdate *q);
99         void cleanupCache();
100 };
101
102 struct MeshUpdateResult
103 {
104         v3s16 p;
105         MapBlockMesh *mesh;
106         bool ack_block_to_server;
107
108         MeshUpdateResult()
109             : p(-1338, -1338, -1338), mesh(NULL), ack_block_to_server(false)
110         {
111         }
112 };
113
114 class MeshUpdateThread : public UpdateThread
115 {
116 public:
117         MeshUpdateThread(Client *client);
118
119         // Caches the block at p and its neighbors (if needed) and queues a mesh
120         // update for the block at p
121         void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
122
123         v3s16 m_camera_offset;
124         MutexedQueue<MeshUpdateResult> m_queue_out;
125
126 private:
127         MeshUpdateQueue m_queue_in;
128
129         // TODO: Add callback to update these when g_settings changes
130         int m_generation_interval;
131
132 protected:
133         virtual void doUpdate();
134 };
135
136 #endif