[MetalKit]7-Using-MetalKit-part-6使用MetalKit6

本系列文章是对 metalkit.org 上面MetalKit内容的全面翻译和学习.html

MetalKit系统文章目录ios


让咱们来看看新的MetalKit框架和之前的Metal框架有什么不一样.它们是同时共存的,可是,MetalKit引入了一些强劲的新特性,好比:git

  • 纹理更容易加载(只需几行代码就能够异步加载).
  • Model I/O网格mash和Metal缓冲器buffer之间的高效数据传输.
  • MTKView-一个方便的Metal-aware Metal认识的视图(咱们稍后会深刻研究它).

我将从回顾第一篇中的程序开始:github

import MetalKit

class MetalView: MTKView {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        render()
    }
 
    func render() {
        device = MTLCreateSystemDefaultDevice()
        if let rpd = currentRenderPassDescriptor, drawable = currentDrawable {
            rpd.colorAttachments[0].clearColor = MTLClearColorMake(0, 0.5, 0.5, 1.0)
            let command_buffer = device!.newCommandQueue().commandBuffer()
            let command_encoder = command_buffer.renderCommandEncoderWithDescriptor(rpd)
            command_encoder.endEncoding()
            command_buffer.presentDrawable(drawable)
            command_buffer.commit()
        }
    }
}
复制代码

就是这样!简单优雅清理设备的背景色.如今咱们切换到Metal框架,它并无MTKView,因此咱们必须继承于NSView(iOS上是UIView).swift

import Cocoa

class MetalView: NSView {
复制代码

你会直接注意到一些错误提示,显示下面的属性默认状况下没有提供给咱们:app

  • a device设备
  • a drawable绘制资源
  • a render pass descriptor渲染通道描述符

让咱们修复这些错误.首先,由于NSView不是Metal-aware Metal认识的,咱们须要建立一个CAMetalLayer并告诉NSView将其用作本身的支持层.CAMetalLayerCore Animation层,它管理着一个用来渲染内容的纹理池.为了使用Metal来渲染,咱们须要用这个类做为咱们视图的支持层,经过在视图的layerClass() 类方法中返回图层来实现:框架

override class func layerClass() -> AnyClass {
    return CAMetalLayer.self
}

var metalLayer: CAMetalLayer {
    return self.layer as! CAMetalLayer
}
复制代码

接下来,在render() 函数中建立一个新的device并告诉metalLayer去引用它,还要设置层使用的像素格式.而后,建立一个drawable.注意,咱们并无使用MTKView提供的currentDrawable.而是,CAMetalLayer提供了一个nextDrawable给咱们使用.最后,建立一个渲染通道描述符.仍是须要注意,咱们没有提供currentRenderPassDescriptor:异步

let device = MTLCreateSystemDefaultDevice()!
metalLayer.device = device
metalLayer.pixelFormat = .BGRA8Unorm
let drawable = metalLayer.nextDrawable()
let texture = drawable!.texture
let rpd = MTLRenderPassDescriptor()
复制代码

在结束本章节以前,让咱们看一下MTKView类,去看看为何这是在咱们的app中用Metal渲染内容的最佳方式:ide

@available(OSX 10.11, *)
public class MTKView : NSView, NSCoding {
    public init(frame frameRect: CGRect, device: MTLDevice?)
    public init(coder: NSCoder)
    weak public var delegate: MTKViewDelegate?
    public var device: MTLDevice?
    public var currentDrawable: CAMetalDrawable? { get }
    public var framebufferOnly: Bool
    public var presentsWithTransaction: Bool
    public var colorPixelFormat: MTLPixelFormat
    public var depthStencilPixelFormat: MTLPixelFormat
    public var sampleCount: Int
    public var clearColor: MTLClearColor
    public var clearDepth: Double
    public var clearStencil: UInt32
    public var depthStencilTexture: MTLTexture? { get }
    public var multisampleColorTexture: MTLTexture? { get }
    public func releaseDrawables()
    public var currentRenderPassDescriptor: MTLRenderPassDescriptor? { get }
    public var preferredFramesPerSecond: Int
    public var enableSetNeedsDisplay: Bool
    public var autoResizeDrawable: Bool
    public var drawableSize: CGSize
    public var paused: Bool
    public func draw()
}

@available(OSX 10.11, *)
public protocol MTKViewDelegate : NSObjectProtocol {
    public func mtkView(view: MTKView, drawableSizeWillChange size: CGSize)
    public func drawInMTKView(view: MTKView)
}
复制代码

在这一大堆属性中,注意咱们感兴趣的几个:device, currentDrawablecurrentRenderPassDescriptor. 另外一个值得注意的地方,是这个类提供了一个协议,MTKViewDelegate属性.想要了解更多关于这些属性和函数的知识,要去看MTKView的参考文档.函数

源代码source code 已发布在Github上.

下次见!

相关文章
相关标签/搜索