Skip to content

Commit 411b6a9

Browse files
committed
Merge pull request #52314 from lawnjelly/four_delta_smooth
Add frame delta smoothing option (4.x)
2 parents c97ead9 + 7925670 commit 411b6a9

9 files changed

+385
-0
lines changed

core/core_bind.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ int OS::get_low_processor_usage_mode_sleep_usec() const {
224224
return ::OS::get_singleton()->get_low_processor_usage_mode_sleep_usec();
225225
}
226226

227+
void OS::set_delta_smoothing(bool p_enabled) {
228+
::OS::get_singleton()->set_delta_smoothing(p_enabled);
229+
}
230+
231+
bool OS::is_delta_smoothing_enabled() const {
232+
return ::OS::get_singleton()->is_delta_smoothing_enabled();
233+
}
234+
227235
void OS::alert(const String &p_alert, const String &p_title) {
228236
::OS::get_singleton()->alert(p_alert, p_title);
229237
}
@@ -556,6 +564,9 @@ void OS::_bind_methods() {
556564
ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &OS::set_low_processor_usage_mode_sleep_usec);
557565
ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &OS::get_low_processor_usage_mode_sleep_usec);
558566

567+
ClassDB::bind_method(D_METHOD("set_delta_smoothing", "delta_smoothing_enabled"), &OS::set_delta_smoothing);
568+
ClassDB::bind_method(D_METHOD("is_delta_smoothing_enabled"), &OS::is_delta_smoothing_enabled);
569+
559570
ClassDB::bind_method(D_METHOD("get_processor_count"), &OS::get_processor_count);
560571
ClassDB::bind_method(D_METHOD("get_processor_name"), &OS::get_processor_name);
561572

@@ -631,6 +642,7 @@ void OS::_bind_methods() {
631642

632643
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
633644
ADD_PROPERTY(PropertyInfo(Variant::INT, "low_processor_usage_mode_sleep_usec"), "set_low_processor_usage_mode_sleep_usec", "get_low_processor_usage_mode_sleep_usec");
645+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "delta_smoothing"), "set_delta_smoothing", "is_delta_smoothing_enabled");
634646

635647
// Those default values need to be specified for the docs generator,
636648
// to avoid using values from the documentation writer's own OS instance.

core/core_bind.h

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class OS : public Object {
141141
void set_low_processor_usage_mode_sleep_usec(int p_usec);
142142
int get_low_processor_usage_mode_sleep_usec() const;
143143

144+
void set_delta_smoothing(bool p_enabled);
145+
bool is_delta_smoothing_enabled() const;
146+
144147
void alert(const String &p_alert, const String &p_title = "ALERT!");
145148
void crash(const String &p_message);
146149

core/os/os.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ int OS::get_low_processor_usage_mode_sleep_usec() const {
151151
return low_processor_usage_mode_sleep_usec;
152152
}
153153

154+
void OS::set_delta_smoothing(bool p_enabled) {
155+
_delta_smoothing_enabled = p_enabled;
156+
}
157+
158+
bool OS::is_delta_smoothing_enabled() const {
159+
return _delta_smoothing_enabled;
160+
}
161+
154162
String OS::get_executable_path() const {
155163
return _execpath;
156164
}

core/os/os.h

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class OS {
5252
bool _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
5353
bool low_processor_usage_mode = false;
5454
int low_processor_usage_mode_sleep_usec = 10000;
55+
bool _delta_smoothing_enabled = false;
5556
bool _verbose_stdout = false;
5657
bool _debug_stdout = false;
5758
String _local_clipboard;
@@ -154,6 +155,9 @@ class OS {
154155
virtual void set_low_processor_usage_mode_sleep_usec(int p_usec);
155156
virtual int get_low_processor_usage_mode_sleep_usec() const;
156157

158+
void set_delta_smoothing(bool p_enabled);
159+
bool is_delta_smoothing_enabled() const;
160+
157161
virtual Vector<String> get_system_fonts() const { return Vector<String>(); };
158162
virtual String get_system_font_path(const String &p_font_name, int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return String(); };
159163
virtual Vector<String> get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale = String(), const String &p_script = String(), int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return Vector<String>(); };

doc/classes/OS.xml

+3
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@
670670
</method>
671671
</methods>
672672
<members>
673+
<member name="delta_smoothing" type="bool" setter="set_delta_smoothing" getter="is_delta_smoothing_enabled" default="true">
674+
If [code]true[/code], the engine filters the time delta measured between each frame, and attempts to compensate for random variation. This will only operate on systems where V-Sync is active.
675+
</member>
673676
<member name="low_processor_usage_mode" type="bool" setter="set_low_processor_usage_mode" getter="is_in_low_processor_usage_mode" default="false">
674677
If [code]true[/code], the engine optimizes for low processor usage by only refreshing the screen if needed. Can improve battery consumption on mobile.
675678
</member>

doc/classes/ProjectSettings.xml

+5
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@
284284
<member name="application/config/windows_native_icon" type="String" setter="" getter="" default="&quot;&quot;">
285285
Icon set in [code].ico[/code] format used on Windows to set the game's icon. This is done automatically on start by calling [method DisplayServer.set_native_icon].
286286
</member>
287+
<member name="application/run/delta_smoothing" type="bool" setter="" getter="" default="true">
288+
Time samples for frame deltas are subject to random variation introduced by the platform, even when frames are displayed at regular intervals thanks to V-Sync. This can lead to jitter. Delta smoothing can often give a better result by filtering the input deltas to correct for minor fluctuations from the refresh rate.
289+
[b]Note:[/b] Delta smoothing is only attempted when [member display/window/vsync/vsync_mode] is set to [code]enabled[/code], as it does not work well without V-Sync.
290+
It may take several seconds at a stable frame rate before the smoothing is initially activated. It will only be active on machines where performance is adequate to render frames at the refresh rate.
291+
</member>
287292
<member name="application/run/disable_stderr" type="bool" setter="" getter="" default="false">
288293
If [code]true[/code], disables printing to standard error. If [code]true[/code], this also hides error and warning messages printed by [method @GlobalScope.push_error] and [method @GlobalScope.push_warning]. See also [member application/run/disable_stdout].
289294
Changes to this setting will only be applied upon restarting the application.

main/main.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ void Main::print_help(const char *p_binary) {
470470
OS::get_singleton()->print(" --disable-render-loop Disable render loop so rendering only occurs when called explicitly from script.\n");
471471
OS::get_singleton()->print(" --disable-crash-handler Disable crash handler when supported by the platform code.\n");
472472
OS::get_singleton()->print(" --fixed-fps <fps> Force a fixed number of frames per second. This setting disables real-time synchronization.\n");
473+
OS::get_singleton()->print(" --delta-smoothing <enable> Enable or disable frame delta smoothing ['enable', 'disable'].\n");
473474
OS::get_singleton()->print(" --print-fps Print the frames per second to the stdout.\n");
474475
OS::get_singleton()->print("\n");
475476

@@ -794,6 +795,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
794795
Vector<String> breakpoints;
795796
bool use_custom_res = true;
796797
bool force_res = false;
798+
bool delta_smoothing_override = false;
797799

798800
String default_renderer = "";
799801
String default_renderer_mobile = "";
@@ -1003,6 +1005,29 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
10031005
OS::get_singleton()->print("Missing tablet driver argument, aborting.\n");
10041006
goto error;
10051007
}
1008+
} else if (I->get() == "--delta-smoothing") {
1009+
if (I->next()) {
1010+
String string = I->next()->get();
1011+
bool recognised = false;
1012+
if (string == "enable") {
1013+
OS::get_singleton()->set_delta_smoothing(true);
1014+
delta_smoothing_override = true;
1015+
recognised = true;
1016+
}
1017+
if (string == "disable") {
1018+
OS::get_singleton()->set_delta_smoothing(false);
1019+
delta_smoothing_override = false;
1020+
recognised = true;
1021+
}
1022+
if (!recognised) {
1023+
OS::get_singleton()->print("Delta-smoothing argument not recognised, aborting.\n");
1024+
goto error;
1025+
}
1026+
N = I->next()->next();
1027+
} else {
1028+
OS::get_singleton()->print("Missing delta-smoothing argument, aborting.\n");
1029+
goto error;
1030+
}
10061031
} else if (I->get() == "--single-window") { // force single window
10071032

10081033
single_window = true;
@@ -1930,6 +1955,11 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
19301955
OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(
19311956
GLOBAL_DEF(PropertyInfo(Variant::INT, "application/run/low_processor_mode_sleep_usec", PROPERTY_HINT_RANGE, "0,33200,1,or_greater"), 6900)); // Roughly 144 FPS
19321957

1958+
GLOBAL_DEF("application/run/delta_smoothing", true);
1959+
if (!delta_smoothing_override) {
1960+
OS::get_singleton()->set_delta_smoothing(GLOBAL_GET("application/run/delta_smoothing"));
1961+
}
1962+
19331963
GLOBAL_DEF("display/window/ios/allow_high_refresh_rate", true);
19341964
GLOBAL_DEF("display/window/ios/hide_home_indicator", true);
19351965
GLOBAL_DEF("display/window/ios/hide_status_bar", true);

0 commit comments

Comments
 (0)