From cc9ecfbee7b1c346bd6b35bf1b9501bdf7eb9c21 Mon Sep 17 00:00:00 2001 From: Xiao-Yong Date: Fri, 17 May 2019 22:17:45 -0500 Subject: devdraw: cocoa metal screen updates (#215) * devdraw: cocoa metal screen uses a dirty hack to make everything smooth * devdraw: cocoa metal screen uses a layer to make fullscreen applications behave * devdraw: macOS cocoa metal fix resizeimg without img * devdraw: macOS cocoa metal uses blit instead of render We directly use the blit command encoder to copy texture to the framebuffer. We no longer need to compile the metal shader every time the application starts just for rendering a flat 2D surface. * travis: add osx images covering 10.13 and 10.14 --- src/cmd/devdraw/cocoa-screen-metal.m | 90 +++++++++++------------------------- 1 file changed, 27 insertions(+), 63 deletions(-) (limited to 'src/cmd/devdraw') diff --git a/src/cmd/devdraw/cocoa-screen-metal.m b/src/cmd/devdraw/cocoa-screen-metal.m index 5fc03172..09b34635 100644 --- a/src/cmd/devdraw/cocoa-screen-metal.m +++ b/src/cmd/devdraw/cocoa-screen-metal.m @@ -64,9 +64,7 @@ static NSWindow *win = NULL; static NSCursor *currentCursor = NULL; static DrawLayer *layer; -static MTLRenderPassDescriptor *renderPass; static id device; -static id pipelineState; static id commandQueue; static id texture; @@ -74,32 +72,6 @@ static Memimage *img = NULL; static QLock snarfl; -static NSString *const metal = -@"#include\n" -"using namespace metal;\n" -"typedef struct {\n" -" float4 rCoord [[position]];\n" -" float2 tCoord;\n" -"} VertexOut;\n" -"vertex VertexOut\n" -"renderVertex(unsigned int vid [[ vertex_id ]])\n" -"{\n" -" const VertexOut fixedV[] = {\n" -" {{ -1.0f, -1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f }},\n" -" {{ 1.0f, -1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f }},\n" -" {{ -1.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 0.0f }},\n" -" {{ 1.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f }},\n" -" };\n" -" return fixedV[vid];\n" -"}\n" -"fragment half4\n" -"renderFragment(VertexOut in [[ stage_in ]],\n" -" texture2d texture [[ texture(0) ]]) {\n" -" constexpr sampler s;\n" -" return texture.sample(s, in.tCoord);\n" -"}"; - - void threadmain(int argc, char **argv) { @@ -150,9 +122,6 @@ threadmain(int argc, char **argv) Rectangle wr; int set; char *s; - id library; - MTLRenderPipelineDescriptor *pipelineDesc; - NSError *error; NSArray *allDevices; const NSWindowStyleMask Winstyle = NSWindowStyleMaskTitled @@ -189,7 +158,7 @@ threadmain(int argc, char **argv) [win center]; [win setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; [win setContentMinSize:NSMakeSize(64,64)]; - + [win setOpaque:YES]; [win setRestorable:NO]; [win setAcceptsMouseMovedEvents:YES]; [win setDelegate:myApp]; @@ -209,7 +178,7 @@ threadmain(int argc, char **argv) } if(!device) device = MTLCreateSystemDefaultDevice(); - + commandQueue = [device newCommandQueue]; layer = (DrawLayer *)[myContent layer]; @@ -218,25 +187,14 @@ threadmain(int argc, char **argv) layer.framebufferOnly = YES; layer.opaque = YES; - renderPass = [MTLRenderPassDescriptor renderPassDescriptor]; - renderPass.colorAttachments[0].loadAction = MTLLoadActionDontCare; - renderPass.colorAttachments[0].storeAction = MTLStoreActionDontCare; - - library = [device newLibraryWithSource:metal options:nil error:&error]; - if(!library) - sysfatal((char *)[[error localizedDescription] UTF8String]); - - pipelineDesc = [MTLRenderPipelineDescriptor new]; - pipelineDesc.alphaToOneEnabled = YES; - pipelineDesc.inputPrimitiveTopology = MTLPrimitiveTopologyClassTriangle; - pipelineDesc.rasterSampleCount = 1; - pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; - [pipelineDesc setVertexFunction: [library newFunctionWithName: @"renderVertex"]]; - [pipelineDesc setFragmentFunction: [library newFunctionWithName: @"renderFragment"]]; - - pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineDesc error:&error]; - if(!pipelineState) - sysfatal((char *)[[error localizedDescription] UTF8String]); + // We use a default transparent layer on top of the CAMetalLayer. + // This seems to make fullscreen applications behave. + { + CALayer *stub = [CALayer layer]; + stub.frame = CGRectMake(0, 0, 1, 1); + [stub setNeedsDisplay]; + [layer addSublayer:stub]; + } [NSEvent setMouseCoalescingEnabled:NO]; @@ -278,7 +236,7 @@ struct Cursors { NSImage *i; NSPoint p; uchar *plane[5], *plane2[5]; - int b; + uint b; cs = [v pointerValue]; c = cs->c; @@ -577,13 +535,15 @@ struct Cursors { - (void)viewDidEndLiveResize { [super viewDidEndLiveResize]; - resizeimg(); + if(img) + resizeimg(); } - (void)viewDidChangeBackingProperties { [super viewDidChangeBackingProperties]; - resizeimg(); + if(img) + resizeimg(); } // conforms to protocol NSTextInputClient @@ -801,7 +761,7 @@ struct Cursors { - (void)display { id cbuf; - id cmd; + id blit; LOG(@"display"); @@ -821,13 +781,17 @@ struct Cursors { LOG(@"display got drawable"); - renderPass.colorAttachments[0].texture = drawable.texture; - - cmd = [cbuf renderCommandEncoderWithDescriptor:renderPass]; - [cmd setRenderPipelineState:pipelineState]; - [cmd setFragmentTexture:texture atIndex:0]; - [cmd drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4]; - [cmd endEncoding]; + blit = [cbuf blitCommandEncoder]; + [blit copyFromTexture:texture + sourceSlice:0 + sourceLevel:0 + sourceOrigin:MTLOriginMake(0, 0, 0) + sourceSize:MTLSizeMake(texture.width, texture.height, texture.depth) + toTexture:drawable.texture + destinationSlice:0 + destinationLevel:0 + destinationOrigin:MTLOriginMake(0, 0, 0)]; + [blit endEncoding]; [cbuf presentDrawable:drawable]; drawable = nil; -- cgit v1.2.3