Skip to content

Commit

Permalink
Now the target fps is customizable for ThreadPool when threading is n…
Browse files Browse the repository at this point in the history
…ot enabled.
  • Loading branch information
Relintai committed Sep 17, 2022
1 parent b76e803 commit 5f5ac16
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
41 changes: 30 additions & 11 deletions thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,33 @@ void ThreadPool::set_thread_fallback_count(const int value) {
_dirty = true;
}

float ThreadPool::get_max_time_per_frame() const {
return _max_time_per_frame;
}
void ThreadPool::set_max_time_per_frame(const float value) {
_max_time_per_frame = value;
}

float ThreadPool::get_max_work_per_frame_percent() const {
return _max_work_per_frame_percent;
}
void ThreadPool::set_max_work_per_frame_percent(const float value) {
_max_work_per_frame_percent = value;
_dirty = true;

apply_max_work_per_frame_percent();
}

float ThreadPool::get_max_time_per_frame() const {
return _max_time_per_frame;
float ThreadPool::get_target_fps() const {
return _target_fps;
}
void ThreadPool::set_max_time_per_frame(const float value) {
_max_time_per_frame = value;
_dirty = true;
void ThreadPool::set_target_fps(const float value) {
_target_fps = value;

apply_max_work_per_frame_percent();
}

void ThreadPool::apply_max_work_per_frame_percent() {
_max_time_per_frame = (1.0 / _target_fps) * (_max_work_per_frame_percent / 100.0);
}

bool ThreadPool::is_working() const {
Expand Down Expand Up @@ -353,11 +366,11 @@ ThreadPool::ThreadPool() {
_thread_fallback_count = 1;
}

_target_fps = GLOBAL_DEF("thread_pool/target_fps", 60);
//Todo Add help text, as this will only come into play if threading is disabled, or not available
_max_work_per_frame_percent = GLOBAL_DEF("thread_pool/max_work_per_frame_percent", 25);

//Todo this should be recalculated constantly to smooth out performance better
_max_time_per_frame = (1 / 60.0) * (_max_work_per_frame_percent / 100.0);
apply_max_work_per_frame_percent();

if (!OS::get_singleton()->can_use_threads()) {
_use_threads = false;
Expand Down Expand Up @@ -418,13 +431,19 @@ void ThreadPool::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_thread_fallback_count", "value"), &ThreadPool::set_thread_fallback_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "thread_fallback_count"), "set_thread_fallback_count", "get_thread_fallback_count");

ClassDB::bind_method(D_METHOD("get_max_time_per_frame"), &ThreadPool::get_max_time_per_frame);
ClassDB::bind_method(D_METHOD("set_max_time_per_frame", "value"), &ThreadPool::set_max_time_per_frame);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_time_per_frame"), "set_max_time_per_frame", "get_max_time_per_frame");

ClassDB::bind_method(D_METHOD("get_max_work_per_frame_percent"), &ThreadPool::get_max_work_per_frame_percent);
ClassDB::bind_method(D_METHOD("set_max_work_per_frame_percent", "value"), &ThreadPool::set_max_work_per_frame_percent);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_work_per_frame_percent"), "set_max_work_per_frame_percent", "get_max_work_per_frame_percent");

ClassDB::bind_method(D_METHOD("get_max_time_per_frame"), &ThreadPool::get_max_time_per_frame);
ClassDB::bind_method(D_METHOD("set_max_time_per_frame", "value"), &ThreadPool::set_max_time_per_frame);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_time_per_frame"), "set_max_time_per_frame", "get_max_time_per_frame");
ClassDB::bind_method(D_METHOD("get_target_fps"), &ThreadPool::get_target_fps);
ClassDB::bind_method(D_METHOD("set_target_fps", "value"), &ThreadPool::set_target_fps);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "target_fps"), "set_target_fps", "get_target_fps");

ClassDB::bind_method(D_METHOD("apply_max_work_per_frame_percent"), &ThreadPool::apply_max_work_per_frame_percent);

ClassDB::bind_method(D_METHOD("is_working"), &ThreadPool::is_working);
ClassDB::bind_method(D_METHOD("is_working_no_lock"), &ThreadPool::is_working_no_lock);
Expand Down
14 changes: 10 additions & 4 deletions thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ SOFTWARE.

#if VERSION_MAJOR > 3
#include "core/object/object.h"
#include "core/templates/vector.h"
#include "core/templates/list.h"
#include "core/templates/vector.h"
#else
#include "core/list.h"
#include "core/object.h"
#include "core/vector.h"
#include "core/list.h"
#endif

#include "core/os/semaphore.h"
Expand Down Expand Up @@ -75,11 +75,16 @@ class ThreadPool : public Object {
int get_thread_fallback_count() const;
void set_thread_fallback_count(const int value);

float get_max_time_per_frame() const;
void set_max_time_per_frame(const float value);

float get_max_work_per_frame_percent() const;
void set_max_work_per_frame_percent(const float value);

float get_max_time_per_frame() const;
void set_max_time_per_frame(const float value);
float get_target_fps() const;
void set_target_fps(const float value);

void apply_max_work_per_frame_percent();

bool is_working() const;
bool is_working_no_lock() const;
Expand Down Expand Up @@ -113,6 +118,7 @@ class ThreadPool : public Object {
int _thread_fallback_count;
float _max_work_per_frame_percent;
float _max_time_per_frame;
float _target_fps;

Vector<ThreadPoolContext *> _threads;

Expand Down

0 comments on commit 5f5ac16

Please sign in to comment.