842 lines
27 KiB
C++
842 lines
27 KiB
C++
#include "voxel_server.h"
|
|
#include "../meshers/transvoxel/voxel_mesher_transvoxel.h"
|
|
#include "../util/macros.h"
|
|
#include "../util/profiling.h"
|
|
#include "../voxel_constants.h"
|
|
#include <core/os/memory.h>
|
|
#include <scene/main/viewport.h>
|
|
#include <thread>
|
|
|
|
namespace {
|
|
VoxelServer *g_voxel_server = nullptr;
|
|
}
|
|
|
|
template <typename Dst_T>
|
|
inline Dst_T *must_be_cast(IVoxelTask *src) {
|
|
#ifdef TOOLS_ENABLED
|
|
Dst_T *dst = dynamic_cast<Dst_T *>(src);
|
|
CRASH_COND_MSG(dst == nullptr, "Invalid cast");
|
|
return dst;
|
|
#else
|
|
return static_cast<Dst_T *>(src);
|
|
#endif
|
|
}
|
|
|
|
template <typename T>
|
|
inline std::shared_ptr<T> gd_make_shared() {
|
|
// std::make_shared() apparently wont allow us to specify custom new and delete
|
|
return std::shared_ptr<T>(memnew(T), memdelete<T>);
|
|
}
|
|
|
|
VoxelServer *VoxelServer::get_singleton() {
|
|
CRASH_COND_MSG(g_voxel_server == nullptr, "Accessing singleton while it's null");
|
|
return g_voxel_server;
|
|
}
|
|
|
|
void VoxelServer::create_singleton() {
|
|
CRASH_COND_MSG(g_voxel_server != nullptr, "Creating singleton twice");
|
|
g_voxel_server = memnew(VoxelServer);
|
|
}
|
|
|
|
void VoxelServer::destroy_singleton() {
|
|
CRASH_COND_MSG(g_voxel_server == nullptr, "Destroying singleton twice");
|
|
memdelete(g_voxel_server);
|
|
g_voxel_server = nullptr;
|
|
}
|
|
|
|
VoxelServer::VoxelServer() {
|
|
const unsigned int hw_threads_hint = std::thread::hardware_concurrency();
|
|
PRINT_VERBOSE(String("HW threads hint: {0}").format(varray(hw_threads_hint)));
|
|
// TODO Automatic thread assignment and project settings
|
|
|
|
// Can't be more than 1 thread. File access with more threads isn't worth it.
|
|
_streaming_thread_pool.set_thread_count(1);
|
|
_streaming_thread_pool.set_priority_update_period(300);
|
|
_streaming_thread_pool.set_batch_count(16);
|
|
|
|
_generation_thread_pool.set_thread_count(2);
|
|
_generation_thread_pool.set_priority_update_period(300);
|
|
_generation_thread_pool.set_batch_count(1);
|
|
|
|
// This pool works on visuals so it must have lower latency
|
|
_meshing_thread_pool.set_thread_count(2);
|
|
_meshing_thread_pool.set_priority_update_period(64);
|
|
_meshing_thread_pool.set_batch_count(1);
|
|
|
|
// Init world
|
|
_world.shared_priority_dependency = gd_make_shared<PriorityDependencyShared>();
|
|
|
|
PRINT_VERBOSE(String("Size of BlockDataRequest: {0}").format(varray((int)sizeof(BlockDataRequest))));
|
|
PRINT_VERBOSE(String("Size of BlockMeshRequest: {0}").format(varray((int)sizeof(BlockMeshRequest))));
|
|
}
|
|
|
|
VoxelServer::~VoxelServer() {
|
|
// The GDScriptLanguage singleton can get destroyed before ours, so any script referenced by tasks
|
|
// cannot be freed. To work this around, tasks are cleared when the scene tree autoload is destroyed.
|
|
// So normally there should not be any task left to clear here,
|
|
// but doing it anyways for correctness, it's how it should have been...
|
|
// See https://github.com/Zylann/godot_voxel/issues/189
|
|
wait_and_clear_all_tasks(true);
|
|
}
|
|
|
|
void VoxelServer::wait_and_clear_all_tasks(bool warn) {
|
|
_streaming_thread_pool.wait_for_all_tasks();
|
|
_generation_thread_pool.wait_for_all_tasks();
|
|
|
|
// Wait a second time because the generation pool can generate streaming requests
|
|
_streaming_thread_pool.wait_for_all_tasks();
|
|
|
|
_meshing_thread_pool.wait_for_all_tasks();
|
|
|
|
_streaming_thread_pool.dequeue_completed_tasks([warn](IVoxelTask *task) {
|
|
if (warn) {
|
|
WARN_PRINT("Streaming tasks remain on module cleanup, "
|
|
"this could become a problem if they reference scripts");
|
|
}
|
|
memdelete(task);
|
|
});
|
|
|
|
_meshing_thread_pool.dequeue_completed_tasks([](IVoxelTask *task) {
|
|
memdelete(task);
|
|
});
|
|
|
|
_generation_thread_pool.dequeue_completed_tasks([warn](IVoxelTask *task) {
|
|
if (warn) {
|
|
WARN_PRINT("Generator tasks remain on module cleanup, "
|
|
"this could become a problem if they reference scripts");
|
|
}
|
|
memdelete(task);
|
|
});
|
|
}
|
|
|
|
int VoxelServer::get_priority(const PriorityDependency &dep, uint8_t lod, float *out_closest_distance_sq) {
|
|
const std::vector<Vector3> &viewer_positions = dep.shared->viewers;
|
|
const Vector3 block_position = dep.world_position;
|
|
|
|
float closest_distance_sq = 99999.f;
|
|
if (viewer_positions.size() == 0) {
|
|
// Assume origin
|
|
closest_distance_sq = block_position.length_squared();
|
|
} else {
|
|
for (size_t i = 0; i < viewer_positions.size(); ++i) {
|
|
float d = viewer_positions[i].distance_squared_to(block_position);
|
|
if (d < closest_distance_sq) {
|
|
closest_distance_sq = d;
|
|
}
|
|
}
|
|
}
|
|
|
|
int priority = static_cast<int>(closest_distance_sq);
|
|
|
|
if (out_closest_distance_sq != nullptr) {
|
|
*out_closest_distance_sq = closest_distance_sq;
|
|
}
|
|
|
|
// Higher lod indexes come first to allow the octree to subdivide.
|
|
// Then comes distance, which is modified by how much in view the block is
|
|
priority += (VoxelConstants::MAX_LOD - lod) * 10000;
|
|
|
|
return priority;
|
|
}
|
|
|
|
uint32_t VoxelServer::add_volume(ReceptionBuffers *buffers, VolumeType type) {
|
|
CRASH_COND(buffers == nullptr);
|
|
Volume volume;
|
|
volume.type = type;
|
|
volume.reception_buffers = buffers;
|
|
volume.meshing_dependency = gd_make_shared<MeshingDependency>();
|
|
return _world.volumes.create(volume);
|
|
}
|
|
|
|
void VoxelServer::set_volume_transform(uint32_t volume_id, Transform t) {
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
volume.transform = t;
|
|
}
|
|
|
|
void VoxelServer::set_volume_block_size(uint32_t volume_id, uint32_t block_size) {
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
volume.block_size = block_size;
|
|
}
|
|
|
|
void VoxelServer::set_volume_stream(uint32_t volume_id, Ref<VoxelStream> stream) {
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
volume.stream = stream;
|
|
|
|
// Commit a new dependency to process requests with
|
|
if (volume.stream_dependency != nullptr) {
|
|
volume.stream_dependency->valid = false;
|
|
}
|
|
|
|
volume.stream_dependency = gd_make_shared<StreamingDependency>();
|
|
volume.stream_dependency->generator = volume.generator;
|
|
volume.stream_dependency->stream = volume.stream;
|
|
}
|
|
|
|
void VoxelServer::set_volume_generator(uint32_t volume_id, Ref<VoxelGenerator> generator) {
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
volume.generator = generator;
|
|
|
|
// Commit a new dependency to process requests with
|
|
if (volume.stream_dependency != nullptr) {
|
|
volume.stream_dependency->valid = false;
|
|
}
|
|
|
|
volume.stream_dependency = gd_make_shared<StreamingDependency>();
|
|
volume.stream_dependency->generator = volume.generator;
|
|
volume.stream_dependency->stream = volume.stream;
|
|
}
|
|
|
|
void VoxelServer::set_volume_mesher(uint32_t volume_id, Ref<VoxelMesher> mesher) {
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
volume.mesher = mesher;
|
|
volume.meshing_dependency = gd_make_shared<MeshingDependency>();
|
|
volume.meshing_dependency->mesher = volume.mesher;
|
|
}
|
|
|
|
void VoxelServer::set_volume_octree_split_scale(uint32_t volume_id, float split_scale) {
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
volume.octree_split_scale = split_scale;
|
|
}
|
|
|
|
void VoxelServer::invalidate_volume_mesh_requests(uint32_t volume_id) {
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
volume.meshing_dependency->valid = false;
|
|
volume.meshing_dependency = gd_make_shared<MeshingDependency>();
|
|
volume.meshing_dependency->mesher = volume.mesher;
|
|
}
|
|
|
|
static inline Vector3i get_block_center(Vector3i pos, int bs, int lod) {
|
|
return (pos << lod) * bs + Vector3i(bs / 2);
|
|
}
|
|
|
|
void VoxelServer::init_priority_dependency(
|
|
VoxelServer::PriorityDependency &dep, Vector3i block_position, uint8_t lod, const Volume &volume) {
|
|
|
|
const Vector3i voxel_pos = get_block_center(block_position, volume.block_size, lod);
|
|
const float block_radius = (volume.block_size << lod) / 2;
|
|
dep.shared = _world.shared_priority_dependency;
|
|
dep.world_position = volume.transform.xform(voxel_pos.to_vec3());
|
|
const float transformed_block_radius =
|
|
volume.transform.basis.xform(Vector3(block_radius, block_radius, block_radius)).length();
|
|
|
|
switch (volume.type) {
|
|
case VOLUME_SPARSE_GRID:
|
|
// Distance beyond which no field of view can overlap the block
|
|
dep.drop_distance_squared =
|
|
squared(_world.shared_priority_dependency->highest_view_distance + transformed_block_radius);
|
|
break;
|
|
|
|
case VOLUME_SPARSE_OCTREE:
|
|
// Distance beyond which it is safe to drop a block without risking to block LOD subdivision.
|
|
// This does not depend on viewer's view distance, but on LOD precision instead.
|
|
dep.drop_distance_squared = squared(2.f * transformed_block_radius *
|
|
get_octree_lod_block_region_extent(volume.octree_split_scale));
|
|
break;
|
|
|
|
default:
|
|
CRASH_NOW_MSG("Unexpected type");
|
|
break;
|
|
}
|
|
}
|
|
|
|
void VoxelServer::request_block_mesh(uint32_t volume_id, BlockMeshInput &input) {
|
|
const Volume &volume = _world.volumes.get(volume_id);
|
|
ERR_FAIL_COND(volume.meshing_dependency == nullptr);
|
|
|
|
BlockMeshRequest *r = memnew(BlockMeshRequest);
|
|
r->volume_id = volume_id;
|
|
r->blocks = input.blocks;
|
|
r->position = input.position;
|
|
r->lod = input.lod;
|
|
r->meshing_dependency = volume.meshing_dependency;
|
|
|
|
init_priority_dependency(r->priority_dependency, input.position, input.lod, volume);
|
|
|
|
// We'll allocate this quite often. If it becomes a problem, it should be easy to pool.
|
|
_meshing_thread_pool.enqueue(r);
|
|
}
|
|
|
|
void VoxelServer::request_block_load(uint32_t volume_id, Vector3i block_pos, int lod) {
|
|
const Volume &volume = _world.volumes.get(volume_id);
|
|
ERR_FAIL_COND(volume.stream_dependency == nullptr);
|
|
|
|
if (volume.stream_dependency->stream.is_valid()) {
|
|
BlockDataRequest r;
|
|
r.volume_id = volume_id;
|
|
r.position = block_pos;
|
|
r.lod = lod;
|
|
r.type = BlockDataRequest::TYPE_LOAD;
|
|
r.block_size = volume.block_size;
|
|
r.stream_dependency = volume.stream_dependency;
|
|
|
|
init_priority_dependency(r.priority_dependency, block_pos, lod, volume);
|
|
|
|
BlockDataRequest *rp = memnew(BlockDataRequest(r));
|
|
_streaming_thread_pool.enqueue(rp);
|
|
|
|
} else {
|
|
// Directly generate the block without checking the stream
|
|
ERR_FAIL_COND(volume.stream_dependency->generator.is_null());
|
|
|
|
BlockGenerateRequest r;
|
|
r.volume_id = volume_id;
|
|
r.position = block_pos;
|
|
r.lod = lod;
|
|
r.block_size = volume.block_size;
|
|
r.stream_dependency = volume.stream_dependency;
|
|
|
|
init_priority_dependency(r.priority_dependency, block_pos, lod, volume);
|
|
|
|
BlockGenerateRequest *rp = memnew(BlockGenerateRequest(r));
|
|
_generation_thread_pool.enqueue(rp);
|
|
}
|
|
}
|
|
|
|
void VoxelServer::request_block_save(uint32_t volume_id, Ref<VoxelBuffer> voxels, Vector3i block_pos, int lod) {
|
|
const Volume &volume = _world.volumes.get(volume_id);
|
|
ERR_FAIL_COND(volume.stream.is_null());
|
|
CRASH_COND(volume.stream_dependency == nullptr);
|
|
|
|
BlockDataRequest r;
|
|
r.voxels = voxels;
|
|
r.volume_id = volume_id;
|
|
r.position = block_pos;
|
|
r.lod = lod;
|
|
r.type = BlockDataRequest::TYPE_SAVE;
|
|
r.block_size = volume.block_size;
|
|
r.stream_dependency = volume.stream_dependency;
|
|
|
|
// No priority data, saving doesnt need sorting
|
|
|
|
BlockDataRequest *rp = memnew(BlockDataRequest(r));
|
|
_streaming_thread_pool.enqueue(rp);
|
|
}
|
|
|
|
void VoxelServer::request_block_generate_from_data_request(BlockDataRequest *src) {
|
|
// This can be called from another thread
|
|
|
|
BlockGenerateRequest r;
|
|
r.voxels = src->voxels;
|
|
r.volume_id = src->volume_id;
|
|
r.position = src->position;
|
|
r.lod = src->lod;
|
|
r.block_size = src->block_size;
|
|
r.stream_dependency = src->stream_dependency;
|
|
r.priority_dependency = src->priority_dependency;
|
|
|
|
BlockGenerateRequest *rp = memnew(BlockGenerateRequest(r));
|
|
_generation_thread_pool.enqueue(rp);
|
|
}
|
|
|
|
void VoxelServer::request_block_save_from_generate_request(BlockGenerateRequest *src) {
|
|
// This can be called from another thread
|
|
|
|
PRINT_VERBOSE(String("Requesting save of generator output for block {0} lod {1}")
|
|
.format(varray(src->position.to_vec3(), src->lod)));
|
|
|
|
ERR_FAIL_COND(src->voxels.is_null());
|
|
|
|
BlockDataRequest r;
|
|
r.voxels = src->voxels->duplicate(true);
|
|
r.volume_id = src->volume_id;
|
|
r.position = src->position;
|
|
r.lod = src->lod;
|
|
r.type = BlockDataRequest::TYPE_SAVE;
|
|
r.block_size = src->block_size;
|
|
r.stream_dependency = src->stream_dependency;
|
|
|
|
// No priority data, saving doesnt need sorting
|
|
|
|
BlockDataRequest *rp = memnew(BlockDataRequest(r));
|
|
_streaming_thread_pool.enqueue(rp);
|
|
}
|
|
|
|
void VoxelServer::remove_volume(uint32_t volume_id) {
|
|
{
|
|
Volume &volume = _world.volumes.get(volume_id);
|
|
if (volume.stream_dependency != nullptr) {
|
|
volume.stream_dependency->valid = false;
|
|
}
|
|
if (volume.meshing_dependency != nullptr) {
|
|
volume.meshing_dependency->valid = false;
|
|
}
|
|
}
|
|
|
|
_world.volumes.destroy(volume_id);
|
|
// TODO How to cancel meshing tasks?
|
|
|
|
if (_world.volumes.count() == 0) {
|
|
// To workaround https://github.com/Zylann/godot_voxel/issues/189
|
|
// When the last remaining volume got destroyed (as in game exit)
|
|
wait_and_clear_all_tasks(false);
|
|
}
|
|
}
|
|
|
|
uint32_t VoxelServer::add_viewer() {
|
|
return _world.viewers.create(Viewer());
|
|
}
|
|
|
|
void VoxelServer::remove_viewer(uint32_t viewer_id) {
|
|
_world.viewers.destroy(viewer_id);
|
|
}
|
|
|
|
void VoxelServer::set_viewer_position(uint32_t viewer_id, Vector3 position) {
|
|
Viewer &viewer = _world.viewers.get(viewer_id);
|
|
viewer.world_position = position;
|
|
}
|
|
|
|
void VoxelServer::set_viewer_distance(uint32_t viewer_id, unsigned int distance) {
|
|
Viewer &viewer = _world.viewers.get(viewer_id);
|
|
viewer.view_distance = distance;
|
|
}
|
|
|
|
unsigned int VoxelServer::get_viewer_distance(uint32_t viewer_id) const {
|
|
const Viewer &viewer = _world.viewers.get(viewer_id);
|
|
return viewer.view_distance;
|
|
}
|
|
|
|
void VoxelServer::set_viewer_requires_visuals(uint32_t viewer_id, bool enabled) {
|
|
Viewer &viewer = _world.viewers.get(viewer_id);
|
|
viewer.require_visuals = enabled;
|
|
}
|
|
|
|
bool VoxelServer::is_viewer_requiring_visuals(uint32_t viewer_id) const {
|
|
const Viewer &viewer = _world.viewers.get(viewer_id);
|
|
return viewer.require_visuals;
|
|
}
|
|
|
|
void VoxelServer::set_viewer_requires_collisions(uint32_t viewer_id, bool enabled) {
|
|
Viewer &viewer = _world.viewers.get(viewer_id);
|
|
viewer.require_collisions = enabled;
|
|
}
|
|
|
|
bool VoxelServer::is_viewer_requiring_collisions(uint32_t viewer_id) const {
|
|
const Viewer &viewer = _world.viewers.get(viewer_id);
|
|
return viewer.require_collisions;
|
|
}
|
|
|
|
bool VoxelServer::viewer_exists(uint32_t viewer_id) const {
|
|
return _world.viewers.is_valid(viewer_id);
|
|
}
|
|
|
|
void VoxelServer::process() {
|
|
// Note, this shouldn't be here. It should normally done just after SwapBuffers.
|
|
// Godot does not have any C++ profiler usage anywhere, so when using Tracy Profiler I have to put it somewhere...
|
|
VOXEL_PROFILE_MARK_FRAME();
|
|
VOXEL_PROFILE_SCOPE();
|
|
|
|
// Receive data updates
|
|
_streaming_thread_pool.dequeue_completed_tasks([this](IVoxelTask *task) {
|
|
BlockDataRequest *r = must_be_cast<BlockDataRequest>(task);
|
|
Volume *volume = _world.volumes.try_get(r->volume_id);
|
|
|
|
if (volume != nullptr) {
|
|
// TODO Comparing pointer may not be guaranteed
|
|
// The request response must match the dependency it would have been requested with.
|
|
// If it doesn't match, we are no longer interested in the result.
|
|
if (r->stream_dependency == volume->stream_dependency &&
|
|
r->type != BlockDataRequest::TYPE_FALLBACK_ON_GENERATOR) {
|
|
BlockDataOutput o;
|
|
o.voxels = r->voxels;
|
|
o.position = r->position;
|
|
o.lod = r->lod;
|
|
o.dropped = !r->has_run;
|
|
|
|
switch (r->type) {
|
|
case BlockDataRequest::TYPE_SAVE:
|
|
o.type = BlockDataOutput::TYPE_SAVE;
|
|
break;
|
|
|
|
case BlockDataRequest::TYPE_LOAD:
|
|
o.type = BlockDataOutput::TYPE_LOAD;
|
|
break;
|
|
|
|
default:
|
|
CRASH_NOW_MSG("Unexpected data request response type");
|
|
}
|
|
|
|
volume->reception_buffers->data_output.push_back(o);
|
|
}
|
|
|
|
} else {
|
|
// This can happen if the user removes the volume while requests are still about to return
|
|
PRINT_VERBOSE("Stream data request response came back but volume wasn't found");
|
|
}
|
|
|
|
memdelete(r);
|
|
});
|
|
|
|
// Receive generation updates
|
|
_generation_thread_pool.dequeue_completed_tasks([this](IVoxelTask *task) {
|
|
BlockGenerateRequest *r = must_be_cast<BlockGenerateRequest>(task);
|
|
Volume *volume = _world.volumes.try_get(r->volume_id);
|
|
|
|
if (volume != nullptr) {
|
|
// TODO Comparing pointer may not be guaranteed
|
|
// The request response must match the dependency it would have been requested with.
|
|
// If it doesn't match, we are no longer interested in the result.
|
|
if (r->stream_dependency == volume->stream_dependency) {
|
|
BlockDataOutput o;
|
|
o.voxels = r->voxels;
|
|
o.position = r->position;
|
|
o.lod = r->lod;
|
|
o.dropped = !r->has_run;
|
|
o.type = BlockDataOutput::TYPE_LOAD;
|
|
volume->reception_buffers->data_output.push_back(o);
|
|
}
|
|
|
|
} else {
|
|
// This can happen if the user removes the volume while requests are still about to return
|
|
PRINT_VERBOSE("Gemerated data request response came back but volume wasn't found");
|
|
}
|
|
|
|
memdelete(r);
|
|
});
|
|
|
|
// Receive mesh updates
|
|
_meshing_thread_pool.dequeue_completed_tasks([this](IVoxelTask *task) {
|
|
BlockMeshRequest *r = must_be_cast<BlockMeshRequest>(task);
|
|
Volume *volume = _world.volumes.try_get(r->volume_id);
|
|
|
|
if (volume != nullptr) {
|
|
// TODO Comparing pointer may not be guaranteed
|
|
// The request response must match the dependency it would have been requested with.
|
|
// If it doesn't match, we are no longer interested in the result.
|
|
if (volume->meshing_dependency == r->meshing_dependency) {
|
|
|
|
BlockMeshOutput o;
|
|
// TODO Check for invalidation due to property changes
|
|
|
|
if (r->has_run) {
|
|
o.type = BlockMeshOutput::TYPE_MESHED;
|
|
} else {
|
|
o.type = BlockMeshOutput::TYPE_DROPPED;
|
|
}
|
|
|
|
o.position = r->position;
|
|
o.lod = r->lod;
|
|
o.surfaces = r->surfaces_output;
|
|
|
|
volume->reception_buffers->mesh_output.push_back(o);
|
|
}
|
|
|
|
} else {
|
|
// This can happen if the user removes the volume while requests are still about to return
|
|
PRINT_VERBOSE("Mesh request response came back but volume wasn't found");
|
|
}
|
|
|
|
memdelete(r);
|
|
});
|
|
|
|
// Update viewer dependencies
|
|
{
|
|
const size_t viewer_count = _world.viewers.count();
|
|
if (_world.shared_priority_dependency->viewers.size() != viewer_count) {
|
|
// TODO We can avoid the invalidation by using an atomic size or memory barrier?
|
|
_world.shared_priority_dependency = gd_make_shared<PriorityDependencyShared>();
|
|
_world.shared_priority_dependency->viewers.resize(viewer_count);
|
|
}
|
|
size_t i = 0;
|
|
unsigned int max_distance = 0;
|
|
_world.viewers.for_each([&i, &max_distance, this](Viewer &viewer) {
|
|
_world.shared_priority_dependency->viewers[i] = viewer.world_position;
|
|
if (viewer.view_distance > max_distance) {
|
|
max_distance = viewer.view_distance;
|
|
}
|
|
++i;
|
|
});
|
|
// Cancel distance is increased because of two reasons:
|
|
// - Some volumes use a cubic area which has higher distances on their corners
|
|
// - Hysteresis is needed to reduce ping-pong
|
|
_world.shared_priority_dependency->highest_view_distance = max_distance * 2;
|
|
}
|
|
}
|
|
|
|
// void VoxelServer::get_min_max_block_padding(
|
|
// bool blocky_enabled, bool smooth_enabled, unsigned int &out_min_padding, unsigned int &out_max_padding) const {
|
|
|
|
// // const Volume &volume = _world.volumes.get(volume_id);
|
|
|
|
// // bool smooth_enabled = volume.stream->get_used_channels_mask() & (1 << VoxelBuffer::CHANNEL_SDF);
|
|
// // bool blocky_enabled = volume.voxel_library.is_valid() &&
|
|
// // volume.stream->get_used_channels_mask() & (1 << VoxelBuffer::CHANNEL_TYPE);
|
|
|
|
// out_min_padding = 0;
|
|
// out_max_padding = 0;
|
|
|
|
// if (blocky_enabled) {
|
|
// out_min_padding = max(out_min_padding, _blocky_meshers[0]->get_minimum_padding());
|
|
// out_max_padding = max(out_max_padding, _blocky_meshers[0]->get_maximum_padding());
|
|
// }
|
|
|
|
// if (smooth_enabled) {
|
|
// out_min_padding = max(out_min_padding, _smooth_meshers[0]->get_minimum_padding());
|
|
// out_max_padding = max(out_max_padding, _smooth_meshers[0]->get_maximum_padding());
|
|
// }
|
|
// }
|
|
|
|
static unsigned int debug_get_active_thread_count(const VoxelThreadPool &pool) {
|
|
unsigned int active_count = 0;
|
|
for (unsigned int i = 0; i < pool.get_thread_count(); ++i) {
|
|
VoxelThreadPool::State s = pool.get_thread_debug_state(i);
|
|
if (s == VoxelThreadPool::STATE_RUNNING) {
|
|
++active_count;
|
|
}
|
|
}
|
|
return active_count;
|
|
}
|
|
|
|
static VoxelServer::Stats::ThreadPoolStats debug_get_pool_stats(const VoxelThreadPool &pool) {
|
|
VoxelServer::Stats::ThreadPoolStats d;
|
|
d.tasks = pool.get_debug_remaining_tasks();
|
|
d.active_threads = debug_get_active_thread_count(pool);
|
|
d.thread_count = pool.get_thread_count();
|
|
return d;
|
|
}
|
|
|
|
VoxelServer::Stats VoxelServer::get_stats() const {
|
|
Stats s;
|
|
s.streaming = debug_get_pool_stats(_streaming_thread_pool);
|
|
s.generation = debug_get_pool_stats(_generation_thread_pool);
|
|
s.meshing = debug_get_pool_stats(_meshing_thread_pool);
|
|
return s;
|
|
}
|
|
|
|
Dictionary VoxelServer::_b_get_stats() {
|
|
return get_stats().to_dict();
|
|
}
|
|
|
|
void VoxelServer::_bind_methods() {
|
|
ClassDB::bind_method(D_METHOD("get_stats"), &VoxelServer::_b_get_stats);
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VoxelServer::BlockDataRequest::run(VoxelTaskContext ctx) {
|
|
VOXEL_PROFILE_SCOPE();
|
|
|
|
CRASH_COND(stream_dependency == nullptr);
|
|
Ref<VoxelStream> stream = stream_dependency->stream;
|
|
CRASH_COND(stream.is_null());
|
|
|
|
const Vector3i origin_in_voxels = (position << lod) * block_size;
|
|
|
|
switch (type) {
|
|
case TYPE_LOAD: {
|
|
voxels.instance();
|
|
voxels->create(block_size, block_size, block_size);
|
|
const VoxelStream::Result result = stream->emerge_block(voxels, origin_in_voxels, lod);
|
|
if (result == VoxelStream::RESULT_BLOCK_NOT_FOUND) {
|
|
Ref<VoxelGenerator> generator = stream_dependency->generator;
|
|
if (generator.is_valid()) {
|
|
VoxelServer::get_singleton()->request_block_generate_from_data_request(this);
|
|
type = TYPE_FALLBACK_ON_GENERATOR;
|
|
}
|
|
}
|
|
} break;
|
|
|
|
case TYPE_SAVE: {
|
|
Ref<VoxelBuffer> voxels_copy;
|
|
{
|
|
RWLockRead lock(voxels->get_lock());
|
|
voxels_copy = voxels->duplicate(true);
|
|
}
|
|
voxels.unref();
|
|
stream->immerge_block(voxels_copy, origin_in_voxels, lod);
|
|
} break;
|
|
|
|
default:
|
|
CRASH_NOW_MSG("Invalid type");
|
|
}
|
|
|
|
has_run = true;
|
|
}
|
|
|
|
int VoxelServer::BlockDataRequest::get_priority() {
|
|
if (type == TYPE_SAVE) {
|
|
return 0;
|
|
}
|
|
float closest_viewer_distance_sq;
|
|
const int p = VoxelServer::get_priority(priority_dependency, lod, &closest_viewer_distance_sq);
|
|
too_far = closest_viewer_distance_sq > priority_dependency.drop_distance_squared;
|
|
return p;
|
|
}
|
|
|
|
bool VoxelServer::BlockDataRequest::is_cancelled() {
|
|
return type == TYPE_LOAD && (!stream_dependency->valid || too_far);
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VoxelServer::BlockGenerateRequest::run(VoxelTaskContext ctx) {
|
|
VOXEL_PROFILE_SCOPE();
|
|
|
|
CRASH_COND(stream_dependency == nullptr);
|
|
Ref<VoxelGenerator> generator = stream_dependency->generator;
|
|
ERR_FAIL_COND(generator.is_null());
|
|
|
|
const Vector3i origin_in_voxels = (position << lod) * block_size;
|
|
|
|
if (voxels.is_null()) {
|
|
voxels.instance();
|
|
voxels->create(block_size, block_size, block_size);
|
|
}
|
|
|
|
VoxelBlockRequest r{ voxels, origin_in_voxels, lod };
|
|
generator->generate_block(r);
|
|
|
|
if (stream_dependency->valid) {
|
|
Ref<VoxelStream> stream = stream_dependency->stream;
|
|
if (stream.is_valid() && stream->get_save_generator_output()) {
|
|
VoxelServer::get_singleton()->request_block_save_from_generate_request(this);
|
|
}
|
|
}
|
|
|
|
has_run = true;
|
|
}
|
|
|
|
int VoxelServer::BlockGenerateRequest::get_priority() {
|
|
float closest_viewer_distance_sq;
|
|
const int p = VoxelServer::get_priority(priority_dependency, lod, &closest_viewer_distance_sq);
|
|
too_far = closest_viewer_distance_sq > priority_dependency.drop_distance_squared;
|
|
return p;
|
|
}
|
|
|
|
bool VoxelServer::BlockGenerateRequest::is_cancelled() {
|
|
return !stream_dependency->valid || too_far; // || stream_dependency->stream->get_fallback_generator().is_null();
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------
|
|
|
|
static void copy_block_and_neighbors(const FixedArray<Ref<VoxelBuffer>, Cube::MOORE_AREA_3D_COUNT> &moore_blocks,
|
|
VoxelBuffer &dst, int min_padding, int max_padding, int channels_mask) {
|
|
|
|
VOXEL_PROFILE_SCOPE();
|
|
|
|
FixedArray<uint8_t, VoxelBuffer::MAX_CHANNELS> channels;
|
|
unsigned int channels_count = 0;
|
|
for (unsigned int i = 0; i < VoxelBuffer::MAX_CHANNELS; ++i) {
|
|
if ((channels_mask & (1 << i)) != 0) {
|
|
channels[channels_count] = i;
|
|
++channels_count;
|
|
}
|
|
}
|
|
|
|
Ref<VoxelBuffer> central_buffer = moore_blocks[Cube::MOORE_AREA_3D_CENTRAL_INDEX];
|
|
CRASH_COND_MSG(central_buffer.is_null(), "Central buffer must be valid");
|
|
const int block_size = central_buffer->get_size().x;
|
|
const unsigned int padded_block_size = block_size + min_padding + max_padding;
|
|
|
|
dst.create(padded_block_size, padded_block_size, padded_block_size);
|
|
|
|
for (unsigned int ci = 0; ci < channels.size(); ++ci) {
|
|
dst.set_channel_depth(ci, central_buffer->get_channel_depth(ci));
|
|
}
|
|
|
|
const Vector3i min_pos = -Vector3i(min_padding);
|
|
const Vector3i max_pos = Vector3i(block_size + max_padding);
|
|
|
|
for (unsigned int i = 0; i < Cube::MOORE_AREA_3D_COUNT; ++i) {
|
|
const Vector3i offset = block_size * Cube::g_ordered_moore_area_3d[i];
|
|
Ref<VoxelBuffer> src = moore_blocks[i];
|
|
|
|
if (src.is_null()) {
|
|
continue;
|
|
}
|
|
|
|
const Vector3i src_min = min_pos - offset;
|
|
const Vector3i src_max = max_pos - offset;
|
|
const Vector3i dst_min = offset - min_pos;
|
|
|
|
{
|
|
RWLockRead read(src->get_lock());
|
|
for (unsigned int ci = 0; ci < channels.size(); ++ci) {
|
|
dst.copy_from(**src, src_min, src_max, dst_min, ci);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void VoxelServer::BlockMeshRequest::run(VoxelTaskContext ctx) {
|
|
VOXEL_PROFILE_SCOPE();
|
|
CRASH_COND(meshing_dependency == nullptr);
|
|
|
|
Ref<VoxelMesher> mesher = meshing_dependency->mesher;
|
|
CRASH_COND(mesher.is_null());
|
|
const unsigned int min_padding = mesher->get_minimum_padding();
|
|
const unsigned int max_padding = mesher->get_maximum_padding();
|
|
|
|
// TODO Cache?
|
|
Ref<VoxelBuffer> voxels;
|
|
voxels.instance();
|
|
copy_block_and_neighbors(blocks, **voxels, min_padding, max_padding, mesher->get_used_channels_mask());
|
|
|
|
VoxelMesher::Input input = { **voxels, lod };
|
|
|
|
mesher->build(surfaces_output, input);
|
|
|
|
has_run = true;
|
|
}
|
|
|
|
int VoxelServer::BlockMeshRequest::get_priority() {
|
|
float closest_viewer_distance_sq;
|
|
const int p = VoxelServer::get_priority(priority_dependency, lod, &closest_viewer_distance_sq);
|
|
too_far = closest_viewer_distance_sq > priority_dependency.drop_distance_squared;
|
|
return p;
|
|
}
|
|
|
|
bool VoxelServer::BlockMeshRequest::is_cancelled() {
|
|
return !meshing_dependency->valid || too_far;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace {
|
|
bool g_updater_created = false;
|
|
}
|
|
|
|
VoxelServerUpdater::VoxelServerUpdater() {
|
|
PRINT_VERBOSE("Creating VoxelServerUpdater");
|
|
set_process(true);
|
|
g_updater_created = true;
|
|
}
|
|
|
|
VoxelServerUpdater::~VoxelServerUpdater() {
|
|
g_updater_created = false;
|
|
}
|
|
|
|
void VoxelServerUpdater::ensure_existence(SceneTree *st) {
|
|
if (st == nullptr) {
|
|
return;
|
|
}
|
|
if (g_updater_created) {
|
|
return;
|
|
}
|
|
Viewport *root = st->get_root();
|
|
for (int i = 0; i < root->get_child_count(); ++i) {
|
|
VoxelServerUpdater *u = Object::cast_to<VoxelServerUpdater>(root->get_child(i));
|
|
if (u != nullptr) {
|
|
return;
|
|
}
|
|
}
|
|
VoxelServerUpdater *u = memnew(VoxelServerUpdater);
|
|
u->set_name("VoxelServerUpdater_dont_touch_this");
|
|
root->add_child(u);
|
|
}
|
|
|
|
void VoxelServerUpdater::_notification(int p_what) {
|
|
switch (p_what) {
|
|
case NOTIFICATION_PROCESS:
|
|
// To workaround the absence of API to have a custom server processing in the main loop
|
|
VoxelServer::get_singleton()->process();
|
|
break;
|
|
|
|
case NOTIFICATION_PREDELETE:
|
|
PRINT_VERBOSE("Deleting VoxelServerUpdater");
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|