chore: massively sped up mesh gen performance

This commit is contained in:
Sara Gerretsen 2026-02-25 18:57:06 +01:00
parent 8ff1b1404d
commit 811970a306
7 changed files with 201 additions and 79 deletions

View file

@ -6,50 +6,62 @@ void Terrain::_bind_methods() {
BIND_PROPERTY(Variant::INT, side_length);
BIND_PROPERTY(Variant::INT, chunk_size);
BIND_PROPERTY(Variant::INT, detail);
BIND_PROPERTY(Variant::INT, thread_count);
}
void Terrain::ready() {
threads.resize_initialized(22);
construct_chunk_grid();
generate_meshes();
}
void Terrain::update_modifier_list() {
bool editor{ Engine::get_singleton()->is_editor_hint() };
this->modifiers.clear();
for (Variant var : get_children()) {
if (TerrainModifier * mod{ cast_to<TerrainModifier>(var) }) {
if (editor && !mod->is_connected(TerrainModifier::sig_changed, callable_mp(this, &self_type::on_terrain_changed))) {
mod->connect(TerrainModifier::sig_changed, callable_mp(this, &self_type::on_terrain_changed));
}
this->modifiers.push_back(mod);
}
void Terrain::process() {
if (!is_inside_tree()) {
return;
}
if (this->pending_tasks == 0 && this->dirty) {
this->workload_lock.lock();
if (this->workload.is_empty()) {
this->dirty = false;
this->workload.append_array(this->meshes);
this->pending_tasks = this->workload.size();
}
this->workload_lock.unlock();
}
on_terrain_changed();
}
void Terrain::child_order_changed() {
// TODO: check if the order of _modifiers_ actually changed
Callable on_changed_callable{ callable_mp(this, &self_type::on_terrain_changed) };
// check if the modifiers actually changed
if (is_ready()) {
update_modifier_list();
}
}
void Terrain::child_entered(Node *node) {
if (cast_to<TerrainModifier>(node) != nullptr && is_ready()) {
update_modifier_list();
bool modified{ false };
size_t idx{ 0 };
for (Variant var : get_children()) {
if (TerrainModifier * mod{ cast_to<TerrainModifier>(var) }) {
if (modified) {
this->modifiers.push_back(mod);
} else if (idx >= this->modifiers.size() || mod != this->modifiers[idx]) {
modified = true;
this->modifiers.resize(idx);
this->modifiers.push_back(mod);
} else {
idx++;
}
if (!mod->is_connected(TerrainModifier::sig_changed, on_changed_callable)) {
mod->connect(TerrainModifier::sig_changed, on_changed_callable);
}
}
}
if (modified && is_ready()) {
on_terrain_changed();
}
}
}
void Terrain::child_exiting(Node *node) {
if (TerrainModifier * mod{ cast_to<TerrainModifier>(node) }) {
this->modifiers.erase(mod);
if (Engine::get_singleton()->is_editor_hint() && !is_queued_for_deletion()) {
mod->disconnect(TerrainModifier::sig_changed, callable_mp(this, &self_type::on_terrain_changed));
}
if (is_ready()) {
on_terrain_changed();
}
}
}
@ -66,36 +78,62 @@ void Terrain::_notification(int what) {
return;
case NOTIFICATION_ENTER_TREE:
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::child_order_changed));
}
return;
case NOTIFICATION_READY:
set_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process();
return;
case NOTIFICATION_EXIT_TREE:
this->workload_lock.lock();
this->threads_stop = true;
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.wait_to_finish();
}
return;
}
}
void Terrain::generate_meshes_thread(void *terrain) {
Terrain *self{ static_cast<Terrain *>(terrain) };
print_line("thread", Thread::get_caller_id(), "start");
for (;;) {
self->workload_lock.lock();
if (self->threads_stop) {
self->workload_lock.unlock();
print_line(Thread::get_caller_id(), "exiting");
break;
}
if (self->workload.is_empty()) {
self->workload_lock.unlock();
return;
Thread::yield();
continue;
}
TerrainChunkMesh *mesh{ self->workload[0] };
self->workload.remove_at(0);
self->workload_lock.unlock();
if (!mesh->is_inside_tree()) {
return;
print_line(Thread::get_caller_id(), "mesh is outside tree, exiting");
break;
}
mesh->update_mesh();
Thread::yield();
}
print_line(Thread::get_caller_id(), "done");
return;
}
void Terrain::construct_chunk_grid() {
for (TerrainChunkMesh *mesh : this->meshes) {
mesh->queue_free();
}
this->meshes.clear();
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 };
for (size_t y{ 0 }; y < chunks_per_side; ++y) {
@ -113,18 +151,7 @@ void Terrain::construct_chunk_grid() {
}
void Terrain::generate_meshes() {
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();
}
}
dirty = true;
}
float Terrain::height_at(Vector2 world_coordinate) {
@ -173,3 +200,30 @@ void Terrain::set_detail(size_t detail) {
size_t Terrain::get_detail() const {
return this->detail;
}
void Terrain::set_thread_count(size_t num) {
this->workload_lock.lock();
this->threads_stop = true;
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.wait_to_finish();
}
this->threads_stop = false;
this->threads.resize_initialized(num);
for (Thread &thread : this->threads) {
thread.start(&self_type::generate_meshes_thread, this);
}
}
size_t Terrain::get_thread_count() const {
return this->threads.size();
}
void Terrain::mesh_task_complete() {
this->pending_tasks--;
if (this->pending_tasks == 0) {
for (TerrainModifier *mod : this->modifiers) {
mod->set_dirty(false);
}
}
}