Skip to content

Commit 0148917

Browse files
authored
Merge pull request #898 from Just-Feeshy/main
Remove the need for `MetalKit`
2 parents 3f8401e + 5a79561 commit 0148917

6 files changed

Lines changed: 115 additions & 20 deletions

File tree

backends/gpu/metal/sources/device.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ static void set_next_fence(kore_gpu_device *device, void *fence) {
123123
}
124124

125125
void kore_metal_device_create(kore_gpu_device *device, const kore_gpu_device_wishlist *wishlist) {
126-
id<MTLDevice> metal_device = MTLCreateSystemDefaultDevice();
127-
getMetalLayer().device = metal_device;
126+
id<MTLDevice> metal_device = getMetalLayer().device;
127+
128+
if(metal_device == nil) {
129+
metal_device = MTLCreateSystemDefaultDevice();
130+
}
131+
128132
device->metal.device = (__bridge_retained void *)metal_device;
129133
device->metal.library = (__bridge_retained void *)[metal_device newDefaultLibrary];
130134

backends/gpu/metal/sources/metalunit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
#include <assert.h>
77

8-
#import <MetalKit/MTKView.h>
8+
#include <Metal/Metal.h>
9+
#include <QuartzCore/CAMetalLayer.h>
910

1011
static id<CAMetalDrawable> drawable = nil;
1112

backends/system/macos/includes/kore3/backend/BasicOpenGLView.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#ifdef KORE_METAL
2-
#import <MetalKit/MTKView.h>
2+
#import <AppKit/NSView.h>
3+
#import <Metal/Metal.h>
4+
#import <QuartzCore/CAMetalLayer.h>
5+
#import <QuartzCore/QuartzCore.h>
36
#else
47
#import <Cocoa/Cocoa.h>
58
#import <OpenGL/CGLContext.h>
@@ -13,7 +16,7 @@
1316

1417
struct kore_g5_render_target;
1518

16-
@interface BasicOpenGLView : MTKView {
19+
@interface BasicOpenGLView : NSView {
1720
@private
1821
id<MTLDevice> device;
1922
id<MTLCommandQueue> commandQueue;
@@ -34,6 +37,8 @@ struct kore_g5_render_target;
3437

3538
#ifdef KORE_METAL
3639
- (CAMetalLayer *)metalLayer;
40+
41+
- (void)updateDrawableSize;
3742
#else
3843
- (void)prepareOpenGL;
3944
- (void)switchBuffers;

backends/system/macos/sources/BasicOpenGLView.m

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
#import "BasicOpenGLView.h"
1+
#import <kore3/backend/BasicOpenGLView.h>
22

33
#include <kore3/input/keyboard.h>
44
#include <kore3/input/mouse.h>
55
#include <kore3/input/pen.h>
66
#include <kore3/system.h>
77

88
#ifdef KORE_METAL
9+
#import <AppKit/NSWindow.h>
10+
#import <AppKit/NSApplication.h>
11+
#import <AppKit/NSText.h>
12+
913
// #include <kore3/graphics5/graphics.h>
1014
#endif
1115

@@ -404,6 +408,8 @@ - (void)prepareOpenGL {
404408
- (void)update { // window resizes, moves and display changes (resize, depth and display config change)
405409
#ifdef KORE_OPENGL
406410
[super update];
411+
#else
412+
[self updateDrawableSize];
407413
#endif
408414
}
409415

@@ -418,24 +424,70 @@ - (id)initWithFrame:(NSRect)frameRect {
418424
return self;
419425
}
420426
#else
427+
- (void)updateDrawableSize { // This is the high DPI version of resize
428+
CAMetalLayer *metalLayer = (CAMetalLayer *)self.layer;
429+
NSSize size = self.bounds.size;
430+
NSSize backingSize = size;
421431

422-
static CAMetalLayer *metalLayer = NULL;
432+
backingSize = [self convertSizeToBacking:size];
433+
434+
metalLayer.contentsScale = backingSize.height / size.height;
435+
metalLayer.drawableSize = NSSizeToCGSize(backingSize);
436+
}
423437

424438
- (id)initWithFrame:(NSRect)frameRect {
425439
self = [super initWithFrame:frameRect];
426440

427-
metalLayer = (CAMetalLayer *)self.layer;
441+
if(self->device == nil) {
442+
self->device = MTLCreateSystemDefaultDevice();
443+
}
444+
445+
self.wantsLayer = YES;
446+
self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
428447

429-
// metalLayer.device = device;
448+
CAMetalLayer* metalLayer = (CAMetalLayer *)self.layer;
449+
metalLayer.device = self->device;
430450
metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
431451
metalLayer.framebufferOnly = NO;
432452
// metalLayer.presentsWithTransaction = YES;
433453

434454
metalLayer.opaque = YES;
435455
metalLayer.backgroundColor = nil;
436456

457+
[self updateDrawableSize];
437458
return self;
438459
}
460+
461+
+ (Class)layerClass {
462+
return [CAMetalLayer class];
463+
}
464+
465+
- (void)setBounds:(NSRect)bounds {
466+
[super setBounds:bounds];
467+
[self updateDrawableSize];
468+
}
469+
470+
- (BOOL)wantsUpdateLayer {
471+
return YES;
472+
}
473+
474+
- (CALayer *)makeBackingLayer {
475+
return [CAMetalLayer layer];
476+
}
477+
478+
- (void)viewDidChangeBackingProperties {
479+
[super viewDidChangeBackingProperties];
480+
[self updateDrawableSize];
481+
}
482+
483+
- (void)setFrame:(NSRect)frame {
484+
[super setFrame:frame];
485+
[self updateDrawableSize];
486+
}
487+
488+
- (CAMetalLayer *)metalLayer {
489+
return (CAMetalLayer *)self.layer;
490+
}
439491
#endif
440492

441493
- (BOOL)acceptsFirstResponder {
@@ -454,12 +506,6 @@ - (void)resize:(NSSize)size {
454506
[self setFrameSize:size];
455507
}
456508

457-
#ifdef KORE_METAL
458-
- (CAMetalLayer *)metalLayer {
459-
return (CAMetalLayer *)self.layer;
460-
}
461-
#endif
462-
463509
@end
464510

465511
void kore_copy_to_clipboard(const char *text) {

backends/system/macos/sources/system.m

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#import "BasicOpenGLView.h"
22

33
#import <Cocoa/Cocoa.h>
4+
#import <CoreVideo/CoreVideo.h>
45

56
#include <kore3/backend/HIDManager.h>
67
#include <kore3/input/keyboard.h>
78
#include <kore3/log.h>
89
#include <kore3/system.h>
910
#include <kore3/window.h>
1011

12+
#ifdef KORE_METAL
13+
#include <kore3/threads/event.h>
14+
#endif
15+
1116
bool withAutoreleasepool(bool (*f)(void)) {
1217
@autoreleasepool {
1318
return f();
@@ -41,6 +46,11 @@ - (void)windowDidBecomeMain:(NSNotification *)notification;
4146
static KoreAppDelegate *delegate;
4247
static struct HIDManager *hidManager;
4348

49+
#ifdef KORE_METAL
50+
static kore_event displayLinkEvent;
51+
static CVDisplayLinkRef displayLink;
52+
#endif
53+
4454
/*struct KoreWindow : public KoreWindowBase {
4555
NSWindow* handle;
4656
BasicOpenGLView* view;
@@ -55,6 +65,17 @@ - (void)windowDidBecomeMain:(NSNotification *)notification;
5565
CAMetalLayer *getMetalLayer(void) {
5666
return [view metalLayer];
5767
}
68+
69+
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink,
70+
const CVTimeStamp* now,
71+
const CVTimeStamp* outputTime,
72+
CVOptionFlags flagsIn,
73+
CVOptionFlags* flagsOut,
74+
void* displayLinkContext) {
75+
76+
kore_event_signal(&displayLinkEvent);
77+
return kCVReturnSuccess;
78+
}
5879
#endif
5980

6081
bool kore_internal_handle_messages(void) {
@@ -68,15 +89,17 @@ bool kore_internal_handle_messages(void) {
6889
}
6990

7091
// Sleep for a frame to limit the calls when the window is not visible.
71-
if (!window.visible) {
72-
[NSThread sleepForTimeInterval:1.0 / 60];
73-
}
92+
// if (!window.visible) {
93+
// [NSThread sleepForTimeInterval:1.0 / 60];
94+
// }
7495
return true;
7596
}
7697

7798
void swapBuffersMac(int windowId) {
7899
#ifndef KORE_METAL
79100
[windows[windowId].view switchBuffers];
101+
#else
102+
kore_event_wait(&displayLinkEvent);
80103
#endif
81104
}
82105

@@ -192,6 +215,13 @@ int kore_init(const char *name, int width, int height, kore_window_parameters *w
192215
// kore_g4_internal_init(); // TODO
193216
// kore_g4_internal_init_window(windowId, frame->depth_bits, frame->stencil_bits, true); // TODO
194217

218+
#ifdef KORE_METAL
219+
kore_event_init(&displayLinkEvent, false);
220+
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
221+
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, NULL);
222+
CVDisplayLinkStart(displayLink);
223+
#endif
224+
195225
return 0;
196226
}
197227

@@ -226,7 +256,16 @@ void kore_load_url(const char *url) {
226256
return language;
227257
}
228258

229-
void kore_internal_shutdown(void) {}
259+
void kore_internal_shutdown(void) {
260+
#ifdef KORE_METAL
261+
if (displayLink) {
262+
CVDisplayLinkStop(displayLink);
263+
CVDisplayLinkRelease(displayLink);
264+
displayLink = NULL;
265+
}
266+
kore_event_destroy(&displayLinkEvent);
267+
#endif
268+
}
230269

231270
static const char *getSavePath(void) {
232271
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);

kfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ else if (platform === Platform.OSX) {
196196
addBackend('gpu/metal');
197197
addKoreDefine('METAL');
198198
project.addLib('Metal');
199-
project.addLib('MetalKit');
199+
project.addLib('QuartzCore');
200200
}
201201
else if (graphics === GraphicsApi.OpenGL) {
202202
addBackend('gpu/opengl');

0 commit comments

Comments
 (0)