From d70adc1cdfc7a87a9ff8eb50fa6e920d8d0dd0dd Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:42:10 +0000 Subject: [PATCH 1/4] chore(deps): update dependency ffmpeg/ffmpeg to v9.1 --- package-lock.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.cmake b/package-lock.cmake index 808ea10..f8cd84d 100644 --- a/package-lock.cmake +++ b/package-lock.cmake @@ -35,7 +35,7 @@ # FFmpeg, cross-compiled with its upstream configure script. # renovate: datasource=github-tags depName=FFmpeg/FFmpeg versioning=semver-coerced # extractVersion=^n(?\d+\.\d+(?:\.\d+)?)$ -set(FFMPEG_VERSION 9.0.2) +set(FFMPEG_VERSION 9.1) set(FFMPEG_NXDK_PATCH "${CMAKE_CURRENT_LIST_DIR}/src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch") CPMDeclarePackage(ffmpeg NAME ffmpeg From cde33eabb3914363705d90a3ab3f122d1392b6ac Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:23:33 -0500 Subject: [PATCH 2/4] fix(build): support FFmpeg 9.1 on nxdk --- .github/workflows/ci.yml | 1 + README.md | 1 + package-lock.cmake | 7 +++++++ src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch | 17 ++++++++++++++++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad28745..24bc9ca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -109,6 +109,7 @@ jobs: update: true install: >- bison + diffutils flex git make diff --git a/README.md b/README.md index 1bd4b89..aa85a23 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,7 @@ nxdk_dependencies=( "mingw-w64-x86_64-nasm" ) moonlight_dependencies=( + "diffutils" "patch" "mingw-w64-x86_64-doxygen" "mingw-w64-x86_64-graphviz" diff --git a/package-lock.cmake b/package-lock.cmake index f8cd84d..ef7f997 100644 --- a/package-lock.cmake +++ b/package-lock.cmake @@ -37,11 +37,18 @@ # extractVersion=^n(?\d+\.\d+(?:\.\d+)?)$ set(FFMPEG_VERSION 9.1) set(FFMPEG_NXDK_PATCH "${CMAKE_CURRENT_LIST_DIR}/src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch") +# Keep each version and patch revision in a separate checkout so FetchContent +# never reapplies patches on top of a previously patched source tree. +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${FFMPEG_NXDK_PATCH}") +file(SHA256 "${FFMPEG_NXDK_PATCH}" ffmpeg_patch_hash) +string(SUBSTRING "${ffmpeg_patch_hash}" 0 12 ffmpeg_patch_id) CPMDeclarePackage(ffmpeg NAME ffmpeg VERSION ${FFMPEG_VERSION} GIT_REPOSITORY https://github.com/FFmpeg/FFmpeg.git GIT_TAG n${FFMPEG_VERSION} + GIT_SHALLOW TRUE + SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/ffmpeg-${FFMPEG_VERSION}-${ffmpeg_patch_id}-src" # Preserve LF endings in the upstream shell scripts on Windows. GIT_CONFIG core.autocrlf=false UPDATE_DISCONNECTED YES diff --git a/src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch b/src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch index 6efadf8..039ab75 100644 --- a/src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch +++ b/src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch @@ -1,7 +1,8 @@ -Adapt FFmpeg's thread wrapper to the Xbox runtime: +Adapt FFmpeg to the Xbox runtime: - nxdk condition variables own kernel events and require explicit destruction. - Desktop Windows thread naming APIs are unavailable on Xbox. +- nxdk has VirtualAlloc but lacks VirtualProtect, and the Xbox scaler has no JIT backend. diff --git a/compat/w32pthreads.h b/compat/w32pthreads.h --- a/compat/w32pthreads.h @@ -27,3 +28,17 @@ diff --git a/compat/w32pthreads.h b/compat/w32pthreads.h typedef HRESULT (WINAPI *SetThreadDescriptionFn)(HANDLE, PCWSTR); // Although SetThreadDescription lives in kernel32.dll, on Windows Server 2016, +diff --git a/libswscale/jit.c b/libswscale/jit.c +--- a/libswscale/jit.c ++++ b/libswscale/jit.c +@@ -59,9 +59,9 @@ void ff_sws_jit_free(void *ptr, size_t size) + munmap(ptr, size); + } + +-#elif HAVE_VIRTUALALLOC ++#elif HAVE_VIRTUALALLOC && !defined(NXDK) + + #include + + void *ff_sws_jit_alloc(size_t size) + { From cfdf69504f37660413b177e6e6f476f33b1fb66d Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:39:44 -0500 Subject: [PATCH 3/4] fix(build): isolate patched nxdk-sdl3 checkouts --- package-lock.cmake | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/package-lock.cmake b/package-lock.cmake index ef7f997..28a72d4 100644 --- a/package-lock.cmake +++ b/package-lock.cmake @@ -32,23 +32,28 @@ # - `set(EXAMPLE_SHA256 )` # - `CPMDeclarePackage(...)` +# Use a fresh checkout when the upstream revision or a local patch changes. +function(_moonlight_patched_source_dir out_var name revision patch) + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${patch}") + file(SHA256 "${patch}" patch_hash) + string(SUBSTRING "${revision}" 0 12 revision_id) + string(SUBSTRING "${patch_hash}" 0 12 patch_id) + set(${out_var} "${FETCHCONTENT_BASE_DIR}/${name}-${revision_id}-${patch_id}-src" PARENT_SCOPE) +endfunction() + # FFmpeg, cross-compiled with its upstream configure script. # renovate: datasource=github-tags depName=FFmpeg/FFmpeg versioning=semver-coerced # extractVersion=^n(?\d+\.\d+(?:\.\d+)?)$ set(FFMPEG_VERSION 9.1) set(FFMPEG_NXDK_PATCH "${CMAKE_CURRENT_LIST_DIR}/src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch") -# Keep each version and patch revision in a separate checkout so FetchContent -# never reapplies patches on top of a previously patched source tree. -set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${FFMPEG_NXDK_PATCH}") -file(SHA256 "${FFMPEG_NXDK_PATCH}" ffmpeg_patch_hash) -string(SUBSTRING "${ffmpeg_patch_hash}" 0 12 ffmpeg_patch_id) +_moonlight_patched_source_dir(FFMPEG_XBOX_SOURCE_DIR ffmpeg "${FFMPEG_VERSION}" "${FFMPEG_NXDK_PATCH}") CPMDeclarePackage(ffmpeg NAME ffmpeg VERSION ${FFMPEG_VERSION} GIT_REPOSITORY https://github.com/FFmpeg/FFmpeg.git GIT_TAG n${FFMPEG_VERSION} GIT_SHALLOW TRUE - SOURCE_DIR "${FETCHCONTENT_BASE_DIR}/ffmpeg-${FFMPEG_VERSION}-${ffmpeg_patch_id}-src" + SOURCE_DIR "${FFMPEG_XBOX_SOURCE_DIR}" # Preserve LF endings in the upstream shell scripts on Windows. GIT_CONFIG core.autocrlf=false UPDATE_DISCONNECTED YES @@ -73,10 +78,12 @@ CPMDeclarePackage(lucide # ref=master set(NXDK_SDL3_COMMIT 62455b72ac80d318961c05c1230ffcb471e9f2d4) set(NXDK_SDL3_PATCH "${CMAKE_CURRENT_LIST_DIR}/src/_nxdk_compat/nxdk-sdl3.patch") +_moonlight_patched_source_dir(NXDK_SDL3_SOURCE_DIR nxdk_sdl3 "${NXDK_SDL3_COMMIT}" "${NXDK_SDL3_PATCH}") CPMDeclarePackage(nxdk_sdl3 NAME nxdk_sdl3 GIT_REPOSITORY https://github.com/Ryzee119/nxdk-sdl3.git GIT_TAG ${NXDK_SDL3_COMMIT} + SOURCE_DIR "${NXDK_SDL3_SOURCE_DIR}" GIT_CONFIG core.autocrlf=false UPDATE_DISCONNECTED YES PATCHES "${NXDK_SDL3_PATCH}" From e42d4962bd88223c4597f62e25217ebbbe1098d7 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:19:25 -0500 Subject: [PATCH 4/4] fix(streaming): replace nxdk math stubs for FFmpeg 9.1 --- cmake/moonlight-dependencies.cmake | 31 ----------- src/_nxdk_compat/ffmpeg_compat.h | 12 +++++ src/_nxdk_compat/ffmpeg_math.h | 69 ++++++++++++++++++++++++ tests/unit/platform/fenv_compat_test.cpp | 41 +++++++++++++- 4 files changed, 121 insertions(+), 32 deletions(-) create mode 100644 src/_nxdk_compat/ffmpeg_math.h diff --git a/cmake/moonlight-dependencies.cmake b/cmake/moonlight-dependencies.cmake index e9be55c..7d9803b 100644 --- a/cmake/moonlight-dependencies.cmake +++ b/cmake/moonlight-dependencies.cmake @@ -1,33 +1,5 @@ include_guard(GLOBAL) -# Disable nxdk's unimplemented math entry points; aligned allocation uses private adapters. -function(_moonlight_patch_ffmpeg_config_header ffmpeg_config_header) - if(NOT EXISTS "${ffmpeg_config_header}") - return() - endif() - - file(READ "${ffmpeg_config_header}" ffmpeg_config_text) - - foreach(ffmpeg_disabled_probe - IN ITEMS - HAVE_EXP2 - HAVE_EXP2F - HAVE_LLRINT - HAVE_LLRINTF - HAVE_LOG2 - HAVE_LOG2F - HAVE_LRINT - HAVE_LRINTF) - string(REGEX REPLACE - "#define ${ffmpeg_disabled_probe} [0-9]+" - "#define ${ffmpeg_disabled_probe} 0" - ffmpeg_config_text - "${ffmpeg_config_text}") - endforeach() - - file(WRITE "${ffmpeg_config_header}" "${ffmpeg_config_text}") -endfunction() - # Validate FFmpeg source and support files before attempting a configure. function(_moonlight_validate_xbox_ffmpeg_inputs ffmpeg_source_dir) if(NOT EXISTS "${ffmpeg_source_dir}/configure") @@ -286,9 +258,6 @@ function(_moonlight_rebuild_xbox_ffmpeg nxdk_dir ffmpeg_source_dir ffmpeg_build_ ${ffmpeg_configure_args}) endif() - set(ffmpeg_config_header "${ffmpeg_build_dir}/config.h") - _moonlight_patch_ffmpeg_config_header("${ffmpeg_config_header}") - if(CMAKE_HOST_WIN32) _moonlight_run_windows_ffmpeg_command( "FFmpeg build" diff --git a/src/_nxdk_compat/ffmpeg_compat.h b/src/_nxdk_compat/ffmpeg_compat.h index 8996765..fbeb295 100644 --- a/src/_nxdk_compat/ffmpeg_compat.h +++ b/src/_nxdk_compat/ffmpeg_compat.h @@ -13,6 +13,18 @@ #include #include +#if defined(NXDK) && !defined(__cplusplus) + #include "ffmpeg_math.h" + + // FFmpeg 9.1 calls these functions directly. nxdk's symbols assert at runtime. + #define exp2 moonlight_ffmpeg_exp2 ///< Use the private double-precision exponential. + #define exp2f moonlight_ffmpeg_exp2f ///< Use the private single-precision exponential. + #define lrint moonlight_ffmpeg_lrint ///< Round a double to the Xbox long type. + #define lrintf moonlight_ffmpeg_lrintf ///< Round a float to the Xbox long type. + #define llrint moonlight_ffmpeg_llrint ///< Round a double to a 64-bit integer. + #define llrintf moonlight_ffmpeg_llrintf ///< Round a float to a 64-bit integer. +#endif + #ifdef NXDK #ifdef __cplusplus extern "C" { diff --git a/src/_nxdk_compat/ffmpeg_math.h b/src/_nxdk_compat/ffmpeg_math.h new file mode 100644 index 0000000..bd64d3e --- /dev/null +++ b/src/_nxdk_compat/ffmpeg_math.h @@ -0,0 +1,69 @@ +/** + * @file src/_nxdk_compat/ffmpeg_math.h + * @brief Supply the math functions used by FFmpeg that nxdk leaves unimplemented. + */ +#pragma once + +#include +#include + +/** + * @brief Evaluate a base-two exponential using nxdk's implemented natural exponential. + * @param exponent The exponent to raise two to. + * @return Two raised to the given exponent. + */ +static inline double moonlight_ffmpeg_exp2(double exponent) { + const double natural_log_of_two = 0x1.62e42fefa39efp-1; + return exp(exponent * natural_log_of_two); +} + +/** + * @brief Evaluate a single-precision base-two exponential. + * @param exponent The exponent to raise two to. + * @return Two raised to the given exponent, converted to float. + */ +static inline float moonlight_ffmpeg_exp2f(float exponent) { + return (float) moonlight_ffmpeg_exp2(exponent); +} + +#if defined(__i386__) || defined(__x86_64__) +/** + * @brief Round a double to the Xbox's 32-bit long using the current x87 rounding mode. + * @param value The value to round. + * @return The rounded integer, or the x87 integer indefinite value if it is unrepresentable. + */ +static inline long moonlight_ffmpeg_lrint(double value) { + int32_t result; + __asm__ volatile("fistpl %0" : "=m"(result) : "t"(value) : "st"); + return result; +} + +/** + * @brief Round a float to the Xbox's 32-bit long using the current x87 rounding mode. + * @param value The value to round. + * @return The rounded integer, or the x87 integer indefinite value if it is unrepresentable. + */ +static inline long moonlight_ffmpeg_lrintf(float value) { + return moonlight_ffmpeg_lrint(value); +} + +/** + * @brief Round a double to a 64-bit integer using the current x87 rounding mode. + * @param value The value to round. + * @return The rounded integer, or the x87 integer indefinite value if it is unrepresentable. + */ +static inline long long moonlight_ffmpeg_llrint(double value) { + int64_t result; + __asm__ volatile("fistpll %0" : "=m"(result) : "t"(value) : "st"); + return result; +} + +/** + * @brief Round a float to a 64-bit integer using the current x87 rounding mode. + * @param value The value to round. + * @return The rounded integer, or the x87 integer indefinite value if it is unrepresentable. + */ +static inline long long moonlight_ffmpeg_llrintf(float value) { + return moonlight_ffmpeg_llrint(value); +} +#endif diff --git a/tests/unit/platform/fenv_compat_test.cpp b/tests/unit/platform/fenv_compat_test.cpp index 9eeb892..891c860 100644 --- a/tests/unit/platform/fenv_compat_test.cpp +++ b/tests/unit/platform/fenv_compat_test.cpp @@ -1,13 +1,30 @@ /** * @file tests/unit/platform/fenv_compat_test.cpp - * @brief Verifies that nxdk rounding changes preserve the other x87 control settings. + * @brief Verify the floating-point adapters used by nxdk and FFmpeg. */ +// test header include #include "src/_nxdk_compat/fenv_compat.h" +// standard includes #include +#include #include + +// lib includes #include +// local includes +#include "src/_nxdk_compat/ffmpeg_math.h" + +/** @brief Preserve FFmpeg's base-two exponential fallback for both precisions. */ +TEST(FfmpegMathTest, EvaluatesBaseTwoExponentials) { + EXPECT_DOUBLE_EQ(moonlight_ffmpeg_exp2(0.0), 1.0); + EXPECT_DOUBLE_EQ(moonlight_ffmpeg_exp2(4.0), 16.0); + EXPECT_DOUBLE_EQ(moonlight_ffmpeg_exp2(-3.0), 0.125); + EXPECT_FLOAT_EQ(moonlight_ffmpeg_exp2f(0.5F), 1.41421356F); + EXPECT_FLOAT_EQ(moonlight_ffmpeg_exp2f(-2.0F), 0.25F); +} + #if defined(__i386__) || defined(__x86_64__) namespace { @@ -65,5 +82,27 @@ namespace { } } + /** @brief FFmpeg integer conversions honor every x87 rounding mode in both precisions. */ + TEST_F(FenvCompatTest, RoundsFfmpegValuesUsingTheActiveMode) { + const std::array modes {0x0000, 0x0400, 0x0800, 0x0C00}; + const std::array expectedPositive {2, 2, 3, 2}; + const std::array expectedNegative {-2, -3, -2, -2}; + + for (std::size_t i = 0; i < modes.size(); ++i) { + ASSERT_EQ(nxdk_compat::set_x87_rounding_mode(modes[i]), 0); + EXPECT_EQ(moonlight_ffmpeg_lrint(2.5), expectedPositive[i]); + EXPECT_EQ(moonlight_ffmpeg_lrintf(-2.5F), expectedNegative[i]); + EXPECT_EQ(moonlight_ffmpeg_llrint(2.5), expectedPositive[i]); + EXPECT_EQ(moonlight_ffmpeg_llrintf(-2.5F), expectedNegative[i]); + } + } + + /** @brief FFmpeg's wide conversion preserves integers larger than an Xbox long. */ + TEST_F(FenvCompatTest, RoundsFfmpegValuesBeyondThe32BitRange) { + ASSERT_EQ(nxdk_compat::set_x87_rounding_mode(0x0000), 0); + EXPECT_EQ(moonlight_ffmpeg_llrint(4294967297.75), 4294967298LL); + EXPECT_EQ(moonlight_ffmpeg_llrintf(-4294967296.0F), -4294967296LL); + } + } // namespace #endif