]> git.lizzy.rs Git - minetest.git/blob - src/client/mesh_generator_thread.h
Limit force shadow update to urgent blocks (#12692)
[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
30 struct CachedMapBlockData
31 {
32         v3s16 p = v3s16(-1337, -1337, -1337);
33         MapNode *data = nullptr; // A copy of the MapBlock's data member
34         int refcount_from_queue = 0;
35         std::time_t last_used_timestamp = std::time(0);
36
37         CachedMapBlockData() = default;
38         ~CachedMapBlockData();
39 };
40
41 struct QueuedMeshUpdate
42 {
43         v3s16 p = v3s16(-1337, -1337, -1337);
44         bool ack_block_to_server = false;
45         int crack_level = -1;
46         v3s16 crack_pos;
47         MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
48         bool urgent = false;
49
50         QueuedMeshUpdate() = default;
51         ~QueuedMeshUpdate();
52 };
53
54 /*
55         A thread-safe queue of mesh update tasks and a cache of MapBlock data
56 */
57 class MeshUpdateQueue
58 {
59         enum UpdateMode
60         {
61                 FORCE_UPDATE,
62                 SKIP_UPDATE_IF_ALREADY_CACHED,
63         };
64
65 public:
66         MeshUpdateQueue(Client *client);
67
68         ~MeshUpdateQueue();
69
70         // Caches the block at p and its neighbors (if needed) and queues a mesh
71         // update for the block at p
72         bool addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
73
74         // Returned pointer must be deleted
75         // Returns NULL if queue is empty
76         QueuedMeshUpdate *pop();
77
78         u32 size()
79         {
80                 MutexAutoLock lock(m_mutex);
81                 return m_queue.size();
82         }
83
84 private:
85         Client *m_client;
86         std::vector<QueuedMeshUpdate *> m_queue;
87         std::unordered_set<v3s16> m_urgents;
88         std::unordered_map<v3s16, CachedMapBlockData *> m_cache;
89         u64 m_next_cache_cleanup; // milliseconds
90         std::mutex m_mutex;
91
92         // TODO: Add callback to update these when g_settings changes
93         bool m_cache_enable_shaders;
94         bool m_cache_smooth_lighting;
95         int m_meshgen_block_cache_size;
96
97         CachedMapBlockData *cacheBlock(Map *map, v3s16 p, UpdateMode mode,
98                         size_t *cache_hit_counter = NULL);
99         CachedMapBlockData *getCachedBlock(const v3s16 &p);
100         void fillDataFromMapBlockCache(QueuedMeshUpdate *q);
101         void cleanupCache();
102 };
103
104 struct MeshUpdateResult
105 {
106         v3s16 p = v3s16(-1338, -1338, -1338);
107         MapBlockMesh *mesh = nullptr;
108         bool ack_block_to_server = false;
109         bool urgent = false;
110
111         MeshUpdateResult() = default;
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                         bool update_neighbors = false);
123
124         v3s16 m_camera_offset;
125         MutexedQueue<MeshUpdateResult> m_queue_out;
126
127 private:
128         MeshUpdateQueue m_queue_in;
129
130         // TODO: Add callback to update these when g_settings changes
131         int m_generation_interval;
132
133 protected:
134         virtual void doUpdate();
135 };