From 951eb8d2b32c876aea3d26c656b9eacc184bc0c3 Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Sat, 6 Jan 2024 03:44:29 -0800 Subject: [PATCH 1/2] Try moving Metal Layer handling to the pipeline --- .../Plasma/Apps/plClient/Mac-Cocoa/PLSView.mm | 33 ++--------- .../Plasma/Apps/plClient/Mac-Cocoa/main.mm | 55 ++++++++++--------- .../pfMetalPipeline/plMetalPipeline.cpp | 8 +++ .../pfMetalPipeline/plMetalPipeline.h | 1 + Sources/Plasma/NucleusLib/inc/plPipeline.h | 11 +++- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSView.mm b/Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSView.mm index a2a89b74d7..775d341f5d 100644 --- a/Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSView.mm +++ b/Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSView.mm @@ -41,7 +41,6 @@ *==LICENSE==*/ #import "PLSView.h" -#include #include #include "plMessage/plInputEventMsg.h" @@ -63,39 +62,16 @@ @interface PLSView () @property NSTrackingArea* mouseTrackingArea; -#if PLASMA_PIPELINE_METAL -@property(weak) CAMetalLayer* metalLayer; -#endif @end @implementation PLSView -// MARK: View setup -- (id)initWithFrame:(NSRect)frameRect -{ - self = [super initWithFrame:frameRect]; -#if PLASMA_PIPELINE_METAL - CAMetalLayer* layer = [CAMetalLayer layer]; - layer.contentsScale = [[NSScreen mainScreen] backingScaleFactor]; - layer.maximumDrawableCount = 3; - layer.pixelFormat = MTLPixelFormatBGR10A2Unorm; - self.layer = self.metalLayer = layer; -#endif - self.layer.backgroundColor = NSColor.blackColor.CGColor; - return self; -} - - (BOOL)acceptsFirstResponder { return YES; } -- (BOOL)wantsLayer -{ - return YES; -} - // MARK: Left mouse button - (void)mouseDown:(NSEvent*)event { @@ -275,9 +251,12 @@ - (void)resizeDrawable:(CGFloat)scaleFactor return; } -#if PLASMA_PIPELINE_METAL - _metalLayer.drawableSize = newSize; -#endif + self.layer.contentsScale = scaleFactor; + + if ([self.layer isKindOfClass:[CAMetalLayer class]]) { + ((CAMetalLayer*)self.layer).drawableSize = newSize; + } + [self.delegate renderView:self didChangeOutputSize:newSize scale:scaleFactor]; diff --git a/Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm b/Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm index 0101728f20..fb942dac82 100644 --- a/Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm +++ b/Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm @@ -42,9 +42,6 @@ // System Frameworks #import -#ifdef PLASMA_PIPELINE_GL -#import -#endif #ifdef PLASMA_PIPELINE_METAL #import #endif @@ -82,18 +79,12 @@ #include "plNetGameLib/plNetGameLib.h" #include "plProduct.h" -// Until a pipeline is integrated with macOS, need to import the -// abstract definition. -#include "plPipeline/pl3DPipeline.h" - void PumpMessageQueueProc(); extern bool gDataServerLocal; extern bool gPythonLocal; extern bool gSDLLocal; -bool NeedsResolutionUpdate = false; - std::vector args; @interface AppDelegate : NSWindowController Send(); @@ -457,20 +449,27 @@ - (void)startClient { PF_CONSOLE_INITIALIZE(Audio) - self.plsView.delegate = self; + self.renderLayer = gClient->GetPipeline()->GetRenderLayer(); + _renderLayer.backgroundColor = NSColor.blackColor.CGColor; + + self.window.contentView.layer = _renderLayer; + self.window.contentView.wantsLayer = YES; // Create a window: // Window controller [self.window setContentSize:NSMakeSize(800, 600)]; [self.window center]; [self.window makeKeyAndOrderFront:self]; - self.renderLayer = self.window.contentView.layer; - - [self.renderLayer addObserver:self - forKeyPath:@"device" - options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial - context:DeviceDidChangeContext]; - + +#ifdef PLASMA_PIPELINE_METAL + if ([_renderLayer respondsToSelector:@selector(device)]) { + [_renderLayer addObserver:self + forKeyPath:@"device" + options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial + context:DeviceDidChangeContext]; + } +#endif + if (!gClient) { exit(0); } @@ -563,7 +562,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N - (void)dealloc { - [_renderLayer removeObserver:self forKeyPath:@"device" context:DeviceDidChangeContext]; +#ifdef PLASMA_PIPELINE_METAL + if ([_renderLayer respondsToSelector:@selector(device)]) { + [_renderLayer removeObserver:self forKeyPath:@"device" context:DeviceDidChangeContext]; + } +#endif } @end diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp index 85f527ede2..c1d043d897 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp @@ -4372,6 +4372,14 @@ uint32_t plMetalPipeline::IGetBufferFormatSize(uint8_t format) const return size; } +CALayer* plMetalPipeline::GetRenderLayer() +{ + CA::MetalLayer* layer = CA::MetalLayer::layer(); + layer->setPixelFormat(MTL::PixelFormatBGR10A2Unorm); + + return reinterpret_cast(layer); +} + void plMetalPipeline::plMetalPipelineCurrentState::Reset() { fCurrentPipelineState = nullptr; diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h index 4d819490c7..70ab0d8b06 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h @@ -138,6 +138,7 @@ class plMetalPipeline : public pl3DPipeline int GetMaxAnisotropicSamples() override; int GetMaxAntiAlias(int Width, int Height, int ColorDepth) override; void ResetDisplayDevice(int Width, int Height, int ColorDepth, bool Windowed, int NumAASamples, int MaxAnisotropicSamples, bool vSync = false) override; + CALayer* GetRenderLayer() override; void RenderSpans(plDrawableSpans* ice, const std::vector& visList) override; void ISetupTransforms(plDrawableSpans* drawable, const plSpan& span, hsMatrix44& lastL2W); bool ICheckDynBuffers(plDrawableSpans* drawable, plGBufferGroup* group, const plSpan* spanBase); diff --git a/Sources/Plasma/NucleusLib/inc/plPipeline.h b/Sources/Plasma/NucleusLib/inc/plPipeline.h index 6b6f463ee5..cde4943b18 100644 --- a/Sources/Plasma/NucleusLib/inc/plPipeline.h +++ b/Sources/Plasma/NucleusLib/inc/plPipeline.h @@ -60,6 +60,11 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #define DEFAULT_SHADOWS 0 #define DEFAULT_PLANARREFLECTIONS 0 +#if __OBJC__ + @class CALayer; +#else + class CALayer; +#endif struct hsPoint3; struct hsVector3; @@ -354,7 +359,11 @@ class plPipeline : public plCreatable plDisplayMode fDesktopParams; virtual size_t GetViewStackSize() const = 0; - + +#ifdef HS_BUILD_FOR_MACOS + virtual CALayer* GetRenderLayer() { return nullptr; } +#endif + float fBackingScale = 1.0f; void SetBackingScale(float scale) { fBackingScale = scale; }; }; From ef04fe5a44928e36e62a7d1b375386e1e1e5ecce Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Tue, 9 Jan 2024 16:27:05 -0800 Subject: [PATCH 2/2] Make plPipeline::GetLayer const Co-Authored-By: Adam Johnson --- Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp | 2 +- Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h | 2 +- Sources/Plasma/NucleusLib/inc/plPipeline.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp index c1d043d897..83198a80e7 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp @@ -4372,7 +4372,7 @@ uint32_t plMetalPipeline::IGetBufferFormatSize(uint8_t format) const return size; } -CALayer* plMetalPipeline::GetRenderLayer() +CALayer* plMetalPipeline::GetRenderLayer() const { CA::MetalLayer* layer = CA::MetalLayer::layer(); layer->setPixelFormat(MTL::PixelFormatBGR10A2Unorm); diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h index 70ab0d8b06..57e8a53ba9 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h @@ -138,7 +138,7 @@ class plMetalPipeline : public pl3DPipeline int GetMaxAnisotropicSamples() override; int GetMaxAntiAlias(int Width, int Height, int ColorDepth) override; void ResetDisplayDevice(int Width, int Height, int ColorDepth, bool Windowed, int NumAASamples, int MaxAnisotropicSamples, bool vSync = false) override; - CALayer* GetRenderLayer() override; + CALayer* GetRenderLayer() const override; void RenderSpans(plDrawableSpans* ice, const std::vector& visList) override; void ISetupTransforms(plDrawableSpans* drawable, const plSpan& span, hsMatrix44& lastL2W); bool ICheckDynBuffers(plDrawableSpans* drawable, plGBufferGroup* group, const plSpan* spanBase); diff --git a/Sources/Plasma/NucleusLib/inc/plPipeline.h b/Sources/Plasma/NucleusLib/inc/plPipeline.h index cde4943b18..13f69c9f46 100644 --- a/Sources/Plasma/NucleusLib/inc/plPipeline.h +++ b/Sources/Plasma/NucleusLib/inc/plPipeline.h @@ -361,7 +361,7 @@ class plPipeline : public plCreatable virtual size_t GetViewStackSize() const = 0; #ifdef HS_BUILD_FOR_MACOS - virtual CALayer* GetRenderLayer() { return nullptr; } + virtual CALayer* GetRenderLayer() const { return nullptr; } #endif float fBackingScale = 1.0f;