Minetest  5.4.0
mesh_generator_thread.h
Go to the documentation of this file.
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 "mapblock_mesh.h"
26 #include "util/thread.h"
27 
29 {
30  v3s16 p = v3s16(-1337, -1337, -1337);
31  MapNode *data = nullptr; // A copy of the MapBlock's data member
33  std::time_t last_used_timestamp = std::time(0);
34 
35  CachedMapBlockData() = default;
37 };
38 
40 {
41  v3s16 p = v3s16(-1337, -1337, -1337);
42  bool ack_block_to_server = false;
43  int crack_level = -1;
45  MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
46 
47  QueuedMeshUpdate() = default;
49 };
50 
51 /*
52  A thread-safe queue of mesh update tasks and a cache of MapBlock data
53 */
55 {
57  {
60  };
61 
62 public:
64 
66 
67  // Caches the block at p and its neighbors (if needed) and queues a mesh
68  // update for the block at p
69  void addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
70 
71  // Returned pointer must be deleted
72  // Returns NULL if queue is empty
74 
75  u32 size()
76  {
77  MutexAutoLock lock(m_mutex);
78  return m_queue.size();
79  }
80 
81 private:
83  std::vector<QueuedMeshUpdate *> m_queue;
84  std::set<v3s16> m_urgents;
85  std::map<v3s16, CachedMapBlockData *> m_cache;
86  std::mutex m_mutex;
87 
88  // TODO: Add callback to update these when g_settings changes
92 
94  size_t *cache_hit_counter = NULL);
97  void cleanupCache();
98 };
99 
101 {
102  v3s16 p = v3s16(-1338, -1338, -1338);
103  MapBlockMesh *mesh = nullptr;
104  bool ack_block_to_server = false;
105 
106  MeshUpdateResult() = default;
107 };
108 
110 {
111 public:
113 
114  // Caches the block at p and its neighbors (if needed) and queues a mesh
115  // update for the block at p
116  void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
117 
120 
121 private:
123 
124  // TODO: Add callback to update these when g_settings changes
126 
127 protected:
128  virtual void doUpdate();
129 };
Definition: client.h:109
Definition: mapblock_mesh.h:86
Definition: map.h:123
Definition: mesh_generator_thread.h:55
bool m_cache_enable_shaders
Definition: mesh_generator_thread.h:89
std::set< v3s16 > m_urgents
Definition: mesh_generator_thread.h:84
~MeshUpdateQueue()
Definition: mesh_generator_thread.cpp:59
CachedMapBlockData * getCachedBlock(const v3s16 &p)
Definition: mesh_generator_thread.cpp:196
std::mutex m_mutex
Definition: mesh_generator_thread.h:86
std::vector< QueuedMeshUpdate * > m_queue
Definition: mesh_generator_thread.h:83
void fillDataFromMapBlockCache(QueuedMeshUpdate *q)
Definition: mesh_generator_thread.cpp:205
u32 size()
Definition: mesh_generator_thread.h:75
MeshUpdateQueue(Client *client)
Definition: mesh_generator_thread.cpp:51
bool m_cache_smooth_lighting
Definition: mesh_generator_thread.h:90
void cleanupCache()
Definition: mesh_generator_thread.cpp:233
void addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
Definition: mesh_generator_thread.cpp:72
UpdateMode
Definition: mesh_generator_thread.h:57
@ SKIP_UPDATE_IF_ALREADY_CACHED
Definition: mesh_generator_thread.h:59
@ FORCE_UPDATE
Definition: mesh_generator_thread.h:58
QueuedMeshUpdate * pop()
Definition: mesh_generator_thread.cpp:141
CachedMapBlockData * cacheBlock(Map *map, v3s16 p, UpdateMode mode, size_t *cache_hit_counter=NULL)
Definition: mesh_generator_thread.cpp:159
Client * m_client
Definition: mesh_generator_thread.h:82
std::map< v3s16, CachedMapBlockData * > m_cache
Definition: mesh_generator_thread.h:85
int m_meshgen_block_cache_size
Definition: mesh_generator_thread.h:91
Definition: mesh_generator_thread.h:110
int m_generation_interval
Definition: mesh_generator_thread.h:125
virtual void doUpdate()
Definition: mesh_generator_thread.cpp:282
MeshUpdateQueue m_queue_in
Definition: mesh_generator_thread.h:122
MutexedQueue< MeshUpdateResult > m_queue_out
Definition: mesh_generator_thread.h:119
MeshUpdateThread(Client *client)
Definition: mesh_generator_thread.cpp:266
void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
Definition: mesh_generator_thread.cpp:274
v3s16 m_camera_offset
Definition: mesh_generator_thread.h:118
Definition: thread.h:189
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
std::unique_lock< std::mutex > MutexAutoLock
Definition: mutex_auto_lock.h:29
Definition: activeobjectmgr.cpp:25
Definition: mesh_generator_thread.h:29
MapNode * data
Definition: mesh_generator_thread.h:31
CachedMapBlockData()=default
int refcount_from_queue
Definition: mesh_generator_thread.h:32
~CachedMapBlockData()
Definition: mesh_generator_thread.cpp:31
std::time_t last_used_timestamp
Definition: mesh_generator_thread.h:33
v3s16 p
Definition: mesh_generator_thread.h:30
Definition: mapnode.h:118
Definition: mapblock_mesh.h:40
Definition: mesh_generator_thread.h:101
bool ack_block_to_server
Definition: mesh_generator_thread.h:104
MeshUpdateResult()=default
v3s16 p
Definition: mesh_generator_thread.h:102
MapBlockMesh * mesh
Definition: mesh_generator_thread.h:103
Definition: mesh_generator_thread.h:40
QueuedMeshUpdate()=default
~QueuedMeshUpdate()
Definition: mesh_generator_thread.cpp:42
int crack_level
Definition: mesh_generator_thread.h:43
v3s16 p
Definition: mesh_generator_thread.h:41
MeshMakeData * data
Definition: mesh_generator_thread.h:45
bool ack_block_to_server
Definition: mesh_generator_thread.h:42
v3s16 crack_pos
Definition: mesh_generator_thread.h:44
std::string p(std::string path)
Definition: test_filepath.cpp:59