2018-09-24 16:54:07 -07:00
|
|
|
#ifndef VOXEL_PROVIDER_THREAD_H
|
|
|
|
#define VOXEL_PROVIDER_THREAD_H
|
|
|
|
|
2019-04-28 09:58:29 -07:00
|
|
|
#include "../math/vector3i.h"
|
|
|
|
#include <core/resource.h>
|
2018-09-24 16:54:07 -07:00
|
|
|
|
|
|
|
class VoxelProvider;
|
|
|
|
class VoxelBuffer;
|
|
|
|
class Thread;
|
|
|
|
class Semaphore;
|
|
|
|
|
|
|
|
class VoxelProviderThread {
|
|
|
|
public:
|
|
|
|
struct ImmergeInput {
|
|
|
|
Vector3i origin;
|
|
|
|
Ref<VoxelBuffer> voxels;
|
2019-04-29 13:57:39 -07:00
|
|
|
int lod = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EmergeInput {
|
|
|
|
Vector3i block_position;
|
|
|
|
int lod = 0;
|
2018-09-24 16:54:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct InputData {
|
|
|
|
Vector<ImmergeInput> blocks_to_immerge;
|
2019-04-29 13:57:39 -07:00
|
|
|
Vector<EmergeInput> blocks_to_emerge;
|
2018-09-24 16:54:07 -07:00
|
|
|
Vector3i priority_block_position;
|
|
|
|
|
|
|
|
inline bool is_empty() {
|
|
|
|
return blocks_to_emerge.empty() && blocks_to_immerge.empty();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EmergeOutput {
|
|
|
|
Ref<VoxelBuffer> voxels;
|
2019-05-03 16:02:10 -07:00
|
|
|
Vector3i origin_in_voxels; // TODO Remove this, redundant now
|
|
|
|
Vector3i block_position;
|
2019-04-29 13:57:39 -07:00
|
|
|
int lod = 0;
|
2018-09-24 16:54:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Stats {
|
2019-04-29 13:57:39 -07:00
|
|
|
bool first = true;
|
|
|
|
uint64_t min_time = 0;
|
|
|
|
uint64_t max_time = 0;
|
|
|
|
int remaining_blocks = 0;
|
2018-09-24 16:54:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct OutputData {
|
|
|
|
Vector<EmergeOutput> emerged_blocks;
|
|
|
|
Stats stats;
|
|
|
|
};
|
|
|
|
|
|
|
|
VoxelProviderThread(Ref<VoxelProvider> provider, int block_size_pow2);
|
|
|
|
~VoxelProviderThread();
|
|
|
|
|
|
|
|
void push(const InputData &input);
|
|
|
|
void pop(OutputData &out_data);
|
|
|
|
|
2019-05-04 17:09:12 -07:00
|
|
|
static Dictionary to_dictionary(const Stats &stats);
|
|
|
|
|
2018-09-24 16:54:07 -07:00
|
|
|
private:
|
|
|
|
static void _thread_func(void *p_self);
|
|
|
|
|
|
|
|
void thread_func();
|
|
|
|
void thread_sync(int emerge_index, Stats stats);
|
|
|
|
|
|
|
|
private:
|
|
|
|
InputData _shared_input;
|
|
|
|
Mutex *_input_mutex;
|
|
|
|
|
|
|
|
Vector<EmergeOutput> _shared_output;
|
|
|
|
Stats _shared_stats;
|
|
|
|
Mutex *_output_mutex;
|
|
|
|
|
|
|
|
Semaphore *_semaphore;
|
|
|
|
bool _thread_exit;
|
|
|
|
Thread *_thread;
|
|
|
|
InputData _input;
|
|
|
|
Vector<EmergeOutput> _output;
|
|
|
|
int _block_size_pow2;
|
|
|
|
|
|
|
|
Ref<VoxelProvider> _voxel_provider;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // VOXEL_PROVIDER_THREAD_H
|