Xbox Cloud Gaming app for PS5 Homebrew
C
perf(gpu, input, audio): compute-stage barriers, zero-alloc gamepad reports, and 100ms jitter elasticity
Rafael Abud Ferreirabd6706002e28
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cppindex ff39a92..8bcb9fa 100644--- a/src/app/stream_player.cpp+++ b/src/app/stream_player.cpp@@ -264,13 +264,13 @@ struct StreamPlayer::Impl {XC_LOGI("first audio packet decoded: %zu samples", pcm.size() / 2);}if (decoded) {- // When audio buffer builds up beyond healthy headroom (>= 80 ms) due to network bursts,+ // When audio buffer builds up beyond healthy headroom (>= 100 ms) due to network bursts,// decode stale packets in Opus to preserve continuous LPC/CELT filter history,// but skip pushing into AudioQueue until queue drains to healthy target (~60 ms).// This keeps audio latency at ~40-60 ms, perfectly in sync with 22-25 ms video,// and completely eliminates 240-700 ms bufferbloat cuts.size_t queued = media::audioQueue().queuedFrames();- if (queued >= kOpusFrameSamples * 4) {+ if (queued >= kOpusFrameSamples * 5) {continue;}media::audioPush(pcm.data(), pcm.size() / 2);diff --git a/src/display/gpu.cpp b/src/display/gpu.cppindex b33f6ba..fa44ae1 100644--- a/src/display/gpu.cpp+++ b/src/display/gpu.cpp@@ -864,13 +864,14 @@ bool videoImages(uint32_t w, uint32_t h) {// --- frames --------------------------------------------------------------------// Every image stays in GENERAL; the barriers only order the work.+// Every image stays in GENERAL; the barriers only order the work across compute, copy and blit.void barrier(VkImage image, VkImageLayout from, VkImageLayout to) {VkImageMemoryBarrier2 b{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2};- b.srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;- b.srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT;- b.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;- b.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT;+ b.srcStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_BLIT_BIT;+ b.srcAccessMask = VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT;+ b.dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_BLIT_BIT;+ b.dstAccessMask = VK_ACCESS_2_SHADER_STORAGE_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT |+ VK_ACCESS_2_SHADER_SAMPLED_READ_BIT | VK_ACCESS_2_TRANSFER_READ_BIT;b.oldLayout = from;b.newLayout = to;b.srcQueueFamilyIndex = b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;@@ -885,10 +886,11 @@ void barrier(VkImage image, VkImageLayout from, VkImageLayout to) {// Everything before (copies, dispatches) done and visible to what follows.void memoryBarrier() {VkMemoryBarrier2 b{VK_STRUCTURE_TYPE_MEMORY_BARRIER_2};- b.srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;- b.srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT;- b.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;- b.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT;+ b.srcStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_BLIT_BIT;+ b.srcAccessMask = VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT;+ b.dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_BLIT_BIT;+ b.dstAccessMask = VK_ACCESS_2_SHADER_STORAGE_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT |+ VK_ACCESS_2_SHADER_SAMPLED_READ_BIT | VK_ACCESS_2_TRANSFER_READ_BIT;VkDependencyInfo dep{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};dep.memoryBarrierCount = 1;dep.pMemoryBarriers = &b;diff --git a/src/stream/input_packet.cpp b/src/stream/input_packet.cppindex a687bbd..20cfd02 100644--- a/src/stream/input_packet.cpp+++ b/src/stream/input_packet.cpp@@ -12,15 +12,18 @@ namespace {constexpr size_t kHeaderSize = 14;-void put8(std::vector<uint8_t>& b, size_t at, uint8_t v) { b[at] = v; }-void put16(std::vector<uint8_t>& b, size_t at, uint16_t v) {+inline void put8(uint8_t* b, size_t at, uint8_t v) { b[at] = v; }+inline void put16(uint8_t* b, size_t at, uint16_t v) {b[at] = static_cast<uint8_t>(v);b[at + 1] = static_cast<uint8_t>(v >> 8);}-void put32(std::vector<uint8_t>& b, size_t at, uint32_t v) {+inline void put32(uint8_t* b, size_t at, uint32_t v) {for (int i = 0; i < 4; ++i) b[at + i] = static_cast<uint8_t>(v >> (8 * i));}-void put32be(std::vector<uint8_t>& b, size_t at, uint32_t v) {+inline void put8(std::vector<uint8_t>& b, size_t at, uint8_t v) { put8(b.data(), at, v); }+inline void put16(std::vector<uint8_t>& b, size_t at, uint16_t v) { put16(b.data(), at, v); }+inline void put32(std::vector<uint8_t>& b, size_t at, uint32_t v) { put32(b.data(), at, v); }+[[maybe_unused]] void put32be(std::vector<uint8_t>& b, size_t at, uint32_t v) {for (int i = 0; i < 4; ++i) b[at + i] = static_cast<uint8_t>(v >> (8 * (3 - i)));}@@ -100,20 +103,33 @@ uint32_t gamepadPhysicality(const GamepadFrame& f) {return p;}-std::vector<uint8_t> gamepadReport(uint32_t sequence, double timestampMs, const GamepadFrame& f) {- auto b = header(kReportGamepad, sequence, timestampMs, kHeaderSize + 1 + 23);+size_t writeGamepadReport(uint8_t* out, size_t outSize, uint32_t sequence, double timestampMs, const GamepadFrame& f) {+ if (!out || outSize < kGamepadReportSize) return 0;+ std::memset(out, 0, kGamepadReportSize);+ put16(out, 0, kReportGamepad);+ put32(out, 2, sequence);+ uint64_t bits;+ std::memcpy(&bits, ×tampMs, sizeof bits);+ for (int i = 0; i < 8; ++i) out[6 + i] = static_cast<uint8_t>(bits >> (8 * i));+size_t o = kHeaderSize;- put8(b, o++, 1); // frame count- put8(b, o++, f.index);- put16(b, o, f.buttons);- put16(b, o + 2, static_cast<uint16_t>(axis(f.leftX)));- put16(b, o + 4, static_cast<uint16_t>(axis(f.leftY)));- put16(b, o + 6, static_cast<uint16_t>(axis(f.rightX)));- put16(b, o + 8, static_cast<uint16_t>(axis(f.rightY)));- put16(b, o + 10, trigger(f.leftTrigger));- put16(b, o + 12, trigger(f.rightTrigger));- put32(b, o + 14, gamepadPhysicality(f)); // physical physicality (0 when idle)- put32(b, o + 18, 0); // virtual physicality (LE)+ out[o++] = 1; // frame count+ out[o++] = f.index;+ put16(out, o, f.buttons);+ put16(out, o + 2, static_cast<uint16_t>(axis(f.leftX)));+ put16(out, o + 4, static_cast<uint16_t>(axis(f.leftY)));+ put16(out, o + 6, static_cast<uint16_t>(axis(f.rightX)));+ put16(out, o + 8, static_cast<uint16_t>(axis(f.rightY)));+ put16(out, o + 10, trigger(f.leftTrigger));+ put16(out, o + 12, trigger(f.rightTrigger));+ put32(out, o + 14, gamepadPhysicality(f)); // physical physicality (0 when idle)+ put32(out, o + 18, 0); // virtual physicality (LE)+ return kGamepadReportSize;+}++std::vector<uint8_t> gamepadReport(uint32_t sequence, double timestampMs, const GamepadFrame& f) {+ std::vector<uint8_t> b(kGamepadReportSize);+ writeGamepadReport(b.data(), b.size(), sequence, timestampMs, f);return b;}diff --git a/src/stream/input_packet.h b/src/stream/input_packet.hindex 2fb74dd..2a98094 100644--- a/src/stream/input_packet.h+++ b/src/stream/input_packet.h@@ -89,6 +89,8 @@ std::vector<uint8_t> metadataReport(uint32_t sequence, double timestampMs, const// Bitmask of active controls for a gamepad frame (0 when idle, bits set for// pressed buttons, engaged triggers and deflected sticks).uint32_t gamepadPhysicality(const GamepadFrame& frame);+constexpr size_t kGamepadReportSize = 38;+size_t writeGamepadReport(uint8_t* out, size_t outSize, uint32_t sequence, double timestampMs, const GamepadFrame& frame);std::vector<uint8_t> gamepadReport(uint32_t sequence, double timestampMs, const GamepadFrame& frame);// Native mouse and keyboard, for the titles that take them (at most 30// frames per report).diff --git a/src/stream/stream_session.cpp b/src/stream/stream_session.cppindex d5bfdd9..81b71e9 100644--- a/src/stream/stream_session.cpp+++ b/src/stream/stream_session.cpp@@ -162,15 +162,19 @@ struct StreamSession::Impl {}}- static void sendBinary(const std::shared_ptr<rtc::DataChannel>& ch, const std::vector<uint8_t>& data) {- if (!ch || !ch->isOpen()) return;+ static void sendBinary(const std::shared_ptr<rtc::DataChannel>& ch, const uint8_t* data, size_t size) {+ if (!ch || !ch->isOpen() || !data || size == 0) return;try {- ch->send(reinterpret_cast<const rtc::byte*>(data.data()), data.size());+ ch->send(reinterpret_cast<const rtc::byte*>(data), size);} catch (const std::exception& e) {XC_LOGW("send on %s: %s", ch->label().c_str(), e.what());}}+ static void sendBinary(const std::shared_ptr<rtc::DataChannel>& ch, const std::vector<uint8_t>& data) {+ sendBinary(ch, data.data(), data.size());+ }+static std::string asText(const rtc::message_variant& m) {if (auto s = std::get_if<std::string>(&m)) return *s;const auto& b = std::get<rtc::binary>(m);@@ -780,7 +784,9 @@ void StreamSession::sendGamepad(const GamepadFrame& frame) {d.loggedFirstMenu = true;XC_LOGI("input: first kMenu sent (pad %u)", frame.index);}- Impl::sendBinary(d.input, gamepadReport(d.inputSequence++, d.nowMs(), frame));+ uint8_t reportBuf[stream::kGamepadReportSize];+ size_t reportLen = stream::writeGamepadReport(reportBuf, sizeof(reportBuf), d.inputSequence++, d.nowMs(), frame);+ Impl::sendBinary(d.input, reportBuf, reportLen);}void StreamSession::sendAudio(const uint8_t* opus, size_t size, uint32_t timestamp) {