Skip to content

Commit

Permalink
Try moving Metal Layer handling to the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Jan 9, 2024
1 parent 63026e7 commit 951eb8d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 54 deletions.
33 changes: 6 additions & 27 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
*==LICENSE==*/

#import "PLSView.h"
#include <Metal/Metal.h>
#include <QuartzCore/QuartzCore.h>
#include "plMessage/plInputEventMsg.h"

Expand All @@ -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
{
Expand Down Expand Up @@ -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];
Expand Down
55 changes: 29 additions & 26 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@

// System Frameworks
#import <Cocoa/Cocoa.h>
#ifdef PLASMA_PIPELINE_GL
#import <OpenGL/gl.h>
#endif
#ifdef PLASMA_PIPELINE_METAL
#import <Metal/Metal.h>
#endif
Expand Down Expand Up @@ -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<ST::string> args;

@interface AppDelegate : NSWindowController <NSApplicationDelegate,
Expand All @@ -111,7 +102,6 @@ @interface AppDelegate : NSWindowController <NSApplicationDelegate,
@property CVDisplayLinkRef displayLink;
@property dispatch_queue_t renderQueue;
@property CALayer* renderLayer;
@property(weak) PLSView* plsView;
@property PLSPatcherWindowController* patcherWindow;
@property NSModalSession currentModalSession;
@property PLSPatcher* patcher;
Expand Down Expand Up @@ -191,16 +181,18 @@ - (id)init
defer:NO];
window.backgroundColor = NSColor.blackColor;

PLSView* view = [[PLSView alloc] init];
self.plsView = view;
window.contentView = view;
[window setDelegate:self];

gClient.SetClientWindow((__bridge void *)view.layer);
PLSView* plView = [[PLSView alloc] init];
window.contentView = plView;

gClient.SetClientWindow((__bridge void *)plView.layer);
gClient.SetClientDisplay((hsWindowHndl)NULL);

self = [super initWithWindow:window];
self.window.acceptsMouseMovedEvents = YES;

plView.delegate = self;
[window setDelegate:self];

return self;
}

Expand All @@ -209,7 +201,7 @@ - (id)init
- (void)startRunLoop
{
[[NSRunLoop currentRunLoop] addPort:[NSMachPort port] forMode:@"PlasmaEventMode"];
[self.plsView setBoundsSize:self.plsView.bounds.size];
[self.window.contentView setBoundsSize:self.window.contentView.bounds.size];
auto* msg = new plDisplayScaleChangedMsg(self.window.backingScaleFactor);
msg->Send();

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CALayer*>(layer);
}

void plMetalPipeline::plMetalPipelineCurrentState::Reset()
{
fCurrentPipelineState = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class plMetalPipeline : public pl3DPipeline<plMetalDevice>
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<int16_t>& visList) override;
void ISetupTransforms(plDrawableSpans* drawable, const plSpan& span, hsMatrix44& lastL2W);
bool ICheckDynBuffers(plDrawableSpans* drawable, plGBufferGroup* group, const plSpan* spanBase);
Expand Down
11 changes: 10 additions & 1 deletion Sources/Plasma/NucleusLib/inc/plPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ You can contact Cyan Worlds, Inc. by email [email protected]
#define DEFAULT_SHADOWS 0
#define DEFAULT_PLANARREFLECTIONS 0

#if __OBJC__
@class CALayer;
#else
class CALayer;
#endif

struct hsPoint3;
struct hsVector3;
Expand Down Expand Up @@ -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; };
};
Expand Down

0 comments on commit 951eb8d

Please sign in to comment.