Reformat Metal files Rewrite Metal render loop to not use DisplayLink so we can use the amazing afterMinimumDuration API call Improve frame drop algorithm Misc fixes to HDR <-> SDR switching, deprecation warning cleanup Fix AVSB mode Fix renderingBackend selected value Add Rendering Backend setting to iPhone Let users choose which renderer to use. Refactor metrics into a singleton Fix tvOS build Port moonlight-qt's Metal renderer Bump imgui to latest Misc tweaks and refactoring Switch iOS to use Opus float32 audio, in line with other clients. It was already being sent from the server as float32, so this is simply decoding it with less loss of dynamic range. Give FrameQueue a faster data structure and full test coverage. Remove pts frame pacing, it's not a fit for iOS. Don't allow very transparent opacity for middle stats overlay box Change wording to select button instead of remote button Add tvOS gesture for double-clicking remote button to show/hide stats Remove kVTDecodeFrame_1xRealTimePlayback, this was found to seriously impact decode times Move decode time stat out of async block Enabe FQ logging Misc cleanup/fixes iPad: 26 will require UIRequiresFullScreen to be false, and for all orientations to be supported Updated iPhone and iPad settings panels Add setting for graph opacity. Enable stats and graphs to be toggled via two finger swipe Fix wait time in displaylink, fix settings spacing, stats fixes Settings for frame queue size, graphs. Misc refactoring and cleanup Bump moonlight-common-c Improve logging readability Fix host frametime and frame queue stats to be recorded during input not output. Show min/max/avg decode time Add bytes per frame graph Started on settings changes, both modes working very well Misc refactoring, move graphs around, both pacing functions working well Set frameDropTarget to 2 always Use raw RTP timestamps without converting to floating point for even more accurate playback Standard frame pacing: remove +3 logic Switch to NSRunLoopCommonModes for displayLink Add frame pacing algorithm based on moonlight-qt's Pacer class as a simpler baseline Switch to os_unfair_lock, misc tweaks Large refactor that now uses VTDecompressionSession based on PR535 from felipejfc. This now allows more control over queue size and allows dropping arbitrary frames, but introduces a lot of new problems bump moonlight-common-c to fix 32-bit rollover issue LiWait timing graph Add displaylink interval graph Graphs checkpoint ImGui graph working checkpoint ImGui working checkpoint Add pending frames to stats, add a define for verbose logging DisplayLink WIP Add imgui & implot modules Frame pacing: alternate method based on thieman's pullFrames thread Frame pacing: simplify displayLink, support passing targetTimestamp down to the sample buffer Point to my common-c with some timing changes. Add stats gestures: 2-finger swipe down to show stats, and 2-finger swipe up to hide Limit refresh rate to 60 when in low power mode Display screen refresh rate in stats overlay, remove decode time and explain why it can't be measured Adjust the refresh rate of the display to match the stream framerate and improve battery life Don't use presentationTimeMs when creating CMSampleTimingInfo Fix overlayView build for iOS Refactor CADisplayLink callback for improved frame delivery. Support a user-configurable buffered frame count. [opt] Use UILabel for stats overlay
145 lines
4.4 KiB
Objective-C
145 lines
4.4 KiB
Objective-C
/*
|
||
See the LICENSE.txt file for this sample’s licensing information.
|
||
|
||
Abstract:
|
||
The implementation of the cross-platform game view controller.
|
||
*/
|
||
|
||
#import "MetalViewController.h"
|
||
#import "FrameQueue.h"
|
||
#import "ImGuiRenderer.h"
|
||
#import "MetalVideoRenderer.h"
|
||
|
||
@implementation MetalViewController {
|
||
/// A queue to initialize the renderer asynchronously from the main thread.
|
||
dispatch_queue_t _dispatch_queue;
|
||
FrameQueue *_frameQueue;
|
||
float _framerate;
|
||
BOOL _enableHdr;
|
||
MetalView *_metalView;
|
||
MetalVideoRenderer *_renderer;
|
||
MetricsHandler _metricsHandler;
|
||
BOOL _stopping;
|
||
}
|
||
|
||
- (nonnull instancetype)initWithFrame:(CGRect)bounds framerate:(float)framerate enableHdr:(BOOL)enableHdr metricsHandler:(MetricsHandler)metricsHandler {
|
||
self = [super init];
|
||
if (self) {
|
||
_bounds = bounds;
|
||
_frameQueue = [FrameQueue sharedInstance];
|
||
_framerate = framerate;
|
||
_enableHdr = enableHdr;
|
||
_metricsHandler = metricsHandler;
|
||
_stopping = NO;
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (void)loadView {
|
||
self.view = [[MetalView alloc] initWithFrame:_bounds];
|
||
}
|
||
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
|
||
/// A queue to initialize the renderer asynchronously from the main thread.
|
||
_dispatch_queue = dispatch_queue_create("com.moonlight.Metal", DISPATCH_QUEUE_CONCURRENT);
|
||
|
||
__block MetalView *view = (MetalView *)self.view;
|
||
if (!view) {
|
||
Log(LOG_E, @"The view attached to MetalViewController isn't a MetalView.");
|
||
return;
|
||
}
|
||
_metalView = view;
|
||
_metalView.delegate = self;
|
||
_metalView.framerate = _framerate;
|
||
|
||
// Select the device to render with.
|
||
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
|
||
if (!device) {
|
||
Log(LOG_E, @"Metal isn't supported on this device.");
|
||
self.view = [[PlatformView alloc] initWithFrame:self.view.frame];
|
||
return;
|
||
}
|
||
view.metalLayer.device = device;
|
||
|
||
// Initialize the renderer.
|
||
MetalVideoRenderer *renderer = [[MetalVideoRenderer alloc] initWithMetalDevice:device
|
||
drawablePixelFormat:MTLPixelFormatBGR10A2Unorm
|
||
framerate:self->_framerate];
|
||
if (!renderer) {
|
||
Log(LOG_E, @"The renderer couldn't be initialized.");
|
||
return;
|
||
}
|
||
|
||
// Initialize the renderer-dependent view properties.
|
||
view.metalLayer.pixelFormat = renderer.colorPixelFormat;
|
||
view.metalLayer.colorspace = renderer.colorspace;
|
||
view.metalLayer.maximumDrawableCount = 3;
|
||
|
||
self->_renderer = renderer;
|
||
}
|
||
|
||
- (void)waitToRenderTo:(nonnull CAMetalLayer *)layer {
|
||
if (!_stopping) {
|
||
// Renderer obtains a nextDrawable, waiting if necessary
|
||
FQLog(LOG_I, @"[MetalViewController] caling [_renderer waitToRenderTo:layer]");
|
||
[_renderer waitToRenderTo:layer];
|
||
|
||
// If we don't have a frame yet, wait on that too
|
||
FQLog(LOG_I, @"[MetalViewController] caling [_renderer waitForEnqueue]");
|
||
[_frameQueue waitForEnqueue];
|
||
}
|
||
}
|
||
|
||
/// Draw frame (used by manual loop)
|
||
- (void)renderTo:(nonnull CAMetalLayer *)layer {
|
||
if (!_renderer) {
|
||
return;
|
||
}
|
||
|
||
CFTimeInterval timeout = (1.0f / _framerate) - _renderer.averageGPUTime;
|
||
FQLog(LOG_I, @"[MetalViewController] caling [_frameQueue dequeueWithTimeout:%f]", timeout);
|
||
Frame *frame = [_frameQueue dequeueWithTimeout:timeout];
|
||
if (frame) {
|
||
FQLog(LOG_I, @"[MetalViewController] calling [_renderer renderFrame] %@", frame);
|
||
[_renderer renderFrame:frame toLayer:layer];
|
||
}
|
||
}
|
||
|
||
- (void)drawableResize:(CGSize)size {
|
||
[_renderer drawableResize:size];
|
||
}
|
||
|
||
#if TARGET_OS_IOS
|
||
/// Hides the Home indicator button automatically.
|
||
- (BOOL)prefersHomeIndicatorAutoHidden {
|
||
return YES;
|
||
}
|
||
#endif
|
||
|
||
#if TARGET_OS_OSX
|
||
/// Makes the view controller the first responder to receive keyboard events.
|
||
- (void)viewDidAppear {
|
||
[_metalView.window makeFirstResponder:self];
|
||
}
|
||
|
||
/// Receives the keydown events to avoid system beeps.
|
||
///
|
||
/// The `GameInputKeyboardMouse` class handles keyboard events.
|
||
- (void)keyDown:(NSEvent *)event {
|
||
// Reference the parameter to avoid an unused parameter warning.
|
||
(void)(event);
|
||
}
|
||
|
||
/// Receives the keyup events to avoid system beeps.
|
||
///
|
||
/// The `GameInputKeyboardMouse` class handles keyboard events.
|
||
- (void)keyUp:(NSEvent *)event {
|
||
// Reference the parameter to avoid an unused parameter warning.
|
||
(void)(event);
|
||
}
|
||
#endif
|
||
|
||
@end
|