From be2cd7a4b32be051b4afb88e6bbde6ab0acebbae Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 1 Oct 2020 20:49:54 +0200 Subject: [PATCH] Added a vector of jobs and an api for it into VoxelChunk. --- world/jobs/voxel_job.cpp | 22 +++++++++++++--------- world/jobs/voxel_job.h | 8 +++----- world/voxel_chunk.cpp | 32 ++++++++++++++++++++++++++++++++ world/voxel_chunk.h | 13 ++++++++++++- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/world/jobs/voxel_job.cpp b/world/jobs/voxel_job.cpp index 1fd63d5..d158349 100644 --- a/world/jobs/voxel_job.cpp +++ b/world/jobs/voxel_job.cpp @@ -26,7 +26,7 @@ SOFTWARE. #include "../../../opensimplex/open_simplex_noise.h" -void VoxelJob::set_chunk(const Ref &chunk) { +void VoxelJob::set_chunk(const Ref &chunk) { _chunk = chunk; _in_tree = true; @@ -46,7 +46,11 @@ void VoxelJob::chunk_exit_tree() { void VoxelJob::_execute() { ERR_FAIL_COND(!_chunk.is_valid()); - if (!_chunk->has_next_phase()) { + Ref chunk = _chunk; + + ERR_FAIL_COND(!chunk.is_valid()); + + if (!chunk->has_next_phase()) { //_chunk->set_build_step_in_progress(false); //if (!_in_tree) { @@ -56,30 +60,30 @@ void VoxelJob::_execute() { set_complete(true); } - ERR_FAIL_COND(!_chunk->has_next_phase()); + ERR_FAIL_COND(!chunk->has_next_phase()); //ERR_FAIL_COND(_build_step_in_progress); //_chunk->set_build_step_in_progress(true); - while (!get_cancelled() && _in_tree && _chunk->has_next_phase() && _chunk->get_active_build_phase_type() == VoxelChunkDefault::BUILD_PHASE_TYPE_NORMAL) { + while (!get_cancelled() && _in_tree && chunk->has_next_phase() && chunk->get_active_build_phase_type() == VoxelChunkDefault::BUILD_PHASE_TYPE_NORMAL) { //int phase = _chunk->get_current_build_phase(); - _chunk->build_phase(); + chunk->build_phase(); //print_error(String::num(get_current_execution_time()) + " phase: " + String::num(phase)); - if (_chunk->get_active_build_phase_type() == VoxelChunkDefault::BUILD_PHASE_TYPE_NORMAL && should_return()) + if (chunk->get_active_build_phase_type() == VoxelChunkDefault::BUILD_PHASE_TYPE_NORMAL && should_return()) return; - if (!_chunk->get_build_phase_done()) + if (!chunk->get_build_phase_done()) break; } - _chunk->set_build_step_in_progress(false); + chunk->set_build_step_in_progress(false); if (!_in_tree) { - _chunk.unref(); + chunk.unref(); } set_complete(true); diff --git a/world/jobs/voxel_job.h b/world/jobs/voxel_job.h index 730d015..8899945 100644 --- a/world/jobs/voxel_job.h +++ b/world/jobs/voxel_job.h @@ -29,7 +29,7 @@ SOFTWARE. #include "core/resource.h" #endif -class VoxelChunkDefault; +class VoxelChunk; #if THREAD_POOL_PRESENT class VoxelJob : public ThreadPoolJob { @@ -46,7 +46,7 @@ public: }; public: - void set_chunk(const Ref &chunk); + void set_chunk(const Ref &chunk); void chunk_exit_tree(); void finalize_build(); @@ -61,10 +61,8 @@ public: protected: static void _bind_methods(); -private: bool _in_tree; - - Ref _chunk; + Ref _chunk; public: #if !THREAD_POOL_PRESENT diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 75bc8ed..adea80b 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -28,6 +28,8 @@ SOFTWARE. #include "../defines.h" +#include "jobs/voxel_job.h" + _FORCE_INLINE_ bool VoxelChunk::get_is_build_threaded() const { return _is_build_threaded; } @@ -218,6 +220,28 @@ void VoxelChunk::set_voxel_world_bind(Node *world) { _voxel_world = Object::cast_to(world); } +Ref VoxelChunk::get_job(int index) const { + ERR_FAIL_INDEX_V(index, _jobs.size(), Ref()); + + return _jobs.get(index); +} +void VoxelChunk::set_job(int index, const Ref &job) { + ERR_FAIL_INDEX(index, _jobs.size()); + + _jobs.set(index, job); +} +void VoxelChunk::remove_job(const int index) { + ERR_FAIL_INDEX(index, _jobs.size()); + + _jobs.remove(index); +} +void VoxelChunk::add_job(const Ref &job) { + _jobs.push_back(job); +} +int VoxelChunk::get_job_count() const { + return _jobs.size(); +} + Ref VoxelChunk::get_mesher(int index) const { ERR_FAIL_INDEX_V(index, _meshers.size(), Ref()); @@ -1070,6 +1094,8 @@ VoxelChunk::~VoxelChunk() { } _colliders.clear(); + + _jobs.clear(); } void VoxelChunk::_world_transform_changed() { @@ -1252,6 +1278,12 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("set_voxel_scale", "value"), &VoxelChunk::set_voxel_scale); ADD_PROPERTY(PropertyInfo(Variant::REAL, "voxel_scale"), "set_voxel_scale", "get_voxel_scale"); + ClassDB::bind_method(D_METHOD("get_job", "index"), &VoxelChunk::get_job); + ClassDB::bind_method(D_METHOD("set_job", "index", "job"), &VoxelChunk::set_job); + ClassDB::bind_method(D_METHOD("remove_job", "index"), &VoxelChunk::remove_job); + ClassDB::bind_method(D_METHOD("add_job", "job"), &VoxelChunk::add_job); + ClassDB::bind_method(D_METHOD("get_job_count"), &VoxelChunk::get_job_count); + ClassDB::bind_method(D_METHOD("get_mesher", "index"), &VoxelChunk::get_mesher); ClassDB::bind_method(D_METHOD("set_mesher", "index", "mesher"), &VoxelChunk::set_mesher); ClassDB::bind_method(D_METHOD("remove_mesher", "index"), &VoxelChunk::remove_mesher); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 89a5123..19b51c2 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -58,8 +58,10 @@ include_pool_vector #include "../library/voxel_surface.h" #include "../library/voxelman_library.h" + ; //hackfix for a clang format issue - class VoxelWorld; +class VoxelJob; +class VoxelWorld; class VoxelChunk : public Resource { GDCLASS(VoxelChunk, Resource); @@ -138,6 +140,13 @@ public: void set_voxel_world(VoxelWorld *world); void set_voxel_world_bind(Node *world); + //Jobs + Ref get_job(const int index) const; + void set_job(const int index, const Ref &job); + void remove_job(const int index); + void add_job(const Ref &job); + int get_job_count() const; + //Meshers Ref get_mesher(const int index) const; void set_mesher(const int index, const Ref &mesher); @@ -346,6 +355,8 @@ protected: float _voxel_scale; + Vector > _jobs; + Ref _library; Vector > _meshers; Vector > _liquid_meshers;