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

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

MetalKit系统文章目录


让我们来看看新的MetalKit框架和以前的Metal框架有什么不同.它们是同时共存的,但是,MetalKit引入了一些强劲的新特性,比如:

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

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

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).

import Cocoa

class MetalView: NSView {
复制代码

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

  • 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渲染内容的最佳方式:

@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上.

下次见!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值