Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions backends/gpu/metal/sources/device.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ static void set_next_fence(kore_gpu_device *device, void *fence) {
}

void kore_metal_device_create(kore_gpu_device *device, const kore_gpu_device_wishlist *wishlist) {
id<MTLDevice> metal_device = MTLCreateSystemDefaultDevice();
getMetalLayer().device = metal_device;
id<MTLDevice> metal_device = getMetalLayer().device;

if(metal_device == nil) {
metal_device = MTLCreateSystemDefaultDevice();
}

device->metal.device = (__bridge_retained void *)metal_device;
device->metal.library = (__bridge_retained void *)[metal_device newDefaultLibrary];

Expand Down
3 changes: 2 additions & 1 deletion backends/gpu/metal/sources/metalunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

#include <assert.h>

#import <MetalKit/MTKView.h>
#include <Metal/Metal.h>
#include <QuartzCore/CAMetalLayer.h>

static id<CAMetalDrawable> drawable = nil;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifdef KORE_METAL
#import <MetalKit/MTKView.h>
#import <AppKit/NSView.h>
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
#import <QuartzCore/QuartzCore.h>
#else
#import <Cocoa/Cocoa.h>
#import <OpenGL/CGLContext.h>
Expand All @@ -13,7 +16,7 @@

struct kore_g5_render_target;

@interface BasicOpenGLView : MTKView {
@interface BasicOpenGLView : NSView {
@private
id<MTLDevice> device;
id<MTLCommandQueue> commandQueue;
Expand All @@ -34,6 +37,8 @@ struct kore_g5_render_target;

#ifdef KORE_METAL
- (CAMetalLayer *)metalLayer;

- (void)updateDrawableSize;
#else
- (void)prepareOpenGL;
- (void)switchBuffers;
Expand Down
66 changes: 56 additions & 10 deletions backends/system/macos/sources/BasicOpenGLView.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#import "BasicOpenGLView.h"
#import <kore3/backend/BasicOpenGLView.h>

#include <kore3/input/keyboard.h>
#include <kore3/input/mouse.h>
#include <kore3/input/pen.h>
#include <kore3/system.h>

#ifdef KORE_METAL
#import <AppKit/NSWindow.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSText.h>

// #include <kore3/graphics5/graphics.h>
#endif

Expand Down Expand Up @@ -404,6 +408,8 @@ - (void)prepareOpenGL {
- (void)update { // window resizes, moves and display changes (resize, depth and display config change)
#ifdef KORE_OPENGL
[super update];
#else
[self updateDrawableSize];
#endif
}

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

static CAMetalLayer *metalLayer = NULL;
backingSize = [self convertSizeToBacking:size];

metalLayer.contentsScale = backingSize.height / size.height;
metalLayer.drawableSize = NSSizeToCGSize(backingSize);
}

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

metalLayer = (CAMetalLayer *)self.layer;
if(self->device == nil) {
self->device = MTLCreateSystemDefaultDevice();
}

self.wantsLayer = YES;
self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;

// metalLayer.device = device;
CAMetalLayer* metalLayer = (CAMetalLayer *)self.layer;
metalLayer.device = self->device;
metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
metalLayer.framebufferOnly = NO;
// metalLayer.presentsWithTransaction = YES;

metalLayer.opaque = YES;
metalLayer.backgroundColor = nil;

[self updateDrawableSize];
return self;
}

+ (Class)layerClass {
return [CAMetalLayer class];
}

- (void)setBounds:(NSRect)bounds {
[super setBounds:bounds];
[self updateDrawableSize];
}

- (BOOL)wantsUpdateLayer {
return YES;
}

- (CALayer *)makeBackingLayer {
return [CAMetalLayer layer];
}

- (void)viewDidChangeBackingProperties {
[super viewDidChangeBackingProperties];
[self updateDrawableSize];
}

- (void)setFrame:(NSRect)frame {
[super setFrame:frame];
[self updateDrawableSize];
}

- (CAMetalLayer *)metalLayer {
return (CAMetalLayer *)self.layer;
}
#endif

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

#ifdef KORE_METAL
- (CAMetalLayer *)metalLayer {
return (CAMetalLayer *)self.layer;
}
#endif

@end

void kore_copy_to_clipboard(const char *text) {
Expand Down
47 changes: 43 additions & 4 deletions backends/system/macos/sources/system.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#import "BasicOpenGLView.h"

#import <Cocoa/Cocoa.h>
#import <CoreVideo/CoreVideo.h>

#include <kore3/backend/HIDManager.h>
#include <kore3/input/keyboard.h>
#include <kore3/log.h>
#include <kore3/system.h>
#include <kore3/window.h>

#ifdef KORE_METAL
#include <kore3/threads/event.h>
#endif

bool withAutoreleasepool(bool (*f)(void)) {
@autoreleasepool {
return f();
Expand Down Expand Up @@ -41,6 +46,11 @@ - (void)windowDidBecomeMain:(NSNotification *)notification;
static KoreAppDelegate *delegate;
static struct HIDManager *hidManager;

#ifdef KORE_METAL
static kore_event displayLinkEvent;
static CVDisplayLinkRef displayLink;
#endif

/*struct KoreWindow : public KoreWindowBase {
NSWindow* handle;
BasicOpenGLView* view;
Expand All @@ -55,6 +65,17 @@ - (void)windowDidBecomeMain:(NSNotification *)notification;
CAMetalLayer *getMetalLayer(void) {
return [view metalLayer];
}

static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp* now,
const CVTimeStamp* outputTime,
CVOptionFlags flagsIn,
CVOptionFlags* flagsOut,
void* displayLinkContext) {

kore_event_signal(&displayLinkEvent);
return kCVReturnSuccess;
}
#endif

bool kore_internal_handle_messages(void) {
Expand All @@ -68,15 +89,17 @@ bool kore_internal_handle_messages(void) {
}

// Sleep for a frame to limit the calls when the window is not visible.
if (!window.visible) {
[NSThread sleepForTimeInterval:1.0 / 60];
}
// if (!window.visible) {
// [NSThread sleepForTimeInterval:1.0 / 60];
// }
return true;
}

void swapBuffersMac(int windowId) {
#ifndef KORE_METAL
[windows[windowId].view switchBuffers];
#else
kore_event_wait(&displayLinkEvent);
#endif
}

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

#ifdef KORE_METAL
kore_event_init(&displayLinkEvent, false);
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, NULL);
CVDisplayLinkStart(displayLink);
#endif

return 0;
}

Expand Down Expand Up @@ -226,7 +256,16 @@ void kore_load_url(const char *url) {
return language;
}

void kore_internal_shutdown(void) {}
void kore_internal_shutdown(void) {
#ifdef KORE_METAL
if (displayLink) {
CVDisplayLinkStop(displayLink);
CVDisplayLinkRelease(displayLink);
displayLink = NULL;
}
kore_event_destroy(&displayLinkEvent);
#endif
}

static const char *getSavePath(void) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
Expand Down
2 changes: 1 addition & 1 deletion kfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ else if (platform === Platform.OSX) {
addBackend('gpu/metal');
addKoreDefine('METAL');
project.addLib('Metal');
project.addLib('MetalKit');
project.addLib('QuartzCore');
}
else if (graphics === GraphicsApi.OpenGL) {
addBackend('gpu/opengl');
Expand Down
Loading