Skip to content

Commit 775282e

Browse files
authored
CHANGE: ISX-2322 Rely on module (and platform) to manage polling frequency. (#2200)
1 parent 28b42c1 commit 775282e

File tree

8 files changed

+56
-3
lines changed

8 files changed

+56
-3
lines changed

Assets/Tests/InputSystem/CoreTests_Devices.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,6 +4394,15 @@ public void Devices_CanSetPollingFrequency()
43944394
Assert.That(InputSystem.pollingFrequency, Is.EqualTo(120).Within(0.000001));
43954395
}
43964396

4397+
#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
4398+
[Test]
4399+
[Category("Devices")]
4400+
public void Devices_PollingFrequencyIsAtLeast60HzByDefault()
4401+
{
4402+
Assert.That(InputSystem.pollingFrequency, Is.GreaterThanOrEqualTo(60));
4403+
}
4404+
4405+
#else
43974406
[Test]
43984407
[Category("Devices")]
43994408
public void Devices_PollingFrequencyIs60HzByDefault()
@@ -4403,6 +4412,8 @@ public void Devices_PollingFrequencyIs60HzByDefault()
44034412
Assert.That(runtime.pollingFrequency, Is.EqualTo(60).Within(0.000001));
44044413
}
44054414

4415+
#endif
4416+
44064417
[Test]
44074418
[Category("Devices")]
44084419
public unsafe void Devices_CanInterceptAndHandleDeviceCommands()

Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
"name": "Unity",
6666
"expression": "6000.0.15",
6767
"define": "UNITY_INPUT_SYSTEM_SENDPOINTERHOVERTOPARENT"
68+
},
69+
{
70+
"name": "Unity",
71+
"expression": "6000.3.0a6",
72+
"define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY"
6873
}
6974
],
7075
"noEngineReferences": false

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ however, it has to be formatted properly to pass verification tests.
8484
### Changed
8585
- Changed default input action asset name from New Controls to New Actions.
8686
- Added disabling of action maps in rebinding UI sample.
87+
- `InputSystem.pollingFrequency` is now initialized based on recommended polling frequency by the underlying platform on Unity versions newer than 6000.3.0a1. It can still be customized (set) during run-time as usual. Increasing polling frequency beyond the default 60Hz leads to less probability of input loss and reduces input latency.
8788

8889
### Added
8990
- An alternative way to access if an action state reached a certain phase during this rendering frame (Update()). This can be utilized even if the InputSystem update mode is set to manual or FixedUpdate. It can be used to access the action phase during rendering, eg for perform updates to the UI.

Packages/com.unity.inputsystem/InputSystem/InputManager.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,28 @@ public InputSettings.ScrollDeltaBehavior scrollDeltaBehavior
197197

198198
public float pollingFrequency
199199
{
200-
get => m_PollingFrequency;
200+
get
201+
{
202+
#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
203+
return m_Runtime.pollingFrequency;
204+
#else
205+
return m_PollingFrequency;
206+
#endif
207+
}
208+
201209
set
202210
{
203211
////REVIEW: allow setting to zero to turn off polling altogether?
204212
if (value <= 0)
205213
throw new ArgumentException("Polling frequency must be greater than zero", "value");
206214

215+
#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
216+
m_Runtime.pollingFrequency = value;
217+
#else
207218
m_PollingFrequency = value;
208219
if (m_Runtime != null)
209220
m_Runtime.pollingFrequency = value;
221+
#endif
210222
}
211223
}
212224

@@ -1888,8 +1900,10 @@ internal void InitializeData()
18881900

18891901
m_ScrollDeltaBehavior = InputSettings.ScrollDeltaBehavior.UniformAcrossAllPlatforms;
18901902

1903+
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
18911904
// Default polling frequency is 60 Hz.
18921905
m_PollingFrequency = 60;
1906+
#endif
18931907

18941908
// Register layouts.
18951909
// NOTE: Base layouts must be registered before their derived layouts
@@ -2141,7 +2155,9 @@ internal struct AvailableDevice
21412155

21422156
// Used by EditorInputControlLayoutCache to determine whether its state is outdated.
21432157
internal int m_LayoutRegistrationVersion;
2158+
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
21442159
private float m_PollingFrequency;
2160+
#endif
21452161

21462162
internal InputControlLayout.Collection m_Layouts;
21472163
private TypeTable m_Processors;
@@ -4092,7 +4108,9 @@ internal SerializedState SaveState()
40924108
return new SerializedState
40934109
{
40944110
layoutRegistrationVersion = m_LayoutRegistrationVersion,
4111+
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
40954112
pollingFrequency = m_PollingFrequency,
4113+
#endif
40964114
devices = deviceArray,
40974115
availableDevices = m_AvailableDevices?.Take(m_AvailableDeviceCount).ToArray(),
40984116
buffers = m_StateBuffers,
@@ -4118,7 +4136,9 @@ internal void RestoreStateWithoutDevices(SerializedState state)
41184136
updateMask = state.updateMask;
41194137
scrollDeltaBehavior = state.scrollDeltaBehavior;
41204138
m_Metrics = state.metrics;
4139+
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
41214140
m_PollingFrequency = state.pollingFrequency;
4141+
#endif
41224142

41234143
if (m_Settings != null)
41244144
Object.DestroyImmediate(m_Settings);

Packages/com.unity.inputsystem/InputSystem/InputSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ public static event InputDeviceFindControlLayoutDelegate onFindLayoutForDevice
13641364
/// The unit is Hertz. A value of 120, for example, means that devices are sampled 120 times
13651365
/// per second.
13661366
///
1367-
/// The default polling frequency is 60 Hz.
1367+
/// The default polling frequency is at least 60 Hz or what is suitable for the target device.
13681368
///
13691369
/// For devices that are polled, the frequency setting will directly translate to changes in the
13701370
/// <see cref="InputEvent.time"/> patterns. At 60 Hz, for example, timestamps for a specific,

Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,16 @@ public Action<bool> onPlayerFocusChanged
211211

212212
public float pollingFrequency
213213
{
214+
#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
215+
get => NativeInputSystem.GetPollingFrequency();
216+
#else
214217
get => m_PollingFrequency;
218+
#endif
215219
set
216220
{
221+
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
217222
m_PollingFrequency = value;
223+
#endif
218224
NativeInputSystem.SetPollingFrequency(value);
219225
}
220226
}
@@ -246,7 +252,12 @@ public bool runInBackground
246252
#if UNITY_EDITOR
247253
private Action m_PlayerLoopInitialization;
248254
#endif
255+
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
256+
// From Unity 6000.3.0a2 (TODO Update comment and manifest before landing PR) this is handled by module
257+
// and initial value is suggested by the platform based on its supported device set.
258+
// In older version this is stored here and package override module/platform.
249259
private float m_PollingFrequency = 60.0f;
260+
#endif
250261
private bool m_DidCallOnShutdown = false;
251262
private void OnShutdown()
252263
{

Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@
111111
"name": "com.unity.modules.unityanalytics",
112112
"expression": "1",
113113
"define": "UNITY_INPUT_SYSTEM_ENABLE_ANALYTICS"
114+
},
115+
{
116+
"name": "Unity",
117+
"expression": "6000.3.0a6",
118+
"define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY"
114119
}
115120
],
116121
"noEngineReferences": false

Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public struct PairedUser
359359
public Action onShutdown { get; set; }
360360
public Action<bool> onPlayerFocusChanged { get; set; }
361361
public bool isPlayerFocused => m_HasFocus;
362-
public float pollingFrequency { get; set; }
362+
public float pollingFrequency { get; set; } = 60.0f; // At least 60 Hz by default
363363
public double currentTime { get; set; }
364364
public double currentTimeForFixedUpdate { get; set; }
365365
public float unscaledGameTime { get; set; } = 1;

0 commit comments

Comments
 (0)