1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-02 20:30:00 +00:00

This is possibly at least dispatching an empty command buffer correctly.

This commit is contained in:
Thomas Harte 2020-08-04 19:44:56 -04:00
parent e235a45abb
commit 72df6e52cd
3 changed files with 54 additions and 10 deletions

View File

@ -7,12 +7,13 @@
//
#import <Foundation/Foundation.h>
#import <MetalKit/MetalKit.h>
/*!
Provides a ScanTarget that uses Metal as its back-end.
*/
@interface CSScanTarget : NSObject
@interface CSScanTarget : NSObject <MTKViewDelegate>
- (nonnull instancetype)init;
- (nonnull instancetype)initWithView:(nonnull MTKView *)view;
@end

View File

@ -11,18 +11,47 @@
#import <Metal/Metal.h>
@implementation CSScanTarget {
id<MTLDevice> _device;
id<MTLCommandQueue> _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 <MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
MTLRenderPassDescriptor *const descriptor = view.currentRenderPassDescriptor;
id <MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:descriptor];
// TODO: the drawing (!)
[encoder endEncoding];
// "Register the drawable's presentation".
[commandBuffer presentDrawable:view.currentDrawable];
// Finalise and commit.
[commandBuffer commit];
}
@end

View File

@ -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]];