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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jobs:
update: true
install: >-
bison
diffutils
flex
git
make
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 0 additions & 31 deletions cmake/moonlight-dependencies.cmake
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 15 additions & 1 deletion package-lock.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,28 @@
# - `set(EXAMPLE_SHA256 <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(?<version>\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")
_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 "${FFMPEG_XBOX_SOURCE_DIR}"
# Preserve LF endings in the upstream shell scripts on Windows.
GIT_CONFIG core.autocrlf=false
UPDATE_DISCONNECTED YES
Expand All @@ -66,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}"
Expand Down
17 changes: 16 additions & 1 deletion src/_nxdk_compat/ffmpeg/ffmpeg-nxdk.patch
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <windows.h>

void *ff_sws_jit_alloc(size_t size)
{
12 changes: 12 additions & 0 deletions src/_nxdk_compat/ffmpeg_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
#include <stdio.h>
#include <sys/types.h>

#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" {
Expand Down
69 changes: 69 additions & 0 deletions src/_nxdk_compat/ffmpeg_math.h
Original file line number Diff line number Diff line change
@@ -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 <math.h>
#include <stdint.h>

/**
* @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
41 changes: 40 additions & 1 deletion tests/unit/platform/fenv_compat_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <array>
#include <cstddef>
#include <cstdint>

// lib includes
#include <lizardbyte/common/testing.h>

// 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 {

Expand Down Expand Up @@ -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
Loading