diff --git a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.h b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.h index 7140a3ea5..93f9fb62c 100644 --- a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.h +++ b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.h @@ -7,12 +7,13 @@ // #import +#import /*! Provides a ScanTarget that uses Metal as its back-end. */ -@interface CSScanTarget : NSObject +@interface CSScanTarget : NSObject -- (nonnull instancetype)init; +- (nonnull instancetype)initWithView:(nonnull MTKView *)view; @end diff --git a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm index f16fcd631..a423db496 100644 --- a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm +++ b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm @@ -11,18 +11,47 @@ #import @implementation CSScanTarget { - id _device; id _commandQueue; } -- (nonnull instancetype)init { +- (nonnull instancetype)initWithView:(nonnull MTKView *)view { self = [super init]; if(self) { - _device = MTLCreateSystemDefaultDevice(); - _commandQueue = [_device newCommandQueue]; - NSLog(@"%@; %@", _device, _commandQueue); + _commandQueue = [view.device newCommandQueue]; } return self; } +/*! + @method mtkView:drawableSizeWillChange: + @abstract Called whenever the drawableSize of the view will change + @discussion Delegate can recompute view and projection matricies or regenerate any buffers to be compatible with the new view size or resolution + @param view MTKView which called this method + @param size New drawable size in pixels + */ +- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size { + NSLog(@"New size: %@", NSStringFromSize(size)); +} + +/*! + @method drawInMTKView: + @abstract Called on the delegate when it is asked to render into the view + @discussion Called on the delegate when it is asked to render into the view + */ +- (void)drawInMTKView:(nonnull MTKView *)view { + id commandBuffer = [_commandQueue commandBuffer]; + MTLRenderPassDescriptor *const descriptor = view.currentRenderPassDescriptor; + id encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor]; + + // TODO: the drawing (!) + + [encoder endEncoding]; + + // "Register the drawable's presentation". + [commandBuffer presentDrawable:view.currentDrawable]; + + // Finalise and commit. + [commandBuffer commit]; +} + @end diff --git a/OSBindings/Mac/Clock Signal/Views/CSScanTargetView.m b/OSBindings/Mac/Clock Signal/Views/CSScanTargetView.m index c67270a5a..dd2907d3d 100644 --- a/OSBindings/Mac/Clock Signal/Views/CSScanTargetView.m +++ b/OSBindings/Mac/Clock Signal/Views/CSScanTargetView.m @@ -8,6 +8,7 @@ #import "CSScanTargetView.h" #import "CSApplication.h" +#import "CSScanTarget.h" @import CoreVideo; @import GLKit; @@ -27,6 +28,8 @@ atomic_int _isDrawingFlag; BOOL _isInvalid; + + CSScanTarget *_scanTarget; } //- (void)prepareOpenGL { @@ -120,9 +123,10 @@ static CVReturn DisplayLinkCallback(__unused CVDisplayLinkRef displayLink, const [self redrawWithEvent:CSScanTargetViewRedrawEventTimer]; } -- (void)drawRect:(NSRect)dirtyRect { - [self redrawWithEvent:CSScanTargetViewRedrawEventAppKit]; -} +//- (void)drawRect:(NSRect)dirtyRect { +// [self redrawWithEvent:CSScanTargetViewRedrawEventAppKit]; +// NSLog(@"..."); +//} - (void)redrawWithEvent:(CSScanTargetViewRedrawEvent)event { [self performWithGLContext:^{ @@ -179,6 +183,13 @@ static CVReturn DisplayLinkCallback(__unused CVDisplayLinkRef displayLink, const //} - (void)awakeFromNib { + // Use the preferred device if available. + if(@available(macOS 10.15, *)) { + self.device = self.preferredDevice; + } else { + self.device = MTLCreateSystemDefaultDevice(); + } + // NSOpenGLPixelFormatAttribute attributes[] = { // NSOpenGLPFADoubleBuffer, // NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core, @@ -203,6 +214,9 @@ static CVReturn DisplayLinkCallback(__unused CVDisplayLinkRef displayLink, const // self.pixelFormat = pixelFormat; // self.openGLContext = context; // self.wantsBestResolutionOpenGLSurface = YES; + // Create the scan target. + _scanTarget = [[CSScanTarget alloc] initWithView:self]; + self.delegate = _scanTarget; // Register to receive dragged and dropped file URLs. [self registerForDraggedTypes:@[(__bridge NSString *)kUTTypeFileURL]];