diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-12-26 01:04:20 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-12-26 01:04:20 +0000 |
| commit | 0f7acbb013044c72f70211fbb19b531d673c91ae (patch) | |
| tree | e74740c806df9a4fad484079d6d96f531c4d34e1 /Common/NativeApp/Apple/Source | |
| parent | Enabled Vulkan mode on Mac (preliminary implementation) (diff) | |
| download | DiligentEngine-0f7acbb013044c72f70211fbb19b531d673c91ae.tar.gz DiligentEngine-0f7acbb013044c72f70211fbb19b531d673c91ae.zip | |
Improved implementation of Vulkan mode in Mac native app
Diffstat (limited to 'Common/NativeApp/Apple/Source')
| -rw-r--r-- | Common/NativeApp/Apple/Source/Classes/OSX/GLView.h | 2 | ||||
| -rw-r--r-- | Common/NativeApp/Apple/Source/Classes/OSX/GLView.m | 2 | ||||
| -rw-r--r-- | Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h | 33 | ||||
| -rw-r--r-- | Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.mm (renamed from Common/NativeApp/Apple/Source/Classes/OSX/MetalView.mm) | 77 | ||||
| -rw-r--r-- | Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.h (renamed from Common/NativeApp/Apple/Source/Classes/OSX/MetalView.h) | 17 | ||||
| -rw-r--r-- | Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm | 55 |
6 files changed, 123 insertions, 63 deletions
diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h index 993334d..7ef94fb 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h +++ b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h @@ -17,6 +17,6 @@ -(void)stopDisplayLink; -(void)startDisplayLink; -(NSString*)getAppName; -- (NSString*)getError; +-(NSString*)getError; @end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m index 79aad2f..fac04b8 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m +++ b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m @@ -12,7 +12,9 @@ #import "GLView.h" #include "NativeAppBase.h" +#ifndef SUPPORT_RETINA_RESOLUTION #define SUPPORT_RETINA_RESOLUTION 1 +#endif @interface GLView () { diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h new file mode 100644 index 0000000..53b2a63 --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h @@ -0,0 +1,33 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#import <AppKit/AppKit.h> +#import <MetalKit/MetalKit.h> + +// Implementation of MTKViewDelegate +@interface MTKRenderer : NSObject<MTKViewDelegate> + +- (nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)mtkView; + +@end + diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.mm b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.mm index e8f6837..175c46d 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.mm +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.mm @@ -21,71 +21,46 @@ * of the possibility of such damages. */ -#include <memory> - -#import "MetalView.h" -#import <QuartzCore/CAMetalLayer.h> +#import <MTKRenderer.h> +#include <memory> #include "NativeAppBase.h" -#pragma mark - -#pragma mark MetalViewController - -@implementation MetalViewController +// Main class performing the rendering +@implementation MTKRenderer { std::unique_ptr<NativeAppBase> _theApp; } --(void) viewDidLoad { - [super viewDidLoad]; - - self.view.wantsLayer = YES; // Back the view with a layer created by the makeBackingLayer method. - - _theApp.reset(CreateApplication()); - _theApp->Initialize(self.view); - - // Temporary - _theApp->Update(); - _theApp->Render(); - _theApp->Present(); -} - -// Resize the window to fit the size of the content as set by the sample code. -/*-(void) viewWillAppear { - [super viewWillAppear]; - - CGSize vSz = self.view.bounds.size; - NSWindow *window = self.view.window; - NSRect wFrm = [window contentRectForFrameRect: window.frame]; - NSRect newWFrm = [window frameRectForContentRect: NSMakeRect(wFrm.origin.x, wFrm.origin.y, vSz.width, vSz.height)]; - [window setFrame: newWFrm display: YES animate: window.isVisible]; - [window center]; -}*/ - -@end - - -#pragma mark - -#pragma mark MetalView - -@implementation MetalView - -// Indicates that the view wants to draw using the backing layer instead of using drawRect:. --(BOOL) wantsUpdateLayer +/// Initialize with the MetalKit view from which we'll obtain our Metal device +- (nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)mtkView { - return YES; + self = [super init]; + if(self) + { + _theApp.reset(CreateApplication()); + _theApp->Initialize(mtkView); + } + + return self; } -// Returns a Metal-compatible layer. -+(Class) layerClass +/// Called whenever view changes orientation or is resized +- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size { - return [CAMetalLayer class]; + NSRect viewRectPoints = [view bounds]; + NSRect viewRectPixels = [view convertRectToBacking:viewRectPoints]; + + _theApp->WindowResize(viewRectPixels.size.width, viewRectPixels.size.height); } -// If the wantsLayer property is set to YES, this method will be invoked to return a layer instance. --(CALayer*) makeBackingLayer +/// Called whenever the view needs to render a frame +- (void)drawInMTKView:(nonnull MTKView *)view { - return [self.class.layerClass layer]; + _theApp->Update(); + _theApp->Render(); + _theApp->Present(); } @end + diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.h b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.h index b17d7f3..bfe4a01 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.h +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.h @@ -21,20 +21,15 @@ * of the possibility of such damages. */ +#if defined(TARGET_IOS) +#import <UIKit/UIKit.h> +#define PlatformViewController UIViewController +#else #import <AppKit/AppKit.h> - -#pragma mark - -#pragma mark MetalViewController +#define PlatformViewController NSViewController +#endif // The main view controller for the app storyboard. @interface MetalViewController : NSViewController -@end - -#pragma mark - -#pragma mark MetalView - -// The Metal-compatibile view for the app Storyboard. -@interface MetalView : NSView @end - diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm new file mode 100644 index 0000000..a3d821f --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm @@ -0,0 +1,55 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#import "MetalViewController.h" +#import "MTKRenderer.h" + +@implementation MetalViewController +{ + MTKView *_view; + MTKRenderer *_renderer; +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + + // Set the view to use the default device + _view = (MTKView *)self.view; + + _renderer = [[MTKRenderer alloc] initWithMetalKitView:_view]; + + if(!_renderer) + { + NSLog(@"Renderer failed initialization"); + return; + } + + // Initialize our renderer with the view size + [_renderer mtkView:_view drawableSizeWillChange:_view.drawableSize]; + + _view.delegate = _renderer; +} + +@end + |
