Now the threading is handled by ThreadPool.

This commit is contained in:
Relintai 2020-08-04 09:44:18 +02:00
parent b95f055acd
commit cff203fc49
8 changed files with 188 additions and 7 deletions

8
SCsub
View File

@ -9,6 +9,9 @@ if os.path.isdir('../texture_packer'):
module_env.Append(CPPDEFINES=['TEXTURE_PACKER_PRESENT']) module_env.Append(CPPDEFINES=['TEXTURE_PACKER_PRESENT'])
has_texture_packer = True has_texture_packer = True
if os.path.isdir('../thread_pool'):
module_env.Append(CPPDEFINES=['THREAD_POOL_PRESENT'])
if os.path.isdir('../mesh_data_resource'): if os.path.isdir('../mesh_data_resource'):
module_env.Append(CPPDEFINES=['MESH_DATA_RESOURCE_PRESENT']) module_env.Append(CPPDEFINES=['MESH_DATA_RESOURCE_PRESENT'])
@ -63,12 +66,13 @@ sources = [
"world/cubic/voxel_chunk_cubic.cpp", "world/cubic/voxel_chunk_cubic.cpp",
"world/cubic/voxel_world_cubic.cpp", "world/cubic/voxel_world_cubic.cpp",
"areas/world_area.cpp", "areas/world_area.cpp",
"world/voxel_world_editor.cpp", "world/voxel_world_editor.cpp",
"thirdparty/lz4/lz4.c" "thirdparty/lz4/lz4.c",
"world/default/voxel_job.cpp",
] ]
if has_texture_packer: if has_texture_packer:

View File

@ -55,7 +55,8 @@ def get_doc_classes():
"VoxelMesherDefault", "VoxelMesherDefault",
"VoxelWorldDefault", "VoxelWorldDefault",
"VoxelChunkBlocky",
"VoxelJob",
] ]
def get_doc_path(): def get_doc_path():

View File

@ -72,6 +72,8 @@ SOFTWARE.
#include "nodes/voxelman_light.h" #include "nodes/voxelman_light.h"
#include "world/default/voxel_job.h"
void register_voxelman_types() { void register_voxelman_types() {
ClassDB::register_class<VoxelMesher>(); ClassDB::register_class<VoxelMesher>();
ClassDB::register_class<VoxelMesherDefault>(); ClassDB::register_class<VoxelMesherDefault>();
@ -121,6 +123,8 @@ void register_voxelman_types() {
ClassDB::register_class<WorldArea>(); ClassDB::register_class<WorldArea>();
ClassDB::register_class<VoxelJob>();
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
EditorPlugins::add_by_type<VoxelWorldEditorPlugin>(); EditorPlugins::add_by_type<VoxelWorldEditorPlugin>();
#endif #endif

View File

@ -31,6 +31,9 @@ SOFTWARE.
#include "../../meshers/default/voxel_mesher_default.h" #include "../../meshers/default/voxel_mesher_default.h"
#include "../voxel_world.h" #include "../voxel_world.h"
#include "../../../thread_pool/thread_pool.h"
#include "voxel_job.h"
const String VoxelChunkDefault::BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE = "Normal,Process,Physics Process"; const String VoxelChunkDefault::BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE = "Normal,Process,Physics Process";
const String VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS = "Use Isolevel,Use Lighting,Use AO,Use RAO,Generate AO,Generate RAO,Bake Lights,Create Collider,Create Lods"; const String VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS = "Use Isolevel,Use Lighting,Use AO,Use RAO,Generate AO,Generate RAO,Bake Lights,Create Collider,Create Lods";
@ -185,6 +188,9 @@ void VoxelChunkDefault::build_step() {
_build_step_in_progress = true; _build_step_in_progress = true;
ThreadPool::get_singleton()->add_job(_job);
/*
if (get_is_build_threaded()) { if (get_is_build_threaded()) {
if (_build_thread) { if (_build_thread) {
wait_and_finish_thread(); wait_and_finish_thread();
@ -202,6 +208,7 @@ void VoxelChunkDefault::build_step() {
} }
_build_step_in_progress = false; _build_step_in_progress = false;
*/
} }
void VoxelChunkDefault::_build_step_threaded(void *_userdata) { void VoxelChunkDefault::_build_step_threaded(void *_userdata) {
@ -933,6 +940,10 @@ void VoxelChunkDefault::_visibility_changed(bool visible) {
} }
} }
void VoxelChunkDefault::_enter_tree() {
_job->set_chunk(Ref<VoxelChunkDefault>(this));
}
void VoxelChunkDefault::_exit_tree() { void VoxelChunkDefault::_exit_tree() {
if (_build_thread) { if (_build_thread) {
_abort_build = true; _abort_build = true;
@ -941,6 +952,8 @@ void VoxelChunkDefault::_exit_tree() {
} }
free_rids(); free_rids();
_job->chunk_exit_tree();
} }
void VoxelChunkDefault::_process(float delta) { void VoxelChunkDefault::_process(float delta) {
if (!get_is_generating()) { if (!get_is_generating()) {
@ -1113,6 +1126,17 @@ void VoxelChunkDefault::free_chunk() {
free_rids(); free_rids();
} }
bool VoxelChunkDefault::get_build_step_in_progress() const {
return _build_step_in_progress;
}
void VoxelChunkDefault::set_build_step_in_progress(const bool value) {
_build_step_in_progress = value;
}
Ref<VoxelJob> VoxelChunkDefault::get_job() {
return _job;
}
VoxelChunkDefault::VoxelChunkDefault() { VoxelChunkDefault::VoxelChunkDefault() {
_abort_build = false; _abort_build = false;
_queued_generation = false; _queued_generation = false;
@ -1134,6 +1158,8 @@ VoxelChunkDefault::VoxelChunkDefault() {
_current_lod_level = 0; _current_lod_level = 0;
_build_flags = BUILD_FLAG_CREATE_COLLIDER | BUILD_FLAG_CREATE_LODS; _build_flags = BUILD_FLAG_CREATE_COLLIDER | BUILD_FLAG_CREATE_LODS;
_job.instance();
} }
VoxelChunkDefault::~VoxelChunkDefault() { VoxelChunkDefault::~VoxelChunkDefault() {
@ -1145,6 +1171,8 @@ VoxelChunkDefault::~VoxelChunkDefault() {
_lights.clear(); _lights.clear();
debug_mesh_free(); debug_mesh_free();
_job.unref();
} }
void VoxelChunkDefault::_setup_channels() { void VoxelChunkDefault::_setup_channels() {
@ -1911,6 +1939,7 @@ void VoxelChunkDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("_build", "immediate"), &VoxelChunkDefault::_build); ClassDB::bind_method(D_METHOD("_build", "immediate"), &VoxelChunkDefault::_build);
ClassDB::bind_method(D_METHOD("_visibility_changed", "visible"), &VoxelChunkDefault::_visibility_changed); ClassDB::bind_method(D_METHOD("_visibility_changed", "visible"), &VoxelChunkDefault::_visibility_changed);
ClassDB::bind_method(D_METHOD("_enter_tree"), &VoxelChunkDefault::_enter_tree);
ClassDB::bind_method(D_METHOD("_exit_tree"), &VoxelChunkDefault::_exit_tree); ClassDB::bind_method(D_METHOD("_exit_tree"), &VoxelChunkDefault::_exit_tree);
ClassDB::bind_method(D_METHOD("_process", "delta"), &VoxelChunkDefault::_process); ClassDB::bind_method(D_METHOD("_process", "delta"), &VoxelChunkDefault::_process);
ClassDB::bind_method(D_METHOD("_physics_process", "delta"), &VoxelChunkDefault::_physics_process); ClassDB::bind_method(D_METHOD("_physics_process", "delta"), &VoxelChunkDefault::_physics_process);
@ -1923,6 +1952,8 @@ void VoxelChunkDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("_world_light_added", "light"), &VoxelChunkDefault::_world_light_added); ClassDB::bind_method(D_METHOD("_world_light_added", "light"), &VoxelChunkDefault::_world_light_added);
ClassDB::bind_method(D_METHOD("_world_light_removed", "light"), &VoxelChunkDefault::_world_light_removed); ClassDB::bind_method(D_METHOD("_world_light_removed", "light"), &VoxelChunkDefault::_world_light_removed);
ClassDB::bind_method(D_METHOD("get_job"), &VoxelChunkDefault::get_job);
BIND_CONSTANT(BUILD_PHASE_DONE); BIND_CONSTANT(BUILD_PHASE_DONE);
BIND_CONSTANT(BUILD_PHASE_SETUP); BIND_CONSTANT(BUILD_PHASE_SETUP);
BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH_SETUP); BIND_CONSTANT(BUILD_PHASE_TERRARIN_MESH_SETUP);

View File

@ -47,6 +47,7 @@ SOFTWARE.
#include "../../library/voxelman_library.h" #include "../../library/voxelman_library.h"
class VoxelWorld; class VoxelWorld;
class VoxelJob;
class VoxelChunkDefault : public VoxelChunk { class VoxelChunkDefault : public VoxelChunk {
GDCLASS(VoxelChunkDefault, VoxelChunk); GDCLASS(VoxelChunkDefault, VoxelChunk);
@ -226,6 +227,11 @@ public:
void generate_random_ao(const int seed, const int octaves = 4, const int period = 30, const float persistence = 0.3, const float scale_factor = 0.6); void generate_random_ao(const int seed, const int octaves = 4, const int period = 30, const float persistence = 0.3, const float scale_factor = 0.6);
bool get_build_step_in_progress() const;
void set_build_step_in_progress(const bool value);
Ref<VoxelJob> get_job();
VoxelChunkDefault(); VoxelChunkDefault();
~VoxelChunkDefault(); ~VoxelChunkDefault();
@ -239,6 +245,7 @@ protected:
virtual void _build(bool immediate); virtual void _build(bool immediate);
virtual void _visibility_changed(bool visible); virtual void _visibility_changed(bool visible);
virtual void _enter_tree();
virtual void _exit_tree(); virtual void _exit_tree();
virtual void _process(float delta); virtual void _process(float delta);
virtual void _physics_process(float delta); virtual void _physics_process(float delta);
@ -290,7 +297,9 @@ protected:
ActiveBuildPhaseType _active_build_phase_type; ActiveBuildPhaseType _active_build_phase_type;
Vector<Ref<VoxelLight>> _lights; Vector<Ref<VoxelLight> > _lights;
Ref<VoxelJob> _job;
}; };
VARIANT_ENUM_CAST(VoxelChunkDefault::DefaultChannels); VARIANT_ENUM_CAST(VoxelChunkDefault::DefaultChannels);

View File

@ -0,0 +1,78 @@
/*
Copyright (c) 2019-2020 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "voxel_job.h"
#include "voxel_chunk_default.h"
void VoxelJob::set_chunk(const Ref<VoxelChunkDefault> &chunk) {
_chunk = chunk;
_in_tree = true;
}
void VoxelJob::chunk_exit_tree() {
_in_tree = false;
if (get_complete()) {
_chunk.unref();
}
}
void VoxelJob::_execute() {
ERR_FAIL_COND(!_chunk.is_valid());
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) {
if (should_return())
return;
_chunk->build_phase();
if (!_chunk->get_build_phase_done())
break;
}
_chunk->set_build_step_in_progress(false);
if (!_in_tree) {
_chunk.unref();
}
set_complete(true);
}
VoxelJob::VoxelJob() {
_build_step_in_progress = false;
_in_tree = false;
}
VoxelJob::~VoxelJob() {
_chunk.unref();
}
void VoxelJob::_bind_methods() {
ClassDB::bind_method(D_METHOD("_execute"), &VoxelJob::_execute);
}

54
world/default/voxel_job.h Normal file
View File

@ -0,0 +1,54 @@
/*
Copyright (c) 2019-2020 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef VOXEL_JOB_H
#define VOXEL_JOB_H
#include "../../../thread_pool/thread_pool_job.h"
class VoxelChunkDefault;
class VoxelJob : public ThreadPoolJob {
GDCLASS(VoxelJob, ThreadPoolJob);
public:
void set_chunk(const Ref<VoxelChunkDefault> &chunk);
void chunk_exit_tree();
void _execute();
VoxelJob();
~VoxelJob();
bool _build_step_in_progress;
protected:
static void _bind_methods();
private:
bool _in_tree;
Ref<VoxelChunkDefault> _chunk;
};
#endif

View File

@ -46,9 +46,6 @@ include_pool_vector
#include "../meshers/voxel_mesher.h" #include "../meshers/voxel_mesher.h"
#include "../library/voxel_surface.h"
#include "../library/voxelman_library.h"
#if PROPS_PRESENT #if PROPS_PRESENT
#include "../../props/props/prop_data.h" #include "../../props/props/prop_data.h"
#endif #endif
@ -59,6 +56,9 @@ include_pool_vector
#define Texture Texture2D #define Texture Texture2D
#endif #endif
#include "../library/voxel_surface.h"
#include "../library/voxelman_library.h"
class VoxelWorld; class VoxelWorld;
class VoxelChunk : public Resource { class VoxelChunk : public Resource {