From 5f5ac160c52eac5710d2ecad8e149fddf5169215 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 17 Sep 2022 23:30:15 +0200 Subject: [PATCH] Now the target fps is customizable for ThreadPool when threading is not enabled. --- thread_pool.cpp | 47 +++++++++++++++++++++++++++++++++-------------- thread_pool.h | 14 ++++++++++---- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/thread_pool.cpp b/thread_pool.cpp index 97a5fad..3782a7b 100644 --- a/thread_pool.cpp +++ b/thread_pool.cpp @@ -80,20 +80,33 @@ void ThreadPool::set_thread_fallback_count(const int value) { _dirty = true; } -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; -} - 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; - _dirty = true; +} + +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; + + apply_max_work_per_frame_percent(); +} + +float ThreadPool::get_target_fps() const { + return _target_fps; +} +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 { @@ -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; @@ -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); diff --git a/thread_pool.h b/thread_pool.h index 1b2b707..6f17454 100644 --- a/thread_pool.h +++ b/thread_pool.h @@ -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" @@ -75,11 +75,16 @@ public: 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; @@ -113,6 +118,7 @@ private: int _thread_fallback_count; float _max_work_per_frame_percent; float _max_time_per_frame; + float _target_fps; Vector _threads;