Luanti 5.19.0-dev
Loading...
Searching...
No Matches
mesh_generator_thread.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <ctime>
8#include <mutex>
9#include <unordered_set>
12#include "util/thread.h"
13#include <vector>
14#include <memory>
15
16class Map;
17class MapBlock;
18class MapBlockMesh;
19struct MeshMakeData;
20struct MeshUpdateResult;
21class Client;
22
24{
25 v3s16 p = v3s16(-1337, -1337, -1337);
26 int crack_level = -1;
28 MeshMakeData *data = nullptr; // This is generated in MeshUpdateQueue::pop()
29 std::vector<MapBlock*> map_blocks;
31 bool urgent = false;
32
33 QueuedMeshUpdate() = default;
35
42 void retrieveBlocks(Map *map, u16 cell_size);
47 void dropBlocks();
54 bool checkSkip(u16 cell_size);
55};
56
57/*
58 A thread-safe queue of mesh update tasks and a cache of MapBlock data
59*/
61{
62public:
64
66
75 bool addBlock(Map *map, v3s16 p, bool urgent, bool from_neighbor);
76
81
82 // Update internal epoch counter
83 void setEpoch(u16 epoch) {
85 m_epoch = epoch;
86 }
87
88 // Returned pointer must be deleted
89 // Returns NULL if queue is empty
91
92 // Marks a position as finished, unblocking the next update
93 void done(v3s16 pos);
94
95 size_t size()
96 {
98 return m_queue.size();
99 }
100
101 void clear();
102
103private:
105 std::vector<QueuedMeshUpdate *> m_queue;
106 std::unordered_set<v3s16> m_urgents;
107 std::unordered_set<v3s16> m_inflight_blocks;
108 u16 m_epoch = 0;
109 std::mutex m_mutex;
110
111 // TODO: Add callback to update these when g_settings changes, and update all meshes
115
117};
118
120{
121 v3s16 p = v3s16(-1338, -1338, -1338);
122 std::unique_ptr<MapBlockMesh> mesh;
124 std::vector<MapBlock*> map_blocks;
126 bool urgent = false;
127
128 MeshUpdateResult() = default;
129
134 void dropBlocks();
135};
136
138
140{
141public:
143
144protected:
145 virtual void doUpdate();
146
147private:
151
152 // TODO: Add callback to update these when g_settings changes
154};
155
157{
158public:
160
161 // Caches the block at p and its neighbors (if needed) and queues a mesh
162 // update for the block at p
163 void updateBlock(Map *map, v3s16 p, bool urgent,
164 bool update_neighbors = false);
165
166 void putResult(MeshUpdateResult &&r);
167
170
177 void forceRegenerate(Map *map, const std::vector<v3s16> &pp);
178
179 void clearAllQueues();
180
181 void start();
182 void stop();
183 void wait();
184
185 bool isRunning();
186
187private:
189
190 void deferUpdate();
191
192 // Epoch counter: used to identify outdated mesh results
193 u16 m_epoch = 0;
194
198
199 std::vector<std::unique_ptr<MeshUpdateWorkerThread>> m_workers;
200};
Definition client.h:108
Definition mapblock_mesh.h:179
Definition mapblock.h:58
Definition map.h:105
Definition mesh_generator_thread.h:157
void updateBlock(Map *map, v3s16 p, bool urgent, bool update_neighbors=false)
Definition mesh_generator_thread.cpp:351
MeshUpdateManager(Client *client)
Definition mesh_generator_thread.cpp:334
void deferUpdate()
Definition mesh_generator_thread.cpp:430
void forceRegenerate(Map *map, const std::vector< v3s16 > &pp)
Forces all in-progress meshes to be re-generated and adds all passed positions to the queue.
Definition mesh_generator_thread.cpp:401
bool isRunning()
Definition mesh_generator_thread.cpp:454
u16 m_epoch
Definition mesh_generator_thread.h:193
MeshUpdateQueue m_queue_in
Definition mesh_generator_thread.h:195
void stop()
Definition mesh_generator_thread.cpp:442
ResultQueue m_queue_out_urgent
Definition mesh_generator_thread.h:197
bool getNextResult(MeshUpdateResult &r)
Definition mesh_generator_thread.cpp:381
void putResult(MeshUpdateResult &&r)
Definition mesh_generator_thread.cpp:373
void clearAllQueues()
Definition mesh_generator_thread.cpp:415
ResultQueue m_queue_out
Definition mesh_generator_thread.h:196
void start()
Definition mesh_generator_thread.cpp:436
std::vector< std::unique_ptr< MeshUpdateWorkerThread > > m_workers
Definition mesh_generator_thread.h:199
void wait()
Definition mesh_generator_thread.cpp:448
MutexedQueue< MeshUpdateResult > ResultQueue
Definition mesh_generator_thread.h:188
Definition mesh_generator_thread.h:61
bool addBlock(Map *map, v3s16 p, bool urgent, bool from_neighbor)
Caches the block at p and its neighbors (if needed) and queues a mesh update for the block p.
Definition mesh_generator_thread.cpp:103
std::unordered_set< v3s16 > m_urgents
Definition mesh_generator_thread.h:106
void setEpoch(u16 epoch)
Definition mesh_generator_thread.h:83
void reAddBlock(MeshUpdateResult &r)
Re-queues a block that was already finished.
Definition mesh_generator_thread.cpp:164
std::mutex m_mutex
Definition mesh_generator_thread.h:109
std::vector< QueuedMeshUpdate * > m_queue
Definition mesh_generator_thread.h:105
bool m_cache_enable_waving_water
Definition mesh_generator_thread.h:114
void done(v3s16 pos)
Definition mesh_generator_thread.cpp:231
MeshUpdateQueue(Client *client)
Definition mesh_generator_thread.cpp:89
void fillDataFromMapBlocks(QueuedMeshUpdate *q)
Definition mesh_generator_thread.cpp:249
bool m_cache_smooth_lighting
Definition mesh_generator_thread.h:112
bool m_cache_enable_water_reflections
Definition mesh_generator_thread.h:113
size_t size()
Definition mesh_generator_thread.h:95
QueuedMeshUpdate * pop()
Definition mesh_generator_thread.cpp:198
std::unordered_set< v3s16 > m_inflight_blocks
Definition mesh_generator_thread.h:107
Client * m_client
Definition mesh_generator_thread.h:104
u16 m_epoch
Definition mesh_generator_thread.h:108
void clear()
Definition mesh_generator_thread.cpp:237
MeshUpdateManager * m_manager
Definition mesh_generator_thread.h:150
MeshUpdateQueue * m_queue_in
Definition mesh_generator_thread.h:149
Client * m_client
Definition mesh_generator_thread.h:148
int m_generation_interval
Definition mesh_generator_thread.h:153
MeshUpdateWorkerThread(Client *client, MeshUpdateQueue *queue_in, MeshUpdateManager *manager)
Definition mesh_generator_thread.cpp:292
virtual void doUpdate()
Definition mesh_generator_thread.cpp:299
Definition container.h:126
UpdateThread(const std::string &name)
Definition thread.h:177
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
#define U16_MAX
Definition irrlichttypes.h:22
std::lock_guard< std::mutex > MutexAutoLock
Definition mutex_auto_lock.h:31
Definition activeobjectmgr.cpp:11
Definition mapblock_mesh.h:35
Definition mesh_generator_thread.h:120
std::vector< MapBlock * > map_blocks
Definition mesh_generator_thread.h:124
void dropBlocks()
Drop block references.
Definition mesh_generator_thread.cpp:279
bool urgent
Definition mesh_generator_thread.h:126
u8 solid_sides
Definition mesh_generator_thread.h:123
MeshUpdateResult()=default
v3s16 p
Definition mesh_generator_thread.h:121
std::unique_ptr< MapBlockMesh > mesh
Definition mesh_generator_thread.h:122
u16 epoch
Definition mesh_generator_thread.h:125
Definition mesh_generator_thread.h:24
void dropBlocks()
Drop block references.
Definition mesh_generator_thread.cpp:63
QueuedMeshUpdate()=default
void retrieveBlocks(Map *map, u16 cell_size)
Get blocks needed for this mesh update from the map.
Definition mesh_generator_thread.cpp:24
int crack_level
Definition mesh_generator_thread.h:26
v3s16 p
Definition mesh_generator_thread.h:25
std::vector< MapBlock * > map_blocks
Definition mesh_generator_thread.h:29
MeshMakeData * data
Definition mesh_generator_thread.h:28
u16 epoch
Definition mesh_generator_thread.h:30
bool urgent
Definition mesh_generator_thread.h:31
v3s16 crack_pos
Definition mesh_generator_thread.h:27
bool checkSkip(u16 cell_size)
Check if the blocks that would comprise the mesh are all air, so the update can be skipped entirely.
Definition mesh_generator_thread.cpp:49