feat: multithreading for terrain mesh gen
This commit is contained in:
parent
d3561eb218
commit
8ff1b1404d
7 changed files with 130 additions and 30 deletions
|
|
@ -9,6 +9,7 @@ void Terrain::_bind_methods() {
|
|||
}
|
||||
|
||||
void Terrain::ready() {
|
||||
threads.resize_initialized(22);
|
||||
construct_chunk_grid();
|
||||
generate_meshes();
|
||||
}
|
||||
|
|
@ -27,8 +28,15 @@ void Terrain::update_modifier_list() {
|
|||
on_terrain_changed();
|
||||
}
|
||||
|
||||
void Terrain::child_order_changed() {
|
||||
// TODO: check if the order of _modifiers_ actually changed
|
||||
if (is_ready()) {
|
||||
update_modifier_list();
|
||||
}
|
||||
}
|
||||
|
||||
void Terrain::child_entered(Node *node) {
|
||||
if (cast_to<TerrainModifier>(node)) {
|
||||
if (cast_to<TerrainModifier>(node) != nullptr && is_ready()) {
|
||||
update_modifier_list();
|
||||
}
|
||||
}
|
||||
|
|
@ -39,7 +47,9 @@ void Terrain::child_exiting(Node *node) {
|
|||
if (Engine::get_singleton()->is_editor_hint() && !is_queued_for_deletion()) {
|
||||
mod->disconnect(TerrainModifier::sig_changed, callable_mp(this, &self_type::on_terrain_changed));
|
||||
}
|
||||
on_terrain_changed();
|
||||
if (is_ready()) {
|
||||
on_terrain_changed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -58,7 +68,7 @@ void Terrain::_notification(int what) {
|
|||
if (!is_ready()) {
|
||||
connect("child_entered_tree", callable_mp(this, &self_type::child_entered));
|
||||
connect("child_exiting_tree", callable_mp(this, &self_type::child_exiting));
|
||||
connect("child_order_changed", callable_mp(this, &self_type::update_modifier_list));
|
||||
connect("child_order_changed", callable_mp(this, &self_type::child_order_changed));
|
||||
}
|
||||
return;
|
||||
case NOTIFICATION_READY:
|
||||
|
|
@ -67,6 +77,24 @@ void Terrain::_notification(int what) {
|
|||
}
|
||||
}
|
||||
|
||||
void Terrain::generate_meshes_thread(void *terrain) {
|
||||
Terrain *self{ static_cast<Terrain *>(terrain) };
|
||||
for (;;) {
|
||||
self->workload_lock.lock();
|
||||
if (self->workload.is_empty()) {
|
||||
self->workload_lock.unlock();
|
||||
return;
|
||||
}
|
||||
TerrainChunkMesh *mesh{ self->workload[0] };
|
||||
self->workload.remove_at(0);
|
||||
self->workload_lock.unlock();
|
||||
if (!mesh->is_inside_tree()) {
|
||||
return;
|
||||
}
|
||||
mesh->update_mesh();
|
||||
}
|
||||
}
|
||||
|
||||
void Terrain::construct_chunk_grid() {
|
||||
size_t const chunks_per_side{ this->side_length / this->chunk_size };
|
||||
Vector3 const origin{ (float)this->chunk_size / 2.f, 0.f, (float)this->chunk_size / 2.f };
|
||||
|
|
@ -85,11 +113,17 @@ void Terrain::construct_chunk_grid() {
|
|||
}
|
||||
|
||||
void Terrain::generate_meshes() {
|
||||
for (TerrainChunkMesh *mesh : this->meshes) {
|
||||
if (!mesh->is_inside_tree()) {
|
||||
return;
|
||||
if (!is_inside_tree()) {
|
||||
return;
|
||||
}
|
||||
this->workload.append_array(this->meshes);
|
||||
for (Thread &thread : this->threads) {
|
||||
thread.start(&self_type::generate_meshes_thread, this);
|
||||
}
|
||||
for (Thread &thread : this->threads) {
|
||||
if (thread.is_started()) {
|
||||
thread.wait_to_finish();
|
||||
}
|
||||
mesh->update_mesh();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -124,6 +158,10 @@ void Terrain::set_chunk_size(size_t size) {
|
|||
}
|
||||
}
|
||||
|
||||
size_t Terrain::get_chunk_size() const {
|
||||
return this->chunk_size;
|
||||
}
|
||||
|
||||
void Terrain::set_detail(size_t detail) {
|
||||
this->detail = detail;
|
||||
if (is_inside_tree()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue