feat: implemented multithreading and chunk lods
This commit is contained in:
parent
49b65a7ade
commit
1951b560ed
6 changed files with 256 additions and 53 deletions
|
|
@ -4,11 +4,9 @@
|
|||
#include "core/object/object.h"
|
||||
#include "core/os/semaphore.h"
|
||||
#include "core/os/thread.h"
|
||||
#include "core/templates/hash_map.h"
|
||||
#include "core/variant/callable.h"
|
||||
#include "scene/main/node.h"
|
||||
#include "scene/resources/gradient.h"
|
||||
#include "scene/resources/material.h"
|
||||
#include "scene/resources/mesh.h"
|
||||
#include "scene/resources/surface_tool.h"
|
||||
#include "terrain_editor/terrain_chunk.h"
|
||||
|
|
@ -16,18 +14,29 @@
|
|||
|
||||
class TerrainMeshGenerator : public Node {
|
||||
GDCLASS(TerrainMeshGenerator, Node);
|
||||
struct TerrainMeshTask {
|
||||
Ref<ArrayMesh> mesh;
|
||||
Rect2 area;
|
||||
size_t side_points;
|
||||
Ref<SurfaceTool> out_surface;
|
||||
Callable callback;
|
||||
};
|
||||
static void _bind_methods();
|
||||
void ready();
|
||||
void enter_tree();
|
||||
void process();
|
||||
void on_configuration_changed();
|
||||
|
||||
protected:
|
||||
void _notification(int what);
|
||||
void rebuild_chunks();
|
||||
static void background_generation_thread(void *user);
|
||||
|
||||
public:
|
||||
Color color_at_height(float height) const;
|
||||
float evaluate_point(Vector2 at) const;
|
||||
void generate_grid(Rect2 area, Ref<ArrayMesh> mesh, size_t side_points);
|
||||
void generate_grid(Rect2 area, Ref<SurfaceTool> mesh, size_t side_points);
|
||||
|
||||
public:
|
||||
void push_task(Rect2 area, Ref<ArrayMesh> mesh, size_t side_points, Callable callback = Callable());
|
||||
void set_primitives(Array array);
|
||||
Array get_primitives() const;
|
||||
void set_vertex_color_gradient(Ref<Gradient> gradient);
|
||||
|
|
@ -42,14 +51,27 @@ public:
|
|||
Ref<PackedScene> get_chunk_scene() const;
|
||||
|
||||
private:
|
||||
Vector<Ref<TerrainPrimitive>> primitives{};
|
||||
Ref<SurfaceTool> surface{ memnew(SurfaceTool) };
|
||||
// main thread exclusive data
|
||||
Ref<PackedScene> chunk_scene{};
|
||||
Vector<TerrainChunk *> chunks{};
|
||||
|
||||
// worker thread owned data
|
||||
Mutex settings_lock{}; // needs to be locked to modify any of the below, worker thread locks while working
|
||||
Array primitives_buffer{}; // main thread buffer for primitives
|
||||
Vector<Ref<TerrainPrimitive>> primitives{}; // work thread buffer for primitives
|
||||
Ref<Gradient> vertex_color_gradient{};
|
||||
float color_gradient_start_height{ 0.f };
|
||||
float color_gradient_end_height{ 10.f };
|
||||
int chunks_per_side{ 10 };
|
||||
Ref<PackedScene> chunk_scene{};
|
||||
Vector<TerrainChunk *> chunks{};
|
||||
|
||||
Thread thread{};
|
||||
Mutex input_lock{};
|
||||
bool end_thread{ false };
|
||||
Vector<TerrainMeshTask> input_queue{};
|
||||
Semaphore work_signal{};
|
||||
|
||||
Mutex output_lock{};
|
||||
Vector<TerrainMeshTask> output_queue{};
|
||||
|
||||
private:
|
||||
Callable generation_changed{ callable_mp(this, &self_type::on_configuration_changed) };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue