-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathMetalView.mm
165 lines (141 loc) · 5.28 KB
/
MetalView.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright 2015-2019 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 <QuartzCore/CAMetalLayer.h>
#import "MetalView.h"
@implementation MetalView
{
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.renderMode = Diligent::MacOSAppBase::RenderMode::Metal;
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder
{
self = [super initWithCoder:coder];
if (self)
{
self.renderMode = Diligent::MacOSAppBase::RenderMode::Metal;
}
return self;
}
- (void) awakeFromNib
{
[super awakeFromNib];
// Back the view with a layer created by the makeBackingLayer method.
self.wantsLayer = YES;
[self initApp:self];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CVDisplayLinkRef displayLink;
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
[self setDisplayLink:displayLink];
CVDisplayLinkSetOutputCallback(displayLink, &DisplayLinkCallback, (__bridge void*)self);
CVDisplayLinkStart(displayLink);
#pragma clang diagnostic pop
[self setPostsBoundsChangedNotifications:YES];
[self setPostsFrameChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChange:) name:NSViewFrameDidChangeNotification object:self];
}
// Indicates that the view wants to draw using the backing
// layer instead of using drawRect:.
-(BOOL) wantsUpdateLayer
{
return YES;
}
// Returns a Metal-compatible layer.
+(Class) layerClass
{
return [CAMetalLayer class];
}
// If the wantsLayer property is set to YES, this method will
// be invoked to return a layer instance.
-(CALayer*) makeBackingLayer
{
CALayer* layer = [self.class.layerClass layer];
CGSize viewScale = [self convertSizeToBacking: CGSizeMake(1.0, 1.0)];
layer.contentsScale = MIN(viewScale.width, viewScale.height);
return layer;
}
-(void)render
{
auto* theApp = [self lockApp];
if (theApp)
{
theApp->Update();
theApp->Render();
theApp->Present();
}
[self unlockApp];
}
- (CVReturn) getFrameForTime:(const CVTimeStamp*)outputTime
{
// There is no autorelease pool when this method is called
// because it will be called from a background thread.
// It's important to create one or app can leak objects.
@autoreleasepool {
[self render];
}
return kCVReturnSuccess;
}
// Rendering loop callback function for use with a CVDisplayLink.
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp* now,
const CVTimeStamp* outputTime,
CVOptionFlags flagsIn,
CVOptionFlags* flagsOut,
void* target)
{
MetalView* view = (__bridge MetalView*)target;
CVReturn result = [view getFrameForTime:outputTime];
return result;
}
-(void)boundsDidChange:(NSNotification *)notification
{
// It is not clear what the proper way to handle window resize is.
// Cube demo from MoltenVK ignores any window resize notifications and
// recreates the swap chain if Present or AcquireNextImage fails, causing
// jagged transitions.
// According to this thread, there is no solution for flickering during
// resize in Metal:
// https://forums.developer.apple.com/thread/77901
// Calling WindowResize() causes flickering.
// Even if [self render] is called ater WindowResize()
// Similar results when using Metal kit view
// Calling [self render] alone produces jagged transitions but no flickering.
// Calling nothing causes the app to crash during resize.
NSRect viewRectPoints = [self bounds];
NSRect viewRectPixels = [self convertRectToBacking:viewRectPoints];
auto* theApp = [self lockApp];
if (theApp)
{
theApp->WindowResize(viewRectPixels.size.width, viewRectPixels.size.height);
}
[self unlockApp];
}
@end