- unify pixel format to MTLPixelFormatRGBA16Float

- move renderingBackEnd to experimental section, add a few tips for performance mode
This commit is contained in:
TrueZhuanjia committed 2025-08-30 18:54:30 +08:00
1 parent 4d783649f5
commit 5903b358ee
7 files changed
+71 -31

No files matched your search

+4 -1
View File
@@ -12,6 +12,7 @@
#import "ConnectionCallbacks.h"
#import "Frame.h"
#import "Plot.h"
#import "TemporarySettings.h"
@interface MetalVideoRenderer : NSObject
@@ -23,7 +24,9 @@
@property (atomic) BOOL isStopping;
@property (nonatomic) BOOL hdrEnabled;
- (nonnull instancetype)initWithMetalDevice:(nonnull id<MTLDevice>)device drawablePixelFormat:(MTLPixelFormat)drawablePixelFormat framerate:(float)framerate hdrEnabled:(BOOL)hdrEnabled;
- (instancetype _Nonnull )initWithMetalDevice:(id<MTLDevice>_Nonnull)device drawablePixelFormat:(MTLPixelFormat)drawablePixelFormat settings:(TemporarySettings* _Nonnull )currentSettings;
- (void)renderFrame:(nonnull Frame *)frame toLayer:(nonnull CAMetalLayer *)layer;
- (void)waitToRenderTo:(nonnull CAMetalLayer *)layer API_AVAILABLE(ios(13.0));
- (void)drawableResize:(CGSize)drawableSize;
+19 -12
View File
@@ -130,21 +130,24 @@ CFStringRef __currentColorSpace;
size_t _lastDrawableHeight;
id<MTLBuffer> _CscParamsBuffer;
id<MTLBuffer> _VideoVertexBuffer;
CFStringRef _nonFullHdrColorSpace;
MTLPixelFormat _nonFullHdrPixelFormat;
// https://developer.apple.com/documentation/metal/synchronizing-cpu-and-gpu-work?language=objc
dispatch_semaphore_t _inFlightSemaphore;
}
- (instancetype)initWithMetalDevice:(id<MTLDevice>)device drawablePixelFormat:(MTLPixelFormat)drawablePixelFormat framerate:(float)framerate hdrEnabled:(BOOL)hdrEnabled {
- (instancetype)initWithMetalDevice:(id<MTLDevice>)device drawablePixelFormat:(MTLPixelFormat)drawablePixelFormat settings:(TemporarySettings* )currentSettings {
self = [super init];
if (self) {
_sq = dispatch_queue_create("com.moonlight.MetalVideoRenderer",
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0));
_averageGPUTime = (1.0f / framerate) / 2;
_averageGPUTime = (1.0f / currentSettings.framerate.floatValue) / 2;
_device = device;
_colorPixelFormat = drawablePixelFormat;
_framerate = framerate;
_hdrEnabled = hdrEnabled;
_framerate = currentSettings.framerate.floatValue;
_hdrEnabled = currentSettings.enableHdr;
_commandQueue = [_device newCommandQueue];
_currentEDRHeadroom = 1.0f;
_lastColorSpace = -1;
@@ -170,6 +173,10 @@ CFStringRef __currentColorSpace;
_renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0);
_renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
if (@available(iOS 14.0, *)) _nonFullHdrColorSpace = kCGColorSpaceITUR_2100_PQ;
else _nonFullHdrColorSpace = kCGColorSpaceITUR_2020;
_nonFullHdrPixelFormat = MTLPixelFormatRGBA16Float;
// Initialize texture array to NULL
for (int i = 0; i < MAX_VIDEO_PLANES; i++) {
_cvMetalTextures[i] = NULL;
@@ -399,16 +406,12 @@ CFStringRef __currentColorSpace;
CFStringRef frame_trc = CFDictionaryGetValue(ext, kCVImageBufferTransferFunctionKey);
if (CFEqual(frame_trc, kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ)) {
isHDR = YES;
if (@available(iOS 14.0, *)) {
newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2100_PQ);
} else {
newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
}
newPixelFormat = MTLPixelFormatBGRA10_XR;
newColorSpace = CGColorSpaceCreateWithName(_nonFullHdrColorSpace);
newPixelFormat = _nonFullHdrPixelFormat;
} else {
// SDR 2020, I'm not sure it's possible to stream this though
newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
newPixelFormat = MTLPixelFormatBGRA10_XR;
newColorSpace = CGColorSpaceCreateWithName(_nonFullHdrColorSpace);
newPixelFormat = _nonFullHdrPixelFormat;
}
if (isHDR) {
paramBuffer.cscParams = (fullRange ? k_CscParams_Bt2020Full_10bit : k_CscParams_Bt2020Lim_10bit);
@@ -494,6 +497,10 @@ CFStringRef __currentColorSpace;
_lastColorSpace = colorspace;
_lastFullRange = fullRange;
}
// NSLog(@"layer.pixelFormat %lu", (unsigned long)layer.pixelFormat);
return YES;
}
+3 -4
View File
@@ -12,15 +12,14 @@
#import "ImGuiRenderer.h"
#import "MetalVideoRenderer.h"
#import "MetalView.h"
#import "TemporarySettings.h"
@interface MetalViewController : UIViewController <MetalViewDelegate>
@property (nonatomic) CGRect bounds;
- (nonnull instancetype)initWithFrame:(CGRect)bounds
framerate:(float)framerate
enableHdr:(BOOL)enableHdr
metricsHandler:(MetricsHandler _Nonnull)metricsHandler;
- (nonnull instancetype)initWithFrame:(CGRect)bounds framerate:(float)framerate settings:(TemporarySettings* _Nonnull )settings metricsHandler:(MetricsHandler _Nonnull)metricsHandler;
- (void)pauseRendering;
- (void)resumeRendering;
+5 -6
View File
@@ -14,20 +14,20 @@
@implementation MetalViewController {
FrameQueue *_frameQueue;
float _framerate;
BOOL _enableHdr;
TemporarySettings* _currentSettings;
MetalView *_metalView;
MetalVideoRenderer *_renderer;
MetricsHandler _metricsHandler;
CADisplayLink *_displayLink;
}
- (nonnull instancetype)initWithFrame:(CGRect)bounds framerate:(float)framerate enableHdr:(BOOL)enableHdr metricsHandler:(MetricsHandler)metricsHandler {
- (nonnull instancetype)initWithFrame:(CGRect)bounds framerate:(float)framerate settings:(TemporarySettings* )settings metricsHandler:(MetricsHandler)metricsHandler {
self = [super init];
if (self) {
_bounds = bounds;
_frameQueue = [FrameQueue sharedInstance];
_framerate = framerate;
_enableHdr = enableHdr;
_currentSettings = settings;
_metricsHandler = metricsHandler;
}
return self;
@@ -68,7 +68,7 @@
Log(LOG_W, @"Running on iOS Simulator, using BGRA8Unorm pixel format");
#else
// On real devices, check if we should enable HDR
if (_enableHdr) {
if (_currentSettings.enableHdr) {
pixelFormat = MTLPixelFormatBGRA10_XR;
Log(LOG_I, @"HDR enabled, using BGRA10_XR pixel format");
} else {
@@ -80,8 +80,7 @@
// Initialize the renderer.
MetalVideoRenderer *renderer = [[MetalVideoRenderer alloc] initWithMetalDevice:device
drawablePixelFormat:pixelFormat
framerate:self->_framerate
hdrEnabled:self->_enableHdr];
settings:_currentSettings];
if (!renderer) {
Log(LOG_E, @"The renderer couldn't be initialized.");
return;