From 41f290f54e385f28257bd24e8f399bcb7f3727b7 Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 25 May 2019 19:29:34 +0200 Subject: cocoa-cb: add support for 10bit opengl rendering this will request a 16bit half-float framebuffer instead if a 8bit integer framebuffer. Fixes #3613 --- video/out/cocoa-cb/video_layer.swift | 57 ++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'video/out/cocoa-cb') diff --git a/video/out/cocoa-cb/video_layer.swift b/video/out/cocoa-cb/video_layer.swift index 301b900de9..95727ee01f 100644 --- a/video/out/cocoa-cb/video_layer.swift +++ b/video/out/cocoa-cb/video_layer.swift @@ -37,10 +37,16 @@ let glFormatSoftwareBase: [CGLPixelFormatAttribute] = [ kCGLPFADoubleBuffer ] -let glFormatOptional: [CGLPixelFormatAttribute] = [ - kCGLPFABackingStore, - kCGLPFAAllowOfflineRenderers, - kCGLPFASupportsAutomaticGraphicsSwitching +let glFormatOptional: [[CGLPixelFormatAttribute]] = [ + [kCGLPFABackingStore], + [kCGLPFAAllowOfflineRenderers], + [kCGLPFASupportsAutomaticGraphicsSwitching] +] + +let glFormat10Bit: [CGLPixelFormatAttribute] = [ + kCGLPFAColorSize, + _CGLPixelFormatAttribute(rawValue: 64), + kCGLPFAColorFloat ] let attributeLookUp: [UInt32:String] = [ @@ -52,6 +58,8 @@ let attributeLookUp: [UInt32:String] = [ kCGLPFAAccelerated.rawValue: "kCGLPFAAccelerated", kCGLPFADoubleBuffer.rawValue: "kCGLPFADoubleBuffer", kCGLPFABackingStore.rawValue: "kCGLPFABackingStore", + kCGLPFAColorSize.rawValue: "kCGLPFAColorSize", + kCGLPFAColorFloat.rawValue: "kCGLPFAColorFloat", kCGLPFAAllowOfflineRenderers.rawValue: "kCGLPFAAllowOfflineRenderers", kCGLPFASupportsAutomaticGraphicsSwitching.rawValue: "kCGLPFASupportsAutomaticGraphicsSwitching", ] @@ -67,6 +75,7 @@ class VideoLayer: CAOpenGLLayer { var needsFlip: Bool = false var forceDraw: Bool = false var surfaceSize: NSSize = NSSize(width: 0, height: 0) + var bufferDepth: GLint = 8 enum Draw: Int { case normal = 1, atomic, atomicEnd } var draw: Draw = .normal @@ -92,12 +101,16 @@ class VideoLayer: CAOpenGLLayer { init(cocoaCB ccb: CocoaCB) { cocoaCB = ccb - cglPixelFormat = VideoLayer.createPixelFormat(ccb.mpv) + (cglPixelFormat, bufferDepth) = VideoLayer.createPixelFormat(ccb.mpv) cglContext = VideoLayer.createContext(ccb.mpv, cglPixelFormat) super.init() autoresizingMask = [.layerWidthSizable, .layerHeightSizable] backgroundColor = NSColor.black.cgColor + if #available(macOS 10.12, *), bufferDepth > 8 { + contentsFormat = .RGBA16Float + } + var i: GLint = 1 CGLSetParameter(cglContext, kCGLCPSwapInterval, &i) CGLSetCurrentContext(cglContext) @@ -150,7 +163,7 @@ class VideoLayer: CAOpenGLLayer { } updateSurfaceSize() - mpv.drawRender(surfaceSize, ctx) + mpv.drawRender(surfaceSize, bufferDepth, ctx) if needsICCUpdate { needsICCUpdate = false @@ -206,7 +219,7 @@ class VideoLayer: CAOpenGLLayer { if isUpdate && needsFlip { CGLSetCurrentContext(cglContext) if mpv.isRenderUpdateFrame() { - mpv.drawRender(NSZeroSize, cglContext, skip: true) + mpv.drawRender(NSZeroSize, bufferDepth, cglContext, skip: true) } } displayLock.unlock() @@ -222,17 +235,18 @@ class VideoLayer: CAOpenGLLayer { } } - class func createPixelFormat(_ mpv: MPVHelper) -> CGLPixelFormatObj { + class func createPixelFormat(_ mpv: MPVHelper) -> (CGLPixelFormatObj, GLint) { var pix: CGLPixelFormatObj? + var depth: GLint = 8 var err: CGLError = CGLError(rawValue: 0) let swRender = mpv.macOpts?.cocoa_cb_sw_renderer ?? -1 if swRender != 1 { - (pix, err) = VideoLayer.findPixelFormat(mpv) + (pix, depth, err) = VideoLayer.findPixelFormat(mpv) } if (err != kCGLNoError || pix == nil) && swRender != 0 { - (pix, err) = VideoLayer.findPixelFormat(mpv, software: true) + (pix, depth, err) = VideoLayer.findPixelFormat(mpv, software: true) } guard let pixelFormat = pix, err == kCGLNoError else { @@ -240,10 +254,10 @@ class VideoLayer: CAOpenGLLayer { exit(1) } - return pixelFormat + return (pixelFormat, depth) } - class func findPixelFormat(_ mpv: MPVHelper, software: Bool = false) -> (CGLPixelFormatObj?, CGLError) { + class func findPixelFormat(_ mpv: MPVHelper, software: Bool = false) -> (CGLPixelFormatObj?, GLint, CGLError) { var pix: CGLPixelFormatObj? var err: CGLError = CGLError(rawValue: 0) var npix: GLint = 0 @@ -251,22 +265,27 @@ class VideoLayer: CAOpenGLLayer { for ver in glVersions { var glBase = software ? glFormatSoftwareBase : glFormatBase glBase.insert(CGLPixelFormatAttribute(ver.rawValue), at: 1) - var glFormat = glBase + glFormatOptional - for index in stride(from: glFormat.count-1, through: glBase.count-1, by: -1) { - let format = glFormat + [_CGLPixelFormatAttribute(rawValue: 0)] + var glFormat = [glBase] + if (mpv.macOpts?.cocoa_cb_10bit_context == 1) { + glFormat += [glFormat10Bit] + } + glFormat += glFormatOptional + + for index in stride(from: glFormat.count-1, through: 0, by: -1) { + let format = glFormat.flatMap { $0 } + [_CGLPixelFormatAttribute(rawValue: 0)] err = CGLChoosePixelFormat(format, &pix, &npix) if err == kCGLBadAttribute || err == kCGLBadPixelFormat || pix == nil { glFormat.remove(at: index) } else { - let attArray = glFormat.map({ (value: _CGLPixelFormatAttribute) -> String in - return attributeLookUp[value.rawValue] ?? "unknown attribute" + let attArray = format.map({ (value: _CGLPixelFormatAttribute) -> String in + return attributeLookUp[value.rawValue] ?? String(value.rawValue) }) mpv.sendVerbose("Created CGL pixel format with attributes: " + "\(attArray.joined(separator: ", "))") - return (pix, err) + return (pix, glFormat.contains(glFormat10Bit) ? 16 : 8, err) } } } @@ -280,7 +299,7 @@ class VideoLayer: CAOpenGLLayer { mpv.sendWarning("Falling back to software renderer") } - return (pix, err) + return (pix, 8, err) } class func createContext(_ mpv: MPVHelper, _ pixelFormat: CGLPixelFormatObj) -> CGLContextObj { -- cgit v1.2.3