Skip to content

CHANGE: ISX-2322 Rely on module to manage polling frequency. #2200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Assets/Tests/InputSystem/CoreTests_Devices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4394,6 +4394,15 @@ public void Devices_CanSetPollingFrequency()
Assert.That(InputSystem.pollingFrequency, Is.EqualTo(120).Within(0.000001));
}

#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
[Test]
[Category("Devices")]
public void Devices_PollingFrequencyIsAtLeast60HzByDefault()
{
Assert.That(InputSystem.pollingFrequency, Is.GreaterThanOrEqualTo(60));
}
Comment on lines +4400 to +4403
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also run this test when we check XInput on Windows? I'm not sure if its ideal to do this on the package but I'd like to test that this pollingFrequency is at least affected by platforms that do expose it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a setting. The KPI branch on module "sampling performance" is testing exactly that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I also added the achievable frequency to input debugger long ago. If you set this to 250 you might get what the device and system conditions support. e.g. 125


#else
[Test]
[Category("Devices")]
public void Devices_PollingFrequencyIs60HzByDefault()
Expand All @@ -4403,6 +4412,8 @@ public void Devices_PollingFrequencyIs60HzByDefault()
Assert.That(runtime.pollingFrequency, Is.EqualTo(60).Within(0.000001));
}

#endif

[Test]
[Category("Devices")]
public unsafe void Devices_CanInterceptAndHandleDeviceCommands()
Expand Down
5 changes: 5 additions & 0 deletions Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"name": "Unity",
"expression": "6000.0.15",
"define": "UNITY_INPUT_SYSTEM_SENDPOINTERHOVERTOPARENT"
},
{
"name": "Unity",
"expression": "6000.3.0a3",
"define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY"
}
],
"noEngineReferences": false
Expand Down
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ however, it has to be formatted properly to pass verification tests.
### Changed
- Changed default input action asset name from New Controls to New Actions.
- Added disabling of action maps in rebinding UI sample.
- `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.

### Added
- 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.
Expand Down
22 changes: 21 additions & 1 deletion Packages/com.unity.inputsystem/InputSystem/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,28 @@ public InputSettings.ScrollDeltaBehavior scrollDeltaBehavior

public float pollingFrequency
{
get => m_PollingFrequency;
get
{
#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
return m_Runtime.pollingFrequency;
#else
return m_PollingFrequency;
#endif
}

set
{
////REVIEW: allow setting to zero to turn off polling altogether?
if (value <= 0)
throw new ArgumentException("Polling frequency must be greater than zero", "value");

#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
m_Runtime.pollingFrequency = value;
#else
m_PollingFrequency = value;
if (m_Runtime != null)
m_Runtime.pollingFrequency = value;
#endif
}
}

Expand Down Expand Up @@ -1888,8 +1900,10 @@ internal void InitializeData()

m_ScrollDeltaBehavior = InputSettings.ScrollDeltaBehavior.UniformAcrossAllPlatforms;

#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
// Default polling frequency is 60 Hz.
m_PollingFrequency = 60;
#endif

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

// Used by EditorInputControlLayoutCache to determine whether its state is outdated.
internal int m_LayoutRegistrationVersion;
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
private float m_PollingFrequency;
#endif

internal InputControlLayout.Collection m_Layouts;
private TypeTable m_Processors;
Expand Down Expand Up @@ -4092,7 +4108,9 @@ internal SerializedState SaveState()
return new SerializedState
{
layoutRegistrationVersion = m_LayoutRegistrationVersion,
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
pollingFrequency = m_PollingFrequency,
#endif
devices = deviceArray,
availableDevices = m_AvailableDevices?.Take(m_AvailableDeviceCount).ToArray(),
buffers = m_StateBuffers,
Expand All @@ -4118,7 +4136,9 @@ internal void RestoreStateWithoutDevices(SerializedState state)
updateMask = state.updateMask;
scrollDeltaBehavior = state.scrollDeltaBehavior;
m_Metrics = state.metrics;
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
m_PollingFrequency = state.pollingFrequency;
#endif

if (m_Settings != null)
Object.DestroyImmediate(m_Settings);
Expand Down
2 changes: 1 addition & 1 deletion Packages/com.unity.inputsystem/InputSystem/InputSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ public static event InputDeviceFindControlLayoutDelegate onFindLayoutForDevice
/// The unit is Hertz. A value of 120, for example, means that devices are sampled 120 times
/// per second.
///
/// The default polling frequency is 60 Hz.
/// The default polling frequency is at least 60 Hz or what is suitable for the target device.
///
/// For devices that are polled, the frequency setting will directly translate to changes in the
/// <see cref="InputEvent.time"/> patterns. At 60 Hz, for example, timestamps for a specific,
Expand Down
11 changes: 11 additions & 0 deletions Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,16 @@ public Action<bool> onPlayerFocusChanged

public float pollingFrequency
{
#if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
get => NativeInputSystem.GetPollingFrequency();
#else
get => m_PollingFrequency;
#endif
set
{
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
m_PollingFrequency = value;
#endif
NativeInputSystem.SetPollingFrequency(value);
}
}
Expand Down Expand Up @@ -246,7 +252,12 @@ public bool runInBackground
#if UNITY_EDITOR
private Action m_PlayerLoopInitialization;
#endif
#if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY
// From Unity 6000.3.0a2 (TODO Update comment and manifest before landing PR) this is handled by module
// and initial value is suggested by the platform based on its supported device set.
// In older version this is stored here and package override module/platform.
private float m_PollingFrequency = 60.0f;
#endif
private bool m_DidCallOnShutdown = false;
private void OnShutdown()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
"name": "com.unity.modules.unityanalytics",
"expression": "1",
"define": "UNITY_INPUT_SYSTEM_ENABLE_ANALYTICS"
},
{
"name": "Unity",
"expression": "6000.3.0a3",
"define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY"
}
],
"noEngineReferences": false
Expand Down