Sort thread updates only when necessary

This commit is contained in:
Marc Gilleron 2018-09-29 17:52:38 +01:00
parent e79dd98d69
commit bca6a72edd
2 changed files with 13 additions and 2 deletions

View File

@ -20,6 +20,8 @@ VoxelMeshUpdater::VoxelMeshUpdater(Ref<VoxelLibrary> library, MeshingParams para
_thread_exit = false;
_semaphore = Semaphore::create();
_thread = Thread::create(_thread_func, this);
_needs_sort = true;
}
VoxelMeshUpdater::~VoxelMeshUpdater() {
@ -75,6 +77,10 @@ void VoxelMeshUpdater::push(const Input &input) {
}
}
if(_shared_input.priority_position != input.priority_position || input.blocks.size() > 0) {
_needs_sort = true;
}
_shared_input.priority_position = input.priority_position;
should_run = !_shared_input.is_empty();
}
@ -202,6 +208,7 @@ void VoxelMeshUpdater::thread_sync(int queue_index, Stats stats) {
}
stats.remaining_blocks = _input.blocks.size();
bool needs_sort;
{
// Get input
@ -212,6 +219,9 @@ void VoxelMeshUpdater::thread_sync(int queue_index, Stats stats) {
_shared_input.blocks.clear();
_block_indexes.clear();
needs_sort = _needs_sort;
_needs_sort = false;
}
if(!_output.blocks.empty()) {
@ -226,7 +236,7 @@ void VoxelMeshUpdater::thread_sync(int queue_index, Stats stats) {
_output.blocks.clear();
}
if (!_input.blocks.empty()) {
if (!_input.blocks.empty() && needs_sort) {
// Re-sort priority
SortArray<VoxelMeshUpdater::InputBlock, BlockUpdateComparator> sorter;

View File

@ -70,6 +70,8 @@ private:
private:
Input _shared_input;
Mutex *_input_mutex;
HashMap<Vector3i, int, Vector3iHasher> _block_indexes;
bool _needs_sort;
Output _shared_output;
Mutex *_output_mutex;
@ -78,7 +80,6 @@ private:
Ref<VoxelMesherSmooth> _smooth_mesher;
Input _input;
HashMap<Vector3i, int, Vector3iHasher> _block_indexes;
Output _output;
Semaphore *_semaphore;
Thread *_thread;