Added a vector of jobs and an api for it into VoxelChunk.

This commit is contained in:
Relintai 2020-10-01 20:49:54 +02:00
parent 71f69666cd
commit be2cd7a4b3
4 changed files with 60 additions and 15 deletions

View File

@ -26,7 +26,7 @@ SOFTWARE.
#include "../../../opensimplex/open_simplex_noise.h"
void VoxelJob::set_chunk(const Ref<VoxelChunkDefault> &chunk) {
void VoxelJob::set_chunk(const Ref<VoxelChunk> &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<VoxelChunkDefault> 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);

View File

@ -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<VoxelChunkDefault> &chunk);
void set_chunk(const Ref<VoxelChunk> &chunk);
void chunk_exit_tree();
void finalize_build();
@ -61,10 +61,8 @@ public:
protected:
static void _bind_methods();
private:
bool _in_tree;
Ref<VoxelChunkDefault> _chunk;
Ref<VoxelChunk> _chunk;
public:
#if !THREAD_POOL_PRESENT

View File

@ -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<VoxelWorld>(world);
}
Ref<VoxelMesher> VoxelChunk::get_job(int index) const {
ERR_FAIL_INDEX_V(index, _jobs.size(), Ref<VoxelMesher>());
return _jobs.get(index);
}
void VoxelChunk::set_job(int index, const Ref<VoxelMesher> &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<VoxelMesher> &job) {
_jobs.push_back(job);
}
int VoxelChunk::get_job_count() const {
return _jobs.size();
}
Ref<VoxelMesher> VoxelChunk::get_mesher(int index) const {
ERR_FAIL_INDEX_V(index, _meshers.size(), Ref<VoxelMesher>());
@ -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);

View File

@ -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<VoxelMesher> get_job(const int index) const;
void set_job(const int index, const Ref<VoxelMesher> &job);
void remove_job(const int index);
void add_job(const Ref<VoxelMesher> &job);
int get_job_count() const;
//Meshers
Ref<VoxelMesher> get_mesher(const int index) const;
void set_mesher(const int index, const Ref<VoxelMesher> &mesher);
@ -346,6 +355,8 @@ protected:
float _voxel_scale;
Vector<Ref<VoxelJob> > _jobs;
Ref<VoxelmanLibrary> _library;
Vector<Ref<VoxelMesher> > _meshers;
Vector<Ref<VoxelMesher> > _liquid_meshers;