2018-09-24 16:54:07 -07:00
|
|
|
#include "voxel_mesh_updater.h"
|
2019-04-28 09:58:29 -07:00
|
|
|
#include "../util/utility.h"
|
2019-05-03 16:02:10 -07:00
|
|
|
#include "voxel_lod_terrain.h"
|
2019-04-23 17:29:47 -07:00
|
|
|
#include <core/os/os.h>
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
VoxelMeshUpdater::VoxelMeshUpdater(Ref<VoxelLibrary> library, MeshingParams params) {
|
|
|
|
|
2019-05-03 15:59:21 -07:00
|
|
|
if (library.is_valid()) {
|
|
|
|
_blocky_mesher.instance();
|
|
|
|
_blocky_mesher->set_library(library);
|
|
|
|
_blocky_mesher->set_occlusion_enabled(params.baked_ao);
|
|
|
|
_blocky_mesher->set_occlusion_darkness(params.baked_ao_darkness);
|
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-04-27 17:32:23 -07:00
|
|
|
if (params.smooth_surface) {
|
|
|
|
_dmc_mesher.instance();
|
|
|
|
_dmc_mesher->set_geometric_error(0.05);
|
|
|
|
_dmc_mesher->set_octree_mode(VoxelMesherDMC::OCTREE_NONE);
|
2019-05-05 09:00:42 -07:00
|
|
|
_dmc_mesher->set_seam_mode(VoxelMesherDMC::SEAM_MARCHING_SQUARE_SKIRTS);
|
2019-04-27 17:32:23 -07:00
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
_input_mutex = Mutex::create();
|
|
|
|
_output_mutex = Mutex::create();
|
|
|
|
|
|
|
|
_thread_exit = false;
|
|
|
|
_semaphore = Semaphore::create();
|
|
|
|
_thread = Thread::create(_thread_func, this);
|
2018-09-29 09:52:38 -07:00
|
|
|
|
|
|
|
_needs_sort = true;
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelMeshUpdater::~VoxelMeshUpdater() {
|
|
|
|
|
|
|
|
_thread_exit = true;
|
|
|
|
_semaphore->post();
|
|
|
|
Thread::wait_to_finish(_thread);
|
|
|
|
memdelete(_thread);
|
|
|
|
memdelete(_semaphore);
|
|
|
|
memdelete(_input_mutex);
|
|
|
|
memdelete(_output_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelMeshUpdater::push(const Input &input) {
|
|
|
|
|
|
|
|
bool should_run = false;
|
|
|
|
int replaced_blocks = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexLock lock(_input_mutex);
|
|
|
|
|
2019-04-23 17:29:47 -07:00
|
|
|
for (int i = 0; i < input.blocks.size(); ++i) {
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
const InputBlock &block = input.blocks[i];
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
// If a block is exactly on the priority position, update it instantly on the main thread
|
|
|
|
// This is to eliminate latency for player's actions, assuming updating a block isn't slower than a frame
|
|
|
|
/*if (pos == _shared_input.priority_position) {
|
|
|
|
|
|
|
|
OutputBlock ob;
|
|
|
|
process_block(_shared_input.blocks[i], ob);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexLock lock2(_output_mutex);
|
|
|
|
_shared_output.blocks.push_back(ob);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}*/
|
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
CRASH_COND(block.lod >= MAX_LOD)
|
|
|
|
int *index = _block_indexes[block.lod].getptr(block.position);
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-04-23 17:29:47 -07:00
|
|
|
if (index) {
|
2018-09-24 16:54:07 -07:00
|
|
|
// The block is already in the update queue, replace it
|
|
|
|
++replaced_blocks;
|
2019-05-12 08:33:25 -07:00
|
|
|
_shared_input.blocks[*index] = block;
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
int j = _shared_input.blocks.size();
|
2019-05-03 16:02:10 -07:00
|
|
|
_shared_input.blocks.push_back(block);
|
|
|
|
_block_indexes[block.lod][block.position] = j;
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 17:29:47 -07:00
|
|
|
if (_shared_input.priority_position != input.priority_position || input.blocks.size() > 0) {
|
2018-09-29 09:52:38 -07:00
|
|
|
_needs_sort = true;
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:54:07 -07:00
|
|
|
_shared_input.priority_position = input.priority_position;
|
2019-05-12 08:33:25 -07:00
|
|
|
|
|
|
|
if (input.use_exclusive_region) {
|
|
|
|
_shared_input.use_exclusive_region = true;
|
|
|
|
_shared_input.exclusive_region_extent = input.exclusive_region_extent;
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:54:07 -07:00
|
|
|
should_run = !_shared_input.is_empty();
|
|
|
|
}
|
|
|
|
|
2019-04-27 17:34:00 -07:00
|
|
|
if (replaced_blocks > 0) {
|
2018-09-24 16:54:07 -07:00
|
|
|
print_line(String("VoxelMeshUpdater: {0} blocks already in queue were replaced").format(varray(replaced_blocks)));
|
2019-04-27 17:34:00 -07:00
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
if (should_run) {
|
|
|
|
_semaphore->post();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelMeshUpdater::pop(Output &output) {
|
|
|
|
|
|
|
|
MutexLock lock(_output_mutex);
|
|
|
|
|
|
|
|
output.blocks.append_array(_shared_output.blocks);
|
|
|
|
output.stats = _shared_output.stats;
|
|
|
|
_shared_output.blocks.clear();
|
|
|
|
}
|
|
|
|
|
2019-04-28 12:48:59 -07:00
|
|
|
int VoxelMeshUpdater::get_required_padding() const {
|
|
|
|
|
2019-05-03 15:59:21 -07:00
|
|
|
int padding = 0;
|
|
|
|
|
|
|
|
if (_blocky_mesher.is_valid()) {
|
|
|
|
padding = max(padding, _blocky_mesher->get_minimum_padding());
|
|
|
|
}
|
2019-04-28 12:48:59 -07:00
|
|
|
|
|
|
|
if (_dmc_mesher.is_valid()) {
|
|
|
|
padding = max(padding, _dmc_mesher->get_minimum_padding());
|
|
|
|
}
|
|
|
|
|
|
|
|
return padding;
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:54:07 -07:00
|
|
|
void VoxelMeshUpdater::_thread_func(void *p_self) {
|
2019-04-23 17:29:47 -07:00
|
|
|
VoxelMeshUpdater *self = reinterpret_cast<VoxelMeshUpdater *>(p_self);
|
2018-09-24 16:54:07 -07:00
|
|
|
self->thread_func();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelMeshUpdater::thread_func() {
|
|
|
|
|
|
|
|
while (!_thread_exit) {
|
|
|
|
|
|
|
|
uint32_t sync_interval = 50.0; // milliseconds
|
|
|
|
uint32_t sync_time = OS::get_singleton()->get_ticks_msec() + sync_interval;
|
|
|
|
|
|
|
|
int queue_index = 0;
|
|
|
|
Stats stats;
|
|
|
|
|
|
|
|
thread_sync(queue_index, stats);
|
|
|
|
|
|
|
|
while (!_input.blocks.empty() && !_thread_exit) {
|
|
|
|
|
|
|
|
if (!_input.blocks.empty()) {
|
|
|
|
|
|
|
|
InputBlock block = _input.blocks[queue_index];
|
|
|
|
++queue_index;
|
|
|
|
|
|
|
|
if (queue_index >= _input.blocks.size()) {
|
|
|
|
_input.blocks.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t time_before = OS::get_singleton()->get_ticks_usec();
|
|
|
|
|
|
|
|
OutputBlock ob;
|
|
|
|
process_block(block, ob);
|
|
|
|
|
|
|
|
uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - time_before;
|
|
|
|
|
|
|
|
// Do some stats
|
|
|
|
if (stats.first) {
|
|
|
|
stats.first = false;
|
|
|
|
stats.min_time = time_taken;
|
|
|
|
stats.max_time = time_taken;
|
|
|
|
} else {
|
2019-04-23 17:29:47 -07:00
|
|
|
if (time_taken < stats.min_time)
|
2018-09-24 16:54:07 -07:00
|
|
|
stats.min_time = time_taken;
|
2019-04-23 17:29:47 -07:00
|
|
|
if (time_taken > stats.max_time)
|
2018-09-24 16:54:07 -07:00
|
|
|
stats.max_time = time_taken;
|
|
|
|
}
|
|
|
|
|
|
|
|
_output.blocks.push_back(ob);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t time = OS::get_singleton()->get_ticks_msec();
|
2018-09-26 16:31:09 -07:00
|
|
|
if (time >= sync_time || _input.blocks.empty()) {
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
thread_sync(queue_index, stats);
|
|
|
|
|
|
|
|
sync_time = OS::get_singleton()->get_ticks_msec() + sync_interval;
|
|
|
|
queue_index = 0;
|
|
|
|
stats = Stats();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 17:34:00 -07:00
|
|
|
if (_thread_exit) {
|
2018-09-24 16:54:07 -07:00
|
|
|
break;
|
2019-04-27 17:34:00 -07:00
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
// Wait for future wake-up
|
|
|
|
_semaphore->wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
static void scale_mesh_data(VoxelMesher::Output &data, float factor) {
|
|
|
|
|
|
|
|
for (int i = 0; i < data.surfaces.size(); ++i) {
|
|
|
|
Array &surface = data.surfaces.write[i]; // There is COW here too but should not happen, hopefully
|
|
|
|
|
|
|
|
if (surface.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector3Array positions = surface[Mesh::ARRAY_VERTEX]; // Array of Variants here, implicit cast going on
|
|
|
|
|
|
|
|
// Now dear COW, let's make sure there is only ONE ref to that PoolVector,
|
|
|
|
// so you won't trash performance with pointless allocations
|
|
|
|
surface[Mesh::ARRAY_VERTEX] = Variant();
|
|
|
|
|
2019-05-12 08:33:25 -07:00
|
|
|
{
|
|
|
|
PoolVector3Array::Write w = positions.write();
|
|
|
|
int len = positions.size();
|
|
|
|
for (int j = 0; j < len; ++j) {
|
|
|
|
w[j] = w[j] * factor;
|
|
|
|
}
|
2019-05-03 16:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Thank you
|
|
|
|
surface[Mesh::ARRAY_VERTEX] = positions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:54:07 -07:00
|
|
|
void VoxelMeshUpdater::process_block(const InputBlock &block, OutputBlock &output) {
|
|
|
|
|
|
|
|
CRASH_COND(block.voxels.is_null());
|
|
|
|
|
2019-04-28 12:48:59 -07:00
|
|
|
int padding = get_required_padding();
|
2019-04-27 17:32:23 -07:00
|
|
|
|
2019-05-03 15:59:21 -07:00
|
|
|
if (_blocky_mesher.is_valid()) {
|
|
|
|
_blocky_mesher->build(output.blocky_surfaces, **block.voxels, padding);
|
|
|
|
}
|
2019-04-27 17:32:23 -07:00
|
|
|
|
|
|
|
if (_dmc_mesher.is_valid()) {
|
2019-04-28 12:48:59 -07:00
|
|
|
_dmc_mesher->build(output.smooth_surfaces, **block.voxels, padding);
|
2019-04-27 17:32:23 -07:00
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
output.position = block.position;
|
2019-05-03 16:02:10 -07:00
|
|
|
output.lod = block.lod;
|
|
|
|
|
|
|
|
if (block.lod > 0) {
|
|
|
|
float factor = 1 << block.lod;
|
|
|
|
scale_mesh_data(output.blocky_surfaces, factor);
|
|
|
|
scale_mesh_data(output.smooth_surfaces, factor);
|
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sorts distance to viewer
|
|
|
|
// The closest block will be the first one in the array
|
|
|
|
struct BlockUpdateComparator {
|
2019-05-12 08:19:08 -07:00
|
|
|
Vector3i center; // In LOD0 block coordinates
|
2018-09-24 16:54:07 -07:00
|
|
|
inline bool operator()(const VoxelMeshUpdater::InputBlock &a, const VoxelMeshUpdater::InputBlock &b) const {
|
2019-05-12 08:19:08 -07:00
|
|
|
if (a.lod == b.lod) {
|
|
|
|
int da = (a.position * (1 << a.lod)).distance_sq(center);
|
|
|
|
int db = (b.position * (1 << b.lod)).distance_sq(center);
|
|
|
|
return da < db;
|
|
|
|
} else {
|
|
|
|
// Load highest lods first because they are needed for the octree to subdivide
|
|
|
|
return a.lod > b.lod;
|
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void VoxelMeshUpdater::thread_sync(int queue_index, Stats stats) {
|
|
|
|
|
|
|
|
if (!_input.blocks.empty()) {
|
|
|
|
// Cleanup input vector
|
|
|
|
|
|
|
|
if (queue_index >= _input.blocks.size()) {
|
|
|
|
_input.blocks.clear();
|
|
|
|
|
|
|
|
} else if (queue_index > 0) {
|
|
|
|
|
|
|
|
// Shift up remaining items since we use a Vector
|
|
|
|
shift_up(_input.blocks, queue_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.remaining_blocks = _input.blocks.size();
|
2018-09-29 09:52:38 -07:00
|
|
|
bool needs_sort;
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
// Get input
|
|
|
|
MutexLock lock(_input_mutex);
|
|
|
|
|
2019-05-12 08:33:25 -07:00
|
|
|
append_array(_input.blocks, _shared_input.blocks);
|
|
|
|
|
2018-09-24 16:54:07 -07:00
|
|
|
_input.priority_position = _shared_input.priority_position;
|
|
|
|
|
2019-05-12 08:33:25 -07:00
|
|
|
if (_shared_input.use_exclusive_region) {
|
|
|
|
_input.use_exclusive_region = true;
|
|
|
|
_input.exclusive_region_extent = _shared_input.exclusive_region_extent;
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:54:07 -07:00
|
|
|
_shared_input.blocks.clear();
|
2019-05-03 16:02:10 -07:00
|
|
|
|
|
|
|
for (unsigned int lod_index = 0; lod_index < MAX_LOD; ++lod_index) {
|
|
|
|
_block_indexes[lod_index].clear();
|
|
|
|
}
|
2018-09-29 09:52:38 -07:00
|
|
|
|
|
|
|
needs_sort = _needs_sort;
|
|
|
|
_needs_sort = false;
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
|
|
|
|
2019-04-23 17:29:47 -07:00
|
|
|
if (!_output.blocks.empty()) {
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-04-23 17:29:47 -07:00
|
|
|
// print_line(String("VoxelMeshUpdater: posting {0} blocks, {1} remaining ; cost [{2}..{3}] usec")
|
|
|
|
// .format(varray(_output.blocks.size(), _input.blocks.size(), stats.min_time, stats.max_time)));
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
// Post output
|
|
|
|
MutexLock lock(_output_mutex);
|
|
|
|
_shared_output.blocks.append_array(_output.blocks);
|
|
|
|
_shared_output.stats = stats;
|
|
|
|
_output.blocks.clear();
|
|
|
|
}
|
|
|
|
|
2019-05-12 08:33:25 -07:00
|
|
|
// Cancel blocks outside exclusive region
|
|
|
|
//int dropped_count = 0;
|
|
|
|
if (_input.use_exclusive_region) {
|
|
|
|
for (int i = 0; i < _input.blocks.size(); ++i) {
|
|
|
|
const InputBlock &ib = _input.blocks[i];
|
|
|
|
|
|
|
|
Rect3i box = Rect3i::from_center_extents(_input.priority_position >> ib.lod, Vector3i(_input.exclusive_region_extent));
|
|
|
|
|
|
|
|
if (!box.contains(ib.position)) {
|
|
|
|
|
|
|
|
Vector3i shifted_block_pos = _input.blocks.back().position;
|
|
|
|
|
|
|
|
_input.blocks[i] = _input.blocks.back();
|
|
|
|
_input.blocks.pop_back();
|
|
|
|
|
|
|
|
_block_indexes[ib.lod].erase(ib.position);
|
|
|
|
_block_indexes[ib.lod][shifted_block_pos] = i;
|
|
|
|
|
|
|
|
//++dropped_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (dropped_count > 0) {
|
|
|
|
// print_line(String("Dropped {0} blocks to mesh from thread").format(varray(dropped_count)));
|
|
|
|
// }
|
|
|
|
|
2018-09-29 09:52:38 -07:00
|
|
|
if (!_input.blocks.empty() && needs_sort) {
|
2018-09-24 16:54:07 -07:00
|
|
|
// Re-sort priority
|
|
|
|
|
|
|
|
SortArray<VoxelMeshUpdater::InputBlock, BlockUpdateComparator> sorter;
|
|
|
|
sorter.compare.center = _input.priority_position;
|
2019-05-12 08:33:25 -07:00
|
|
|
sorter.sort(_input.blocks.data(), _input.blocks.size());
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
|
|
|
}
|
2019-05-04 17:09:12 -07:00
|
|
|
|
|
|
|
Dictionary VoxelMeshUpdater::to_dictionary(const Stats &stats) {
|
|
|
|
Dictionary d;
|
|
|
|
d["min_time"] = stats.min_time;
|
|
|
|
d["max_time"] = stats.max_time;
|
|
|
|
d["remaining_blocks"] = stats.remaining_blocks;
|
|
|
|
return d;
|
|
|
|
}
|