summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
authorGravatar Morph2020-10-10 09:03:47 -0400
committerGravatar Morph2020-11-15 23:33:20 -0500
commit9b501af8e3d0f6457fafb0fdfbcc11f6da4f0e8a (patch)
tree3a9cc70c7caa50eb7f3c5ede1bdddd4fa928c118 /src/input_common/sdl/sdl_impl.cpp
parentconfigure_input: Hook up the vibration percentage spinbox (diff)
downloadyuzu-9b501af8e3d0f6457fafb0fdfbcc11f6da4f0e8a.tar.gz
yuzu-9b501af8e3d0f6457fafb0fdfbcc11f6da4f0e8a.tar.xz
yuzu-9b501af8e3d0f6457fafb0fdfbcc11f6da4f0e8a.zip
controllers/npad: Add heuristics to reduce rumble state changes
Sending too many state changes in a short period of time can cause massive performance issues. As a result, we have to use several heuristics to reduce the number of state changes to minimize/eliminate this performance impact while maintaining the quality of these vibrations as much as possible.
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp54
1 files changed, 25 insertions, 29 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 10883e2d9..18fb2ac5e 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -80,30 +80,24 @@ public:
80 return static_cast<float>(state.axes.at(axis)) / (32767.0f * range); 80 return static_cast<float>(state.axes.at(axis)) / (32767.0f * range);
81 } 81 }
82 82
83 bool RumblePlay(f32 amp_low, f32 amp_high, u32 time) { 83 bool RumblePlay(u16 amp_low, u16 amp_high) {
84 const u16 raw_amp_low = static_cast<u16>(amp_low * 0xFFFF); 84 using std::chrono::duration_cast;
85 const u16 raw_amp_high = static_cast<u16>(amp_high * 0xFFFF); 85 using std::chrono::milliseconds;
86 // Lower drastically the number of state changes 86 using std::chrono::steady_clock;
87 if (raw_amp_low >> 11 == last_state_rumble_low >> 11 && 87
88 raw_amp_high >> 11 == last_state_rumble_high >> 11) { 88 // Prevent vibrations less than 10ms apart from each other.
89 if (raw_amp_low + raw_amp_high != 0 || 89 if (duration_cast<milliseconds>(steady_clock::now() - last_vibration) < milliseconds(10)) {
90 last_state_rumble_low + last_state_rumble_high == 0) { 90 return false;
91 return false; 91 };
92 } 92
93 } 93 last_vibration = steady_clock::now();
94 // Don't change state if last vibration was < 20ms 94
95 const auto now = std::chrono::system_clock::now(); 95 if (sdl_controller != nullptr) {
96 if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_vibration) < 96 return SDL_GameControllerRumble(sdl_controller.get(), amp_low, amp_high, 0) == 0;
97 std::chrono::milliseconds(20)) { 97 } else if (sdl_joystick != nullptr) {
98 return raw_amp_low + raw_amp_high == 0; 98 return SDL_JoystickRumble(sdl_joystick.get(), amp_low, amp_high, 0) == 0;
99 } 99 }
100 100
101 last_vibration = now;
102 last_state_rumble_low = raw_amp_low;
103 last_state_rumble_high = raw_amp_high;
104 if (sdl_joystick) {
105 SDL_JoystickRumble(sdl_joystick.get(), raw_amp_low, raw_amp_high, time);
106 }
107 return false; 101 return false;
108 } 102 }
109 103
@@ -172,13 +166,13 @@ private:
172 } state; 166 } state;
173 std::string guid; 167 std::string guid;
174 int port; 168 int port;
175 u16 last_state_rumble_high = 0;
176 u16 last_state_rumble_low = 0;
177 std::chrono::time_point<std::chrono::system_clock> last_vibration;
178 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; 169 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
179 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; 170 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller;
180 mutable std::mutex mutex; 171 mutable std::mutex mutex;
181 172
173 // This is the timepoint of the last vibration and is used to ensure vibrations are 10ms apart.
174 std::chrono::steady_clock::time_point last_vibration;
175
182 // Motion is initialized without PID values as motion input is not aviable for SDL2 176 // Motion is initialized without PID values as motion input is not aviable for SDL2
183 MotionInput motion{0.0f, 0.0f, 0.0f}; 177 MotionInput motion{0.0f, 0.0f, 0.0f};
184}; 178};
@@ -327,10 +321,12 @@ public:
327 return joystick->GetButton(button); 321 return joystick->GetButton(button);
328 } 322 }
329 323
330 bool SetRumblePlay(f32 amp_high, f32 amp_low, f32 freq_high, f32 freq_low) const override { 324 bool SetRumblePlay(f32 amp_low, f32 freq_low, f32 amp_high, f32 freq_high) const override {
331 const f32 new_amp_low = pow(amp_low, 0.5f) * (3.0f - 2.0f * pow(amp_low, 0.15f)); 325 const u16 processed_amp_low =
332 const f32 new_amp_high = pow(amp_high, 0.5f) * (3.0f - 2.0f * pow(amp_high, 0.15f)); 326 static_cast<u16>(pow(amp_low, 0.5f) * (3.0f - 2.0f * pow(amp_low, 0.15f)) * 0xFFFF);
333 return joystick->RumblePlay(new_amp_low, new_amp_high, 250); 327 const u16 processed_amp_high =
328 static_cast<u16>(pow(amp_high, 0.5f) * (3.0f - 2.0f * pow(amp_high, 0.15f)) * 0xFFFF);
329 return joystick->RumblePlay(processed_amp_low, processed_amp_high);
334 } 330 }
335 331
336private: 332private: