Skip to content
Merged
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
9 changes: 9 additions & 0 deletions core/libs/prx/libSceVideoOut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ if(BUILD_TESTING)
set_tests_properties(pad_input_mapping PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(pad_output_pacing_tests tests/PadOutputPacing.cpp)
target_include_directories(pad_output_pacing_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(pad_output_pacing_tests PRIVATE libc)
configure_windows_unwind(pad_output_pacing_tests)
add_test(NAME pad_output_pacing COMMAND pad_output_pacing_tests)
if(WIN32)
set_tests_properties(pad_output_pacing PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endif()

if(BUILD_TESTING)
Expand Down
3 changes: 3 additions & 0 deletions core/libs/prx/libSceVideoOut/include/PadInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "SDL_gamecontroller.h"
#include "prx/libScePad/include/KeyboardMouse.hpp"
#include "prx/libScePad/include/PadState.hpp"
#include "prx/libSceVideoOut/include/PadOutputPacing.hpp"
#include <chrono>

class DisplayWindow;
Expand Down Expand Up @@ -35,6 +36,8 @@ class PadInput {
PadOutputState outputState{};
bool outputPending = false;
std::chrono::steady_clock::time_point nextRumbleRefresh{};
LightBarPacer lightBar;
TriggerEffectCache triggerEffect;
};

#endif
63 changes: 63 additions & 0 deletions core/libs/prx/libSceVideoOut/include/PadOutputPacing.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef CORE_LIBS_PRX_LIBSCEVIDEOOUT_PADOUTPUTPACING_HPP
#define CORE_LIBS_PRX_LIBSCEVIDEOOUT_PADOUTPUTPACING_HPP

#include <array>
#include <chrono>
#include <cstdint>

class LightBarPacer {
public:
using Clock = std::chrono::steady_clock;
using Color = std::array<std::uint8_t, 3>;
static constexpr std::chrono::milliseconds Interval{30};

bool Pending(const Color& color) const {
return !sent || color != last;
}

bool Due(const Color& color, Clock::time_point now) const {
return Pending(color) && now >= next;
}

void Result(const Color& color, Clock::time_point now, bool succeeded) {
next = now + Interval;
if (!succeeded) return;
last = color;
sent = true;
}

void Reset() {
sent = false;
next = {};
}

private:
Color last{};
bool sent = false;
Clock::time_point next{};
};

class TriggerEffectCache {
public:
using Effect = std::array<std::uint8_t, 47>;

bool NeedsSend(const Effect& effect) const {
return !sent || effect != last;
}

void Result(const Effect& effect, bool succeeded) {
if (!succeeded) return;
last = effect;
sent = true;
}

void Reset() {
sent = false;
}

private:
Effect last{};
bool sent = false;
};

#endif
22 changes: 14 additions & 8 deletions core/libs/prx/libSceVideoOut/src/PadInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ void PadInput::openController(int deviceIndex) {
SDL_GameControllerGetNumTouchpads(controller), SDL_GameControllerHasLED(controller) == SDL_TRUE, SDL_GameControllerHasRumbleTriggers(controller) == SDL_TRUE);
enableSensors();
outputPending = true;
lightBar.Reset();
triggerEffect.Reset();
}

void PadInput::enableSensors() {
Expand Down Expand Up @@ -90,6 +92,12 @@ void PadInput::applyOutput() {
const auto small = isPs5 ? outputState.vibrationSmall : std::max(outputState.vibrationSmall, outputState.audioVibrationSmall);
const bool rumbling = large != 0 || small != 0;
const bool triggerRumble = outputState.trigger[0].fallback != 0 || outputState.trigger[1].fallback != 0;
if (SDL_GameControllerHasLED(controller) == SDL_TRUE) {
const LightBarPacer::Color color = outputState.lightBarValid
? LightBarPacer::Color{outputState.lightBar[0], outputState.lightBar[1], outputState.lightBar[2]}
: LightBarPacer::Color{0, 64, 255};
if (lightBar.Due(color, now)) lightBar.Result(color, now, SDL_GameControllerSetLED(controller, color[0], color[1], color[2]) == 0);
}
if (!outputPending) {
if ((rumbling || (triggerRumble && !isPs5)) && now >= nextRumbleRefresh) outputPending = true;
else return;
Expand All @@ -98,17 +106,15 @@ void PadInput::applyOutput() {
nextRumbleRefresh = now + std::chrono::milliseconds(700);
constexpr Uint32 rumbleMs = 2000;
SDL_GameControllerRumble(controller, static_cast<Uint16>(large * 257), static_cast<Uint16>(small * 257), rumbling ? rumbleMs : 0);
if (SDL_GameControllerHasLED(controller) == SDL_TRUE) {
if (outputState.lightBarValid) SDL_GameControllerSetLED(controller, outputState.lightBar[0], outputState.lightBar[1], outputState.lightBar[2]);
else SDL_GameControllerSetLED(controller, 0, 64, 255);
}
if (outputState.triggerTouched) {
if (isPs5) {
Uint8 effect[47] = {};
TriggerEffectCache::Effect effect{};
effect[0] = 0x04 | 0x08;
std::memcpy(effect + 10, outputState.trigger[1].effect, 11);
std::memcpy(effect + 21, outputState.trigger[0].effect, 11);
SDL_GameControllerSendEffect(controller, effect, sizeof(effect));
std::memcpy(effect.data() + 10, outputState.trigger[1].effect, 11);
std::memcpy(effect.data() + 21, outputState.trigger[0].effect, 11);
if (triggerEffect.NeedsSend(effect)) {
triggerEffect.Result(effect, SDL_GameControllerSendEffect(controller, effect.data(), static_cast<int>(effect.size())) == 0);
}
} else if (SDL_GameControllerHasRumbleTriggers(controller) == SDL_TRUE) {
SDL_GameControllerRumbleTriggers(controller, static_cast<Uint16>(outputState.trigger[0].fallback * 257), static_cast<Uint16>(outputState.trigger[1].fallback * 257), triggerRumble ? rumbleMs : 0);
}
Expand Down
89 changes: 89 additions & 0 deletions core/libs/prx/libSceVideoOut/tests/PadOutputPacing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "prx/libSceVideoOut/include/PadOutputPacing.hpp"

#include <chrono>
#include <cstdio>
#include <cstdlib>

namespace {

using namespace std::chrono_literals;

void Require(bool condition, const char* message) {
if (condition) return;
std::fprintf(stderr, "pad_output_pacing: %s\n", message);
std::exit(1);
}

void LightBarSendsChangesOnly() {
LightBarPacer pacer;
const auto start = LightBarPacer::Clock::time_point{} + 1s;
const LightBarPacer::Color blue{0, 64, 255};
Require(pacer.Due(blue, start), "the first colour is sent at once");
pacer.Result(blue, start, true);
Require(!pacer.Pending(blue), "a colour that was sent is not sent again");
Require(!pacer.Due(blue, start + 1s), "an unchanged colour stays quiet");
const LightBarPacer::Color red{255, 0, 0};
Require(pacer.Pending(red) && !pacer.Due(red, start + 10ms), "a new colour waits for the interval");
Require(pacer.Due(red, start + LightBarPacer::Interval), "a new colour goes out once the interval has passed");
}

void LightBarRetriesFailures() {
LightBarPacer pacer;
const auto start = LightBarPacer::Clock::time_point{} + 1s;
const LightBarPacer::Color green{0, 255, 0};
pacer.Result(green, start, false);
Require(pacer.Pending(green), "a colour SDL refused is still pending");
Require(!pacer.Due(green, start + 1ms), "a refused colour is not retried on the next frame");
Require(pacer.Due(green, start + LightBarPacer::Interval), "a refused colour is retried after the interval");
pacer.Result(green, start + LightBarPacer::Interval, true);
Require(!pacer.Pending(green), "a retried colour that was sent is done");
pacer.Reset();
Require(pacer.Due(green, start + LightBarPacer::Interval), "a new controller gets the current colour");
}

void LightBarAnimationIsPaced() {
LightBarPacer pacer;
const auto start = LightBarPacer::Clock::time_point{} + 1s;
constexpr int frames = 120;
int sent = 0;
LightBarPacer::Color color{};
for (int frame = 0; frame < frames; ++frame) {
const auto now = start + std::chrono::microseconds(frame * 8333);
color = {static_cast<std::uint8_t>(frame), 32, 32};
if (!pacer.Due(color, now)) continue;
pacer.Result(color, now, true);
++sent;
}
Require(sent >= 30 && sent <= 34, "a colour that changes every frame at 120 Hz goes out about 33 times a second");
const auto end = start + 1s;
Require(pacer.Pending(color), "the last colour of the animation is still owed");
Require(pacer.Due(color, end + LightBarPacer::Interval), "the last colour of the animation is sent when the interval allows");
}

void TriggerEffectRetriesFailures() {
TriggerEffectCache cache;
TriggerEffectCache::Effect effect{};
effect[0] = 0x0C;
effect[10] = 0x21;
Require(cache.NeedsSend(effect), "the first effect is sent");
cache.Result(effect, false);
Require(cache.NeedsSend(effect), "an effect SDL refused is sent again");
cache.Result(effect, true);
Require(!cache.NeedsSend(effect), "an effect that was sent is not repeated");
auto changed = effect;
changed[21] = 0x26;
Require(cache.NeedsSend(changed), "a changed effect is sent");
cache.Reset();
Require(cache.NeedsSend(effect), "a new controller gets the current effect");
}

}

int main() {
LightBarSendsChangesOnly();
LightBarRetriesFailures();
LightBarAnimationIsPaced();
TriggerEffectRetriesFailures();
std::puts("pad_output_pacing: ok");
return 0;
}
Loading