diff options
author | Xiao-Yong <xjin@anl.gov> | 2019-05-17 22:17:45 -0500 |
---|---|---|
committer | Russ Cox <rsc@swtch.com> | 2019-05-17 23:17:44 -0400 |
commit | cc9ecfbee7b1c346bd6b35bf1b9501bdf7eb9c21 (patch) | |
tree | cfc6ff603a213fb409b3fa85acb4197b9f14bf2e /src/cmd/devdraw | |
parent | 3197719090b3fd0a038767f7e8e15e771b1515be (diff) | |
download | plan9port-cc9ecfbee7b1c346bd6b35bf1b9501bdf7eb9c21.tar.gz plan9port-cc9ecfbee7b1c346bd6b35bf1b9501bdf7eb9c21.tar.bz2 plan9port-cc9ecfbee7b1c346bd6b35bf1b9501bdf7eb9c21.zip |
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
Diffstat (limited to 'src/cmd/devdraw')
-rw-r--r-- | src/cmd/devdraw/cocoa-screen-metal.m | 90 |
1 files changed, 27 insertions, 63 deletions
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<MTLDevice> device; -static id<MTLRenderPipelineState> pipelineState; static id<MTLCommandQueue> commandQueue; static id<MTLTexture> texture; @@ -74,32 +72,6 @@ static Memimage *img = NULL; static QLock snarfl; -static NSString *const metal = -@"#include<metal_stdlib>\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<half> 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<MTLLibrary> 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<MTLCommandBuffer> cbuf; - id<MTLRenderCommandEncoder> cmd; + id<MTLBlitCommandEncoder> 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; |