2018-09-25 01:54:07 +02:00
|
|
|
#ifndef VOXEL_MESH_UPDATER_H
|
|
|
|
#define VOXEL_MESH_UPDATER_H
|
|
|
|
|
|
|
|
#include <core/os/semaphore.h>
|
|
|
|
#include <core/os/thread.h>
|
2019-04-24 02:29:47 +02:00
|
|
|
#include <core/vector.h>
|
2018-09-25 01:54:07 +02:00
|
|
|
|
2019-04-25 02:00:58 +02:00
|
|
|
#include "dmc/voxel_mesher_dmc.h"
|
2018-09-25 01:54:07 +02:00
|
|
|
#include "voxel_buffer.h"
|
|
|
|
#include "voxel_mesher.h"
|
|
|
|
|
|
|
|
class VoxelMeshUpdater {
|
|
|
|
public:
|
|
|
|
struct InputBlock {
|
|
|
|
Ref<VoxelBuffer> voxels;
|
|
|
|
Vector3i position;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Input {
|
|
|
|
Vector<InputBlock> blocks;
|
|
|
|
Vector3i priority_position;
|
|
|
|
|
|
|
|
bool is_empty() const {
|
|
|
|
return blocks.empty();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OutputBlock {
|
|
|
|
Array model_surfaces;
|
|
|
|
Array smooth_surfaces;
|
|
|
|
Vector3i position;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Stats {
|
|
|
|
bool first;
|
|
|
|
uint64_t min_time;
|
|
|
|
uint64_t max_time;
|
|
|
|
uint32_t remaining_blocks;
|
|
|
|
|
2019-04-24 02:29:47 +02:00
|
|
|
Stats() :
|
|
|
|
first(true),
|
|
|
|
min_time(0),
|
|
|
|
max_time(0),
|
|
|
|
remaining_blocks(0) {}
|
2018-09-25 01:54:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Output {
|
|
|
|
Vector<OutputBlock> blocks;
|
|
|
|
Stats stats;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MeshingParams {
|
|
|
|
bool baked_ao;
|
|
|
|
float baked_ao_darkness;
|
|
|
|
|
2019-04-24 02:29:47 +02:00
|
|
|
MeshingParams() :
|
|
|
|
baked_ao(true),
|
|
|
|
baked_ao_darkness(0.75) {}
|
2018-09-25 01:54:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
VoxelMeshUpdater(Ref<VoxelLibrary> library, MeshingParams params);
|
|
|
|
~VoxelMeshUpdater();
|
|
|
|
|
|
|
|
void push(const Input &input);
|
|
|
|
void pop(Output &output);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void _thread_func(void *p_self);
|
|
|
|
void thread_func();
|
|
|
|
|
|
|
|
void thread_sync(int queue_index, Stats stats);
|
|
|
|
|
|
|
|
void process_block(const InputBlock &block, OutputBlock &output);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Input _shared_input;
|
|
|
|
Mutex *_input_mutex;
|
2018-09-29 18:52:38 +02:00
|
|
|
HashMap<Vector3i, int, Vector3iHasher> _block_indexes;
|
|
|
|
bool _needs_sort;
|
2018-09-25 01:54:07 +02:00
|
|
|
|
|
|
|
Output _shared_output;
|
|
|
|
Mutex *_output_mutex;
|
|
|
|
|
2019-04-25 02:00:58 +02:00
|
|
|
Ref<VoxelMesher> _blocky_mesher;
|
|
|
|
Ref<VoxelMesherDMC> _dmc_mesher;
|
2018-09-25 01:54:07 +02:00
|
|
|
|
|
|
|
Input _input;
|
|
|
|
Output _output;
|
|
|
|
Semaphore *_semaphore;
|
|
|
|
Thread *_thread;
|
|
|
|
bool _thread_exit;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // VOXEL_MESH_UPDATER_H
|