-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathmetal.cpp
751 lines (594 loc) · 27.5 KB
/
metal.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//
// metal.cpp
// ares
//
// Created by jcm on 3/4/24.
//
#include "metal.hpp"
struct VideoMetal;
@interface RubyVideoMetal : MTKView <MTKViewDelegate> {
@public
VideoMetal* video;
}
-(id) initWith:(VideoMetal*)video frame:(NSRect)frame device:(id<MTLDevice>)metalDevice;
-(void) mtkView:(MTKView *)view drawableSizeWillChange:(CGSize) size;
-(BOOL) acceptsFirstResponder;
@end
struct VideoMetal : VideoDriver, Metal {
VideoMetal& self = *this;
VideoMetal(Video& super) : VideoDriver(super) {}
~VideoMetal() { terminate(); }
auto create() -> bool override {
return initialize();
}
auto driver() -> string override { return "Metal"; }
auto ready() -> bool override { return _ready; }
auto hasFullScreen() -> bool override { return true; }
auto hasMonitor() -> bool override { return !_nativeFullScreen; }
auto hasContext() -> bool override { return true; }
auto hasFlush() -> bool override { return true; }
auto hasBlocking() -> bool override {
if (@available(macOS 10.15.4, *)) {
return true;
} else {
return false;
}
}
auto hasForceSRGB() -> bool override { return true; }
auto hasThreadedRenderer() -> bool override { return true; }
auto hasNativeFullScreen() -> bool override { return true; }
auto hasShader() -> bool override { return true; }
auto setFullScreen(bool fullScreen) -> bool override {
// todo: fix/make consistent mouse cursor hide behavior
if (_nativeFullScreen) {
[view.window toggleFullScreen:nil];
} else {
/// This option implements non-idiomatic macOS fullscreen behavior that sets the window frame equal to the selected display's
/// frame size and hides the cursor. This version of fullscreen is desirable because it allows us to render around the camera
/// housing on newer Macs (important for bezel-style shaders), has snappier entrance/exit and tabbing behavior, and functions
/// better with recording and capture software such as OBS.
if (fullScreen) {
auto monitor = Video::monitor(self.monitor);
NSScreen *handle = (__bridge NSScreen *)(void *)monitor.nativeHandle; //eew
frameBeforeFullScreen = view.window.frame;
[NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
[view.window setStyleMask:NSWindowStyleMaskBorderless];
[view.window setFrame:handle.frame display:YES];
[NSCursor setHiddenUntilMouseMoves:YES];
} else {
[NSApp setPresentationOptions:NSApplicationPresentationDefault];
[view.window setStyleMask:(NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable)];
[view.window setFrame:frameBeforeFullScreen display:YES];
}
[view.window makeFirstResponder:view];
}
return true;
}
auto setContext(uintptr context) -> bool override {
return initialize();
}
auto setBlocking(bool blocking) -> bool override {
_blocking = blocking;
updatePresentInterval();
return true;
}
auto setForceSRGB(bool forceSRGB) -> bool override {
if (forceSRGB) {
view.colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
} else {
view.colorspace = view.window.screen.colorSpace.CGColorSpace;
}
return true;
}
auto setThreadedRenderer(bool threadedRenderer) -> bool override {
_threaded = threadedRenderer;
return true;
}
auto setNativeFullScreen(bool nativeFullScreen) -> bool override {
_nativeFullScreen = nativeFullScreen;
if (nativeFullScreen) {
//maximize goes fullscreen
[view.window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
} else {
//maximize does not go fullscreen
[view.window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
}
return true;
}
auto setFlush(bool flush) -> bool override {
_flush = flush;
return true;
}
auto refreshRateHint(double refreshRate) -> void override {
if (refreshRate == _refreshRateHint) return;
_refreshRateHint = refreshRate;
updatePresentInterval();
}
auto isVRRSupported() -> bool {
if (@available(macOS 12.0, *)) {
NSTimeInterval minInterval = view.window.screen.minimumRefreshInterval;
NSTimeInterval maxInterval = view.window.screen.maximumRefreshInterval;
_vrrIsSupported = minInterval != maxInterval;
return _vrrIsSupported;
} else {
return false;
}
}
auto updatePresentInterval() -> void {
if (!isVRRSupported()) {
CGDirectDisplayID displayID = CGMainDisplayID();
CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode(displayID);
CFTimeInterval refreshRate = CGDisplayModeGetRefreshRate(displayMode);
_presentInterval = (1.0 / refreshRate);
} else {
if (@available(macOS 12.0, *)) {
CFTimeInterval minimumInterval = view.window.screen.minimumRefreshInterval;
if (_refreshRateHint != 0) {
_presentInterval = (1.0 / _refreshRateHint);
NSLog(@"Refresh rate hint changed to %lf", _refreshRateHint);
averagePresentDuration = _presentInterval;
} else {
_presentInterval = minimumInterval;
}
}
}
}
auto setShader(string pathname) -> bool override {
if (_filterChain != NULL) {
_libra.mtl_filter_chain_free(&_filterChain);
}
if (_preset != NULL) {
_libra.preset_free(&_preset);
}
if(file::exists(pathname)) {
if (_libra.preset_create(pathname.data(), &_preset) != NULL) {
print(string{"Metal: Failed to load shader: ", pathname, "\n"});
return false;
}
if (_libra.mtl_filter_chain_create(&_preset, _commandQueue, nil, &_filterChain) != NULL) {
print(string{"Metal: Failed to create filter chain for: ", pathname, "\n"});
return false;
};
} else {
return false;
}
return true;
}
auto focused() -> bool override {
return true;
}
auto clear() -> void override {
dispatch_sync(_renderQueue, ^{
id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
MTLRenderPassDescriptor *drawableRenderPassDescriptor = view.currentRenderPassDescriptor;
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:drawableRenderPassDescriptor];
[renderEncoder endEncoding];
id<CAMetalDrawable> drawable = view.currentDrawable;
[commandBuffer presentDrawable:drawable];
[view draw];
[commandBuffer commit];
});
}
auto size(u32& width, u32& height) -> void override {
if ((_viewWidth == width && _viewHeight == height) && (_viewWidth != 0 && _viewHeight != 0)) { return; }
auto area = [view convertRectToBacking:[view bounds]];
width = area.size.width;
height = area.size.height;
auto newSize = CGSize();
newSize.width = width;
newSize.height = height;
view.drawableSize = newSize;
_viewWidth = width;
_viewHeight = height;
_outputX = (width - outputWidth) / 2;
_outputY = (height - outputHeight) / 2;
}
auto resizeSourceBuffers() {
for (int i = 0; i < kMaxSourceBuffersInFlight; i++) {
if (sourceWidth < 1 || sourceHeight < 1) {
_sourceTextures[i] = nullptr;
continue;
}
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor new];
textureDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm;
textureDescriptor.width = sourceWidth;
textureDescriptor.height = sourceHeight;
textureDescriptor.usage = MTLTextureUsageRenderTarget|MTLTextureUsageShaderRead;
_sourceTextures[i] = [_device newTextureWithDescriptor:textureDescriptor];
}
f64 newWidth = sourceWidth * _scaleX;
f64 newHeight = sourceHeight * _scaleY;
if (_interlace) {
newHeight = sourceHeight;
}
for (int i = 0; i < kMaxSourceBuffersInFlight; i++) {
if (newWidth < 1 || newHeight < 1) {
_finalSourceTextures[i] = nullptr;
continue;
}
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor new];
textureDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm;
textureDescriptor.width = newWidth;
textureDescriptor.height = newHeight;
textureDescriptor.usage = MTLTextureUsageRenderTarget|MTLTextureUsageShaderRead;
_finalSourceTextures[i] = [_device newTextureWithDescriptor:textureDescriptor];
}
NSLog(@"Resized final source textures to %lf, %lf", newWidth, newHeight);
}
auto setScale(f64 scaleX, f64 scaleY) -> void override {
if (scaleX != _scaleX || scaleY != scaleY) {
_scaleX = scaleX;
_scaleY = scaleY;
dispatch_async(_renderQueue, ^{
resizeSourceBuffers();
});
}
}
auto setInterlace(bool interlaceField) -> void override {
if (_interlace) return;
_interlace = true;
_progressive = false;
dispatch_async(_renderQueue, ^{
resizeSourceBuffers();
});
}
auto setProgressive(bool progressiveDouble) -> void override {
if (_progressive) return;
_interlace = false;
_progressive = true;
dispatch_async(_renderQueue, ^{
resizeSourceBuffers();
});
}
auto acquire(u32*& data, u32& pitch, u32 width, u32 height) -> bool override {
if (sourceWidth != width || sourceHeight != height) {
sourceWidth = width, sourceHeight = height;
if (buffer) {
delete[] buffer;
buffer = nullptr;
}
buffer = new u32[width * height]();
bytesPerRow = sourceWidth * sizeof(u32);
if (bytesPerRow < 16) bytesPerRow = 16;
dispatch_async(_renderQueue, ^{
resizeSourceBuffers();
});
}
pitch = sourceWidth * sizeof(u32);
return data = buffer;
}
auto release() -> void override {}
auto resizeOutputBuffers(u32 width, u32 height) {
NSLog(@"Resizing output buffers to %i, %i", width, height);
outputWidth = width;
outputHeight = height;
float widthfloat = (float)width;
float heightfloat = (float)height;
MetalVertex vertices[] =
{
// Pixel positions, Texture coordinates
{ { widthfloat / 2, -heightfloat / 2 }, { 1.f, 1.f } },
{ { -widthfloat / 2, -heightfloat / 2 }, { 0.f, 1.f } },
{ { -widthfloat / 2, heightfloat / 2 }, { 0.f, 0.f } },
{ { widthfloat / 2, -heightfloat / 2 }, { 1.f, 1.f } },
{ { -widthfloat / 2, heightfloat / 2 }, { 0.f, 0.f } },
{ { widthfloat / 2, heightfloat / 2 }, { 1.f, 0.f } },
};
_vertexBuffer = [_device newBufferWithBytes:vertices length:sizeof(vertices) options:MTLResourceStorageModeShared];
MTLTextureDescriptor *texDescriptor = [MTLTextureDescriptor new];
texDescriptor.textureType = MTLTextureType2D;
texDescriptor.width = width;
texDescriptor.height = height;
texDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm;
texDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
_renderTargetTexture = [_device newTextureWithDescriptor:texDescriptor];
_viewportSize.x = width;
_viewportSize.y = height;
_libraViewport.width = (uint32_t) width;
_libraViewport.height = (uint32_t) height;
_libraViewport.x = 0;
_libraViewport.y = 0;
_outputX = (_viewWidth - width) / 2;
_outputY = (_viewHeight - height) / 2;
}
auto output(u32 width, u32 height) -> void override {
/// Synchronously copy the current framebuffer to a Metal texture, then call into the render dispatch queue
/// either synchronously or asynchronously depending on whether blocking is on and VRR is supported.
if (depth >= kMaxSourceBuffersInFlight) {
//if we are running very behind, drop this frame
return;
}
//can we do this outside of the output function?
//currently no, because in theory framebuffer size can change during runtime
if (width != outputWidth || height != outputHeight) {
resizeOutputBuffers(width, height);
}
@autoreleasepool {
frameCount++;
auto index = frameCount % kMaxSourceBuffersInFlight;
auto sourceTexture = _sourceTextures[index];
auto finalSourceTexture = _finalSourceTextures[index];
[sourceTexture replaceRegion:MTLRegionMake2D(0, 0, sourceWidth, sourceHeight) mipmapLevel:0 withBytes:buffer bytesPerRow:bytesPerRow];
if (@available(macOS 10.15.4, *)) {
depth++;
}
/// Only block with `dispatch_sync` if blocking enabled and VRR not supported, or if the threaded renderer
/// is explicitly disabled. if VRR is supported, we should try to not _literally_ block, because we'll be making a best
/// effort to synchronize to the guest and host refresh rate at the same time. It's easier to do that if we have
/// assurances that we won't block the emulation thread in the worst case system conditions.
if ((_blocking && !_vrrIsSupported) || !_threaded) {
dispatch_sync(_renderQueue, ^{
outputHelper(width, height, sourceTexture, finalSourceTexture);
});
} else {
dispatch_async(_renderQueue, ^{
outputHelper(width, height, sourceTexture, finalSourceTexture);
});
}
}
}
private:
auto outputHelper(u32 width, u32 height, id<MTLTexture> sourceTexture, id<MTLTexture> finalSourceTexture) -> void {
/// Uses two render passes (plus librashader's render passes). The first render pass samples the source texture,
/// consisting of the pixel buffer from the emulator, onto a texture the same size as our eventual output,
/// `_renderTargetTexture`. Then it calls into librashader, which performs postprocessing onto the same
/// output texture. Then for the second render pass here, we composite the output texture within ares's viewport.
/// We need this last pass because librashader expects the viewport to be the same size as the output texture,
/// which is not the case for ares.
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
if (commandBuffer != nil) {
__block dispatch_semaphore_t block_sema = _semaphore;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
dispatch_semaphore_signal(block_sema);
}];
_renderToTextureRenderPassDescriptor.colorAttachments[0].texture = finalSourceTexture;
if (_renderToTextureRenderPassDescriptor != nil) {
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:_renderToTextureRenderPassDescriptor];
_renderToTextureRenderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
[renderEncoder setRenderPipelineState:_renderToTextureRenderPipeline];
[renderEncoder setViewport:(MTLViewport){0, 0, (double)finalSourceTexture.width, (double)finalSourceTexture.height, -1.0, 1.0}];
[renderEncoder setVertexBuffer:_vertexBuffer
offset:0
atIndex:0];
[renderEncoder setVertexBytes:&_viewportSize
length:sizeof(_viewportSize)
atIndex:MetalVertexInputIndexViewportSize];
[renderEncoder setFragmentTexture:sourceTexture atIndex:0];
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:6];
[renderEncoder endEncoding];
if (_filterChain) {
_libra.mtl_filter_chain_frame(&_filterChain, commandBuffer, frameCount, finalSourceTexture, _libraViewport, _renderTargetTexture, nil, nil);
} else {
_renderToTextureRenderPassDescriptor.colorAttachments[0].texture = _renderTargetTexture;
if (_renderToTextureRenderPassDescriptor != nil) {
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:_renderToTextureRenderPassDescriptor];
_renderToTextureRenderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
[renderEncoder setRenderPipelineState:_renderToTextureRenderPipeline];
[renderEncoder setViewport:(MTLViewport){0, 0, (double)width, (double)height, -1.0, 1.0}];
[renderEncoder setVertexBuffer:_vertexBuffer
offset:0
atIndex:0];
[renderEncoder setVertexBytes:&_viewportSize
length:sizeof(_viewportSize)
atIndex:MetalVertexInputIndexViewportSize];
[renderEncoder setFragmentTexture:finalSourceTexture atIndex:0];
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:6];
[renderEncoder endEncoding];
}
}
//this call will block the current thread/queue if a drawable is not yet available
MTLRenderPassDescriptor *drawableRenderPassDescriptor = view.currentRenderPassDescriptor;
drawableRenderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
if (drawableRenderPassDescriptor != nil) {
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:drawableRenderPassDescriptor];
[renderEncoder setRenderPipelineState:_drawableRenderPipeline];
[renderEncoder setViewport:(MTLViewport){_outputX, _outputY, (double)width, (double)height, -1.0, 1.0}];
[renderEncoder setVertexBuffer:_vertexBuffer
offset:0
atIndex:0];
[renderEncoder setVertexBytes:&_viewportSize
length:sizeof(_viewportSize)
atIndex:MetalVertexInputIndexViewportSize];
[renderEncoder setFragmentTexture:_renderTargetTexture atIndex:0];
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:6];
[renderEncoder endEncoding];
id<CAMetalDrawable> drawable = view.currentDrawable;
if (@available(macOS 10.15.4, *)) {
[drawable addPresentedHandler:^(id<MTLDrawable> drawable) {
self.drawableWasPresented(drawable);
depth--;
}];
}
auto targetPresentDuration = determineNextPresentDuration();
if (drawable != nil) {
if (_blocking) {
//_blocking is not enabled unless 10.15.4 is available, so ignore availability warnings here
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
[commandBuffer presentDrawable:drawable afterMinimumDuration:targetPresentDuration];
#pragma clang diagnostic pop
} else {
[commandBuffer presentDrawable:drawable];
}
[view draw];
}
}
[commandBuffer commit];
if (_flush) {
[commandBuffer waitUntilCompleted];
}
}
}
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
auto drawableWasPresented(id<MTLDrawable> drawable) -> void {
if (drawable.presentedTime == 0) { return; }
if (previousPresentedTime <= 0) {
previousPresentedTime = drawable.presentedTime;
}
CFTimeInterval presentationDuration = drawable.presentedTime - previousPresentedTime;
const double alpha = kPresentIntervalRollingAverageWeight;
averagePresentDuration = (presentationDuration * alpha) + (averagePresentDuration * (1.0 - alpha));
previousPresentedTime = drawable.presentedTime;
}
#pragma clang diagnostic pop
auto determineNextPresentDuration() -> CFTimeInterval {
/// We use a rolling average of the last few seconds worth of frames to determine if we are running fast or slow. If
/// we are running ahead, we do nothing special; it's sufficient to present at the prescribed interval and eventually
/// we will fall behind. When we fall behind, we need to present earlier than the target present interval. The way VRR
/// works on macOS, we can request an earlier present interval, but we don't always get it. So what we do in this
/// function is "nudge" the system to display our frame early, but not immediately, in an attempt to correct for running
/// behind. If in this process we get more than 3 frames behind, we start requesting immediate presents.
CFTimeInterval targetPresentDuration = _presentInterval;
CFTimeInterval differenceFromTarget = _presentInterval - averagePresentDuration;
if (-differenceFromTarget >= (_presentInterval * kVRRCorrectiveTolerance)) {
targetPresentDuration = _presentInterval + (differenceFromTarget * kVRRCorrectiveForce);
}
if (depth > kVRRImmediatePresentThreshold) {
return 0;
} else {
return targetPresentDuration;
}
}
auto initialize() -> bool {
terminate();
if (!self.context) return false;
auto context = (__bridge NSView*)(void *)self.context;
auto size = [context frame].size;
NSError *error = nil;
//Put renderer on a separate queue so we can choose whether or not to block the main thread (audio) waiting on a drawable.
dispatch_queue_attr_t queueAttributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, -1);
_renderQueue = dispatch_queue_create("com.ares.metal-renderer", queueAttributes);
_device = MTLCreateSystemDefaultDevice();
_commandQueue = [_device newCommandQueue];
_semaphore = dispatch_semaphore_create(kMaxOutputBuffersInFlight);
_renderToTextureRenderPassDescriptor = [MTLRenderPassDescriptor new];
_renderToTextureRenderPassDescriptor.colorAttachments[0].texture = _renderTargetTexture;
_renderToTextureRenderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
_renderToTextureRenderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1);
_renderToTextureRenderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
///We compile shaders at runtime so we do not need to add the `xcrun` Metal compiler toolchain to the ares build process.
///Metal frame capture does not get along with runtime-compiled shaders in my testing, however. If you are debugging ares
///and need GPU captures, run `scripts/macos-metal-debug.sh` and then compile ares in debug mode.
bool libraryCreated = false;
#if defined(BUILD_DEBUG)
if (@available(macOS 10.13, *)) {
NSURL *shaderLibURL = [NSURL fileURLWithPath:@"ares.app/Contents/Resources/Shaders/shaders.metallib"];
_library = [_device newLibraryWithURL: shaderLibURL error:&error];
}
if (_library != nil) {
libraryCreated = true;
} else {
NSLog(@"Compiled in debug mode, but debug .metallib not found. If you require Metal debugging, ensure you are on macOS 10.13+ and compile debug Metal shaders with scripts/macos-metal-debug.sh.");
}
#endif
if (!libraryCreated) {
NSString *bundleResourcePath = [NSBundle mainBundle].resourcePath;
const string& fileComponent = "/Shaders/Shaders.metal";
NSString *shaderFilePath = [bundleResourcePath stringByAppendingString: [[NSString new] initWithUTF8String:fileComponent]];
NSString *shaderLibrarySource = [NSString stringWithContentsOfFile:shaderFilePath encoding:NSUTF8StringEncoding error: &error];
if (shaderLibrarySource == nil) {
NSLog(@"%@",error);
return false;
}
_library = [_device newLibraryWithSource: shaderLibrarySource options: [MTLCompileOptions alloc] error:&error];
if (_library == nil) {
NSLog(@"%@",error);
return false;
}
}
MTLRenderPipelineDescriptor *pipelineStateDescriptor = [MTLRenderPipelineDescriptor new];
// Set up pipeline for rendering to the offscreen texture. Reuse the
// descriptor and change properties that differ.
pipelineStateDescriptor.label = @"Offscreen Render Pipeline";
pipelineStateDescriptor.sampleCount = 1;
pipelineStateDescriptor.vertexFunction = [_library newFunctionWithName:@"vertexShader"];
pipelineStateDescriptor.fragmentFunction = [_library newFunctionWithName:@"samplingShader"];
pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
_renderToTextureRenderPipeline = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&error];
if (_renderToTextureRenderPipeline == nil) {
NSLog(@"%@",error);
return false;
}
pipelineStateDescriptor.label = @"Drawable Render Pipeline";
pipelineStateDescriptor.sampleCount = 1;
pipelineStateDescriptor.vertexFunction = [_library newFunctionWithName:@"vertexShader"];
pipelineStateDescriptor.fragmentFunction = [_library newFunctionWithName:@"drawableSamplingShader"];
pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
_drawableRenderPipeline = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&error];
if (_drawableRenderPipeline == nil) {
NSLog(@"%@",error);
return false;
}
auto frame = NSMakeRect(0, 0, size.width, size.height);
view = [[RubyVideoMetal alloc] initWith:this frame:frame device:_device];
[context addSubview:view];
[[view window] makeFirstResponder:view];
bool forceSRGB = self.forceSRGB;
self.setForceSRGB(forceSRGB);
view.autoresizingMask = NSViewWidthSizable|NSViewHeightSizable;
_threaded = self.threadedRenderer;
_blocking = self.blocking;
setNativeFullScreen(self.nativeFullScreen);
_libra = librashader_load_instance();
if (!_libra.instance_loaded) {
print("Metal: Failed to load librashader: shaders will be disabled\n");
}
initialized = true;
return _ready = true;
}
auto terminate() -> void {
_ready = false;
_commandQueue = nullptr;
_library = nullptr;
_vertexBuffer = nullptr;
for (int i = 0; i < kMaxSourceBuffersInFlight; i++) {
_sourceTextures[i] = nullptr;
}
_mtlVertexDescriptor = nullptr;
_renderToTextureRenderPassDescriptor = nullptr;
_renderTargetTexture = nullptr;
_renderToTextureRenderPipeline = nullptr;
_drawableRenderPipeline = nullptr;
if (_filterChain) {
_libra.mtl_filter_chain_free(&_filterChain);
}
_device = nullptr;
if (view) {
[view removeFromSuperview];
view = nil;
}
}
RubyVideoMetal* view = nullptr;
bool _ready = false;
std::recursive_mutex mutex;
};
@implementation RubyVideoMetal : MTKView
-(id) initWith:(VideoMetal*)videoPointer frame:(NSRect)frame device:(id<MTLDevice>)metalDevice {
if (self = [super initWithFrame:frame device:metalDevice]) {
video = videoPointer;
}
self.enableSetNeedsDisplay = NO;
self.paused = YES;
//below is the delegate path; currently not used but likely will be used in future.
//self.enableSetNeedsDisplay = YES;
//self.paused = NO;
//[self setDelegate:self];
return self;
}
-(void) drawInMTKView:(MTKView *)view {
//currently not used
//video->alternateDrawPath();
}
-(void) mtkView:(MTKView *)view drawableSizeWillChange:(CGSize) size {
}
-(BOOL) acceptsFirstResponder {
return YES;
}
-(void) keyDown:(NSEvent*)event {
}
-(void) keyUp:(NSEvent*)event {
}
@end