Created
July 25, 2020 15:17
-
-
Save Akemi/b6179bc155b0388cb2dcbed6a63734be to your computer and use it in GitHub Desktop.
metal_layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class MetalLayer: CAMetalLayer { | |
unowned var common: MacCommon | |
override var device: MTLDevice? { | |
didSet{ | |
if oldValue == nil { | |
//clearBuffer() | |
} | |
} | |
} | |
init(common com: MacCommon) { | |
common = com | |
super.init() | |
//layer.framebufferOnly = false | |
//layer.drawableSize = NSSize(width: 1024, height: 576) | |
pixelFormat = .rgba16Float | |
backgroundColor = NSColor.black.cgColor | |
if #available(macOS 10.13, *) { | |
displaySyncEnabled = true | |
} | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func clearBuffer() { | |
if let device = device, let drawable = nextDrawable() { | |
let commandQueue = device.makeCommandQueue() | |
let commandBuffer = commandQueue?.makeCommandBuffer() | |
let texture = drawable.texture | |
let desc = MTLRenderPassDescriptor() | |
desc.colorAttachments[0].texture = texture | |
desc.colorAttachments[0].loadAction = .clear | |
desc.colorAttachments[0].storeAction = .store | |
desc.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1.0) | |
let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: desc)! | |
commandEncoder?.endEncoding() | |
commandBuffer?.present(drawable) | |
commandBuffer?.commit() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment