Skip to content

Commit

Permalink
Tilt: Fix some edge cases leading to division by zero and similar.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Nov 9, 2023
1 parent 25ab120 commit 4f2f1c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 17 additions & 2 deletions Core/TiltEventProcessor.cpp
Expand Up @@ -35,8 +35,11 @@ void GenerateTriggerButtonEvent(int digitalX, int digitalY);
// deadzone is normalized - 0 to 1
// sensitivity controls how fast the deadzone reaches max value
inline float ApplyDeadzone(float x, float deadzone) {
if (deadzone >= 0.99f) {
// Meaningless, and not reachable with normal controls.
return x;
}
const float factor = 1.0f / (1.0f - deadzone);

if (x > deadzone) {
return (x - deadzone) * factor + deadzone;
} else if (x < -deadzone) {
Expand All @@ -55,7 +58,9 @@ inline void ApplyInverseDeadzone(float x, float y, float *outX, float *outY, flo
}
if (circular) {
float magnitude = sqrtf(x * x + y * y);
magnitude = (magnitude + inverseDeadzone) / magnitude;
if (magnitude > 0.00001f) {
magnitude = (magnitude + inverseDeadzone) / magnitude;
}
*outX = Clamp(x * magnitude, -1.0f, 1.0f);
*outY = Clamp(y * magnitude, -1.0f, 1.0f);
} else {
Expand Down Expand Up @@ -83,6 +88,10 @@ void ProcessTilt(bool landscape, float calibrationAngle, float x, float y, float
float yAngle = angleAroundX - calibrationAngle;
float xAngle = asinf(down.x);

_dbg_assert_(!my_isnanorinf(angleAroundX));
_dbg_assert_(!my_isnanorinf(yAngle));
_dbg_assert_(!my_isnanorinf(xAngle));

float tiltX = xAngle;
float tiltY = yAngle;

Expand All @@ -107,11 +116,17 @@ void ProcessTilt(bool landscape, float calibrationAngle, float x, float y, float
float adjustedTiltX = ApplyDeadzone(tiltX, g_Config.fTiltAnalogDeadzoneRadius);
float adjustedTiltY = ApplyDeadzone(tiltY, g_Config.fTiltAnalogDeadzoneRadius);

_dbg_assert_(!my_isnanorinf(adjustedTiltX));
_dbg_assert_(!my_isnanorinf(adjustedTiltY));

// Unlike regular deadzone, where per-axis is okay, inverse deadzone (to compensate for game deadzones) really needs to be
// applied on the two axes together.
// TODO: Share this code with the joystick code. For now though, we keep it separate.
ApplyInverseDeadzone(adjustedTiltX, adjustedTiltY, &adjustedTiltX, &adjustedTiltY, g_Config.fTiltInverseDeadzone, g_Config.bTiltCircularInverseDeadzone);

_dbg_assert_(!my_isnanorinf(adjustedTiltX));
_dbg_assert_(!my_isnanorinf(adjustedTiltY));

rawTiltAnalogX = adjustedTiltX;
rawTiltAnalogY = adjustedTiltY;
GenerateAnalogStickEvent(adjustedTiltX, adjustedTiltY);
Expand Down
2 changes: 0 additions & 2 deletions GPU/Common/GPUStateUtils.h
Expand Up @@ -249,8 +249,6 @@ struct GenericLogicState {
// Same logicOp is kept.
}
}

void Log();
};

struct ComputedPipelineState {
Expand Down

0 comments on commit 4f2f1c4

Please sign in to comment.