-
Notifications
You must be signed in to change notification settings - Fork 39
/
NuoMetalView.m
executable file
·227 lines (158 loc) · 4.81 KB
/
NuoMetalView.m
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#import "NuoMetalView.h"
#import "NuoTypes.h"
#import "NuoRenderPipeline.h"
#import "NuoRenderPipelinePass.h"
#import "NuoRenderPassTarget.h"
#import <sys/time.h>
#define MEASURE_PERFORMANCE 0
@interface NuoMetalView () < NuoRenderPipelineDelegate >
@property (nonatomic, readonly) CAMetalLayer *metalLayer;
@property (nonatomic, strong) id<MTLCommandQueue> commandQueue;
@property (strong) dispatch_semaphore_t displaySemaphore;
@property (nonatomic, strong) id<CAMetalDrawable> currentDrawable;
/**
* current index in the tri-buffer flow
*/
@property (nonatomic, assign) unsigned int inFlightIndex;
#if MEASURE_PERFORMANCE
@property (nonatomic, assign) unsigned int inFlightNumber;
#endif
@end
@implementation NuoMetalView
- (CALayer*)makeBackingLayer
{
return [CAMetalLayer new];
}
- (CAMetalLayer *)metalLayer
{
CAMetalLayer* layer = (CAMetalLayer *)self.layer;
return layer;
}
- (void)awakeFromNib
{
[self setWantsLayer:YES];
self.metalLayer.device = MTLCreateSystemDefaultDevice();
[self commonInit];
[self updateDrawableSize];
}
- (CGSize)drawableSize
{
return [self metalLayer].drawableSize;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
_displaySemaphore = dispatch_semaphore_create(kInFlightBufferCount);
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame device:(id<MTLDevice>)device
{
if ((self = [super initWithFrame:frame]))
{
[self commonInit];
self.metalLayer.device = device;
_displaySemaphore = dispatch_semaphore_create(kInFlightBufferCount);
}
return self;
}
- (void)commonInit
{
_preferredFramesPerSecond = 60;
_commandQueue = [self.metalLayer.device newCommandQueue];
[self setWantsLayer:YES];
self.metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
}
- (void)viewDidMoveToSuperview
{
[self viewResizing];
}
- (void)resizeSubviewsWithOldSize:(NSSize)oldSize
{
[self viewResizing];
}
- (void)viewResizing
{
[self updateDrawableSize];
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self updateDrawableSize];
}
- (void)updateDrawableSize
{
// During the first layout pass, we will not be in a view hierarchy, so we guess our scale
CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
CGSize drawableSize = self.bounds.size;
// Since drawable size is in pixels, we need to multiply by the scale to move from points to pixels
drawableSize.width *= scale;
drawableSize.height *= scale;
self.metalLayer.drawableSize = drawableSize;
[self.renderPipeline setDrawableSize:drawableSize];
[self render];
}
- (void)viewDidEndLiveResize
{
[super viewDidEndLiveResize];
[self updateDrawableSize];
}
- (void)setColorPixelFormat:(MTLPixelFormat)colorPixelFormat
{
self.metalLayer.pixelFormat = colorPixelFormat;
}
- (MTLPixelFormat)colorPixelFormat
{
return self.metalLayer.pixelFormat;
}
- (void)setRenderPasses:(NSArray<NuoRenderPass *> *)renderPasses
{
_renderPipeline = [[NuoRenderPipeline alloc] init];
_renderPipeline.renderPasses = renderPasses;
_renderPipeline.renderPipelineDelegate = self;
}
- (id<MTLTexture>)nextFinalTexture
{
_currentDrawable = [self.metalLayer nextDrawable];
return [_currentDrawable texture];
}
- (void)render
{
dispatch_semaphore_wait(_displaySemaphore, DISPATCH_TIME_FOREVER);
_inFlightIndex = (_inFlightIndex + 1) % kInFlightBufferCount;
#if MEASURE_PERFORMANCE
_inFlightNumber += 1;
NSLog(@"In flight frame: %u.", _inFlightNumber);
struct timeval begin, __block end, __block presented;
gettimeofday(&begin, NULL);
#endif
id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
__block dispatch_semaphore_t displaySem = self.displaySemaphore;
if (![_renderPipeline renderWithCommandBuffer:commandBuffer inFlight:_inFlightIndex])
{
dispatch_semaphore_signal(displaySem);
#if MEASURE_PERFORMANCE
_inFlightNumber -= 1;
NSLog(@"In flight frame skipped: %u.", _inFlightNumber);
#endif
return;
};
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer)
{
#if MEASURE_PERFORMANCE
gettimeofday(&presented, NULL);
float encodeVal = (end.tv_sec - begin.tv_sec) * 1e6 + (end.tv_usec - begin.tv_usec);
float presentVal = (presented.tv_sec - begin.tv_sec) * 1e6 + (presented.tv_usec - begin.tv_usec);
NSLog(@"Time spent: %f, %f.", encodeVal, presentVal);
_inFlightNumber -= 1;
#endif
dispatch_semaphore_signal(displaySem);
}];
#if MEASURE_PERFORMANCE
gettimeofday(&end, NULL);
#endif
[commandBuffer presentDrawable:_currentDrawable];
[commandBuffer commit];
}
@end