From a25862527cf7411f3c63d5a15f61fe34fbfea167 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 2 Oct 2020 12:19:24 +0200 Subject: [PATCH] More work on VoxelJob's api. --- world/jobs/voxel_job.cpp | 122 +++++++++++++++++++++++++----- world/jobs/voxel_job.h | 38 ++++++++-- world/jobs/voxel_terrarin_job.cpp | 36 +++++++-- world/jobs/voxel_terrarin_job.h | 4 +- 4 files changed, 166 insertions(+), 34 deletions(-) diff --git a/world/jobs/voxel_job.cpp b/world/jobs/voxel_job.cpp index d158349..d0d554e 100644 --- a/world/jobs/voxel_job.cpp +++ b/world/jobs/voxel_job.cpp @@ -26,23 +26,66 @@ SOFTWARE. #include "../../../opensimplex/open_simplex_noise.h" +const String VoxelJob::BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE = "Normal,Process,Physics Process"; + +VoxelJob::ActiveBuildPhaseType VoxelJob::get_build_phase_type() { + return _build_phase_type; +} +void VoxelJob::set_build_phase_type(VoxelJob::ActiveBuildPhaseType build_phase_type) { + _build_phase_type = build_phase_type; +} + void VoxelJob::set_chunk(const Ref &chunk) { _chunk = chunk; _in_tree = true; } -void VoxelJob::chunk_exit_tree() { +int VoxelJob::get_phase() { + return _phase; +} +void VoxelJob::set_phase(const int phase) { + _phase = phase; +} +void VoxelJob::next_phase() { + ++_phase; +} - _in_tree = false; +bool VoxelJob::get_build_done() { + return _build_done; +} +void VoxelJob::set_build_done(const bool val) { + _build_done = val; +} - if (get_complete()) { - _chunk.unref(); - } else { - set_cancelled(true); +void VoxelJob::next_job() { + //chunk->next_job(); + set_build_done(true); +} + +void VoxelJob::reset() { + call("_reset"); +} +void VoxelJob::_reset() { + _build_done = false; + _phase = 0; +} + +#define NEW 0 + +#if NEW + +void VoxelJob::_execute() { + + ActiveBuildPhaseType origpt = _build_phase_type; + + while (!get_cancelled() && _in_tree && !_build_done && origpt == _build_phase_type && !should_return()) { + execute_phase(); } } +#else + void VoxelJob::_execute() { ERR_FAIL_COND(!_chunk.is_valid()); @@ -51,28 +94,15 @@ void VoxelJob::_execute() { ERR_FAIL_COND(!chunk.is_valid()); if (!chunk->has_next_phase()) { - //_chunk->set_build_step_in_progress(false); - - //if (!_in_tree) { - // _chunk.unref(); - //} - set_complete(true); } 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) { - //int phase = _chunk->get_current_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()) return; @@ -89,6 +119,16 @@ void VoxelJob::_execute() { set_complete(true); } +#endif + +void VoxelJob::execute_phase() { + call("_execute_phase"); +} + +void VoxelJob::_execute_phase() { + next_job(); +} + //Data Management functions void VoxelJob::generate_ao() { ERR_FAIL_COND(!_chunk.is_valid()); @@ -176,9 +216,24 @@ void VoxelJob::generate_random_ao(int seed, int octaves, int period, float persi } } +void VoxelJob::chunk_exit_tree() { + + _in_tree = false; + + if (get_complete()) { + _chunk.unref(); + } else { + set_cancelled(true); + } +} + VoxelJob::VoxelJob() { _in_tree = false; + _build_phase_type = BUILD_PHASE_TYPE_NORMAL; + _build_done = true; + _phase = 0; + #if !THREAD_POOL_PRESENT _complete = true; _cancelled = false; @@ -196,8 +251,37 @@ VoxelJob::~VoxelJob() { } void VoxelJob::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_build_phase_type"), &VoxelJob::get_build_phase_type); + ClassDB::bind_method(D_METHOD("set_build_phase_type", "value"), &VoxelJob::set_build_phase_type); + ADD_PROPERTY(PropertyInfo(Variant::INT, "build_phase_type", PROPERTY_HINT_ENUM, BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE), "set_build_phase_type", "get_build_phase_type"); + + ClassDB::bind_method(D_METHOD("set_chunk", "chunk"), &VoxelJob::set_chunk); + + ClassDB::bind_method(D_METHOD("get_phase"), &VoxelJob::get_phase); + ClassDB::bind_method(D_METHOD("set_phase", "phase"), &VoxelJob::set_phase); + ClassDB::bind_method(D_METHOD("next_phase"), &VoxelJob::next_phase); + + ClassDB::bind_method(D_METHOD("get_build_done"), &VoxelJob::get_build_done); + ClassDB::bind_method(D_METHOD("set_build_done", "val"), &VoxelJob::set_build_done); + + ClassDB::bind_method(D_METHOD("next_job"), &VoxelJob::next_job); + + BIND_VMETHOD(MethodInfo("_reset")); + + ClassDB::bind_method(D_METHOD("reset"), &VoxelJob::reset); + ClassDB::bind_method(D_METHOD("_reset"), &VoxelJob::_reset); + ClassDB::bind_method(D_METHOD("_execute"), &VoxelJob::_execute); + BIND_VMETHOD(MethodInfo("_execute_phase")); + + ClassDB::bind_method(D_METHOD("execute_phase"), &VoxelJob::execute_phase); + + ClassDB::bind_method(D_METHOD("generate_ao"), &VoxelJob::generate_ao); + ClassDB::bind_method(D_METHOD("generate_random_ao", "seed", "octaves", "period", "persistence", "scale_factor"), &VoxelJob::generate_random_ao); + + ClassDB::bind_method(D_METHOD("chunk_exit_tree"), &VoxelJob::chunk_exit_tree); + #if !THREAD_POOL_PRESENT ClassDB::bind_method(D_METHOD("get_complete"), &VoxelJob::get_complete); ClassDB::bind_method(D_METHOD("set_complete", "value"), &VoxelJob::set_complete); diff --git a/world/jobs/voxel_job.h b/world/jobs/voxel_job.h index 8899945..9bc034a 100644 --- a/world/jobs/voxel_job.h +++ b/world/jobs/voxel_job.h @@ -40,27 +40,51 @@ class VoxelJob : public Resource { #endif public: - enum { - RESULT_TYPE_FLAG_MESH = 1 << 0, - RESULT_TYPE_FLAG_COLLIDER = 1 << 1, + static const String BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE; + + enum ActiveBuildPhaseType { + BUILD_PHASE_TYPE_NORMAL = 0, + BUILD_PHASE_TYPE_PROCESS, + BUILD_PHASE_TYPE_PHYSICS_PROCESS, }; public: - void set_chunk(const Ref &chunk); - void chunk_exit_tree(); + ActiveBuildPhaseType get_build_phase_type(); + void set_build_phase_type(VoxelJob::ActiveBuildPhaseType build_phase_type); + + void set_chunk(const Ref &chunk); + + int get_phase(); + void set_phase(const int phase); + void next_phase(); + + bool get_build_done(); + void set_build_done(const bool val); + + void next_job(); + + void reset(); + virtual void _reset(); - void finalize_build(); void _execute(); + void execute_phase(); + virtual void _execute_phase(); + void generate_ao(); void generate_random_ao(int seed, int octaves, int period, float persistence, float scale_factor); + void chunk_exit_tree(); + VoxelJob(); ~VoxelJob(); protected: static void _bind_methods(); + ActiveBuildPhaseType _build_phase_type; + bool _build_done; + int _phase; bool _in_tree; Ref _chunk; @@ -103,4 +127,6 @@ private: #endif }; +VARIANT_ENUM_CAST(VoxelJob::ActiveBuildPhaseType); + #endif diff --git a/world/jobs/voxel_terrarin_job.cpp b/world/jobs/voxel_terrarin_job.cpp index 2e0b06f..932dad7 100644 --- a/world/jobs/voxel_terrarin_job.cpp +++ b/world/jobs/voxel_terrarin_job.cpp @@ -542,20 +542,38 @@ void VoxelTerrarinJob::phase_terrarin_mesh() { reset_stages(); } -void VoxelTerrarinJob::_execute() { +void VoxelTerrarinJob::phase_finalize() { + //add vs meshes to chunk + + set_complete(true); //So threadpool knows it's done + set_build_phase_type(BUILD_PHASE_TYPE_PHYSICS_PROCESS); +} + +void VoxelTerrarinJob::phase_finalize_physics_process() { + // add physics meshes + + next_job(); +} + +void VoxelTerrarinJob::_execute_phase() { ERR_FAIL_COND(!_chunk.is_valid()); Ref library = _chunk->get_library(); ERR_FAIL_COND(!library.is_valid()); - //Todo add checks for these whether the phases are done or not - phase_setup(); - phase_terrarin_mesh_setup(); - phase_collider(); - phase_terrarin_mesh(); - - //finish + if (_phase == 0) + phase_setup(); + else if (_phase == 1) + phase_terrarin_mesh_setup(); + else if (_phase == 2) + phase_collider(); + else if (_phase == 3) + phase_terrarin_mesh(); + else if (_phase == 4) + phase_finalize(); + else if (_phase == 5) + phase_finalize_physics_process(); } VoxelTerrarinJob::VoxelTerrarinJob() { @@ -578,4 +596,6 @@ void VoxelTerrarinJob::_bind_methods() { ClassDB::bind_method(D_METHOD("remove_liquid_mesher", "index"), &VoxelTerrarinJob::remove_liquid_mesher); ClassDB::bind_method(D_METHOD("add_liquid_mesher", "mesher"), &VoxelTerrarinJob::add_liquid_mesher); ClassDB::bind_method(D_METHOD("get_liquid_mesher_count"), &VoxelTerrarinJob::get_liquid_mesher_count); + + ClassDB::bind_method(D_METHOD("_execute_phase"), &VoxelTerrarinJob::_execute_phase); } diff --git a/world/jobs/voxel_terrarin_job.h b/world/jobs/voxel_terrarin_job.h index 42f1864..d8a4c8d 100644 --- a/world/jobs/voxel_terrarin_job.h +++ b/world/jobs/voxel_terrarin_job.h @@ -49,8 +49,10 @@ public: void phase_terrarin_mesh_setup(); void phase_collider(); void phase_terrarin_mesh(); + void phase_finalize(); + void phase_finalize_physics_process(); - void _execute(); + void _execute_phase(); VoxelTerrarinJob(); ~VoxelTerrarinJob();