Skip to content

Instantly share code, notes, and snippets.

@Akemi
Created July 25, 2020 15:17
Show Gist options
  • Save Akemi/b6179bc155b0388cb2dcbed6a63734be to your computer and use it in GitHub Desktop.
Save Akemi/b6179bc155b0388cb2dcbed6a63734be to your computer and use it in GitHub Desktop.
metal_layer
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