[MetalKit]9-Using-MetalKit-part-8使用MetalKit8

本系列文章是對 metalkit.org 上面MetalKit內容的全面翻譯和學習.git

MetalKit系統文章目錄github


如今,你能夠注意到我很喜歡Swift.同時我也是個Xcodeplaygrounds的狂熱愛好者.本週咱們會把咱們的Metal代碼放到playground中.歡呼吧,爲了playgrounds中的Metal原型!swift

讓咱們從建立一個新的OS XXcodeplayground開始.建立好後,單擊Show the Assistant editor按鈕並按下Command + 1顯示Project navigator導航區.你的playground看起來應該這樣: bash

chapter08_1.png

第一件事就是在Project navigator中的Resources文件夾下建立Shaders.metal,代碼和本系列前一章節保持一致.而後,咱們在Sources文件夾下建立MathUtils.swiftMetalView.swift.唯一要改動的地方是在MathUtils.swift中爲Vertex結構體建立一個初始化方法:ide

struct Vertex {
    var position: vector_float4
    var color: vector_float4
    init(pos: vector_float4, col: vector_float4) {
        position = pos
        color = col
    }
}
複製代碼

MetalView.swift中咱們須要改的多一些.首先,咱們要讓類是public,由於咱們將在Sources文件夾外調用它.所以,初始化方法和drawRect(:) 方法也必須是public的.同時,咱們再建立第二個初始化方法,這樣咱們就可能建立一個指定frameMetalView了:post

public class MetalView: MTKView {
    ... 
    required public init(coder: NSCoder) {
        super.init(coder: coder)
    }

    override public init(frame frameRect: CGRect, device: MTLDevice?) {
        super.init(frame: frameRect, device: device)
        createBuffers()
        registerShaders()
    }
    ... 
}
複製代碼

下一步,咱們須要作一些奇妙的改動,建立一個Library:學習

let library = device.newDefaultLibrary()!
複製代碼

提示錯誤信息:ui

MTLLibrary.mm:1016: failed assertion `filepath must not be nil.' 複製代碼

這是由於playground沒有默認的filepath來給咱們使用,咱們須要本身建立:spa

func registerShaders() {
    let path = NSBundle.mainBundle().pathForResource("Shaders", ofType: "metal")
    let input: String?
    let library: MTLLibrary
    let vert_func: MTLFunction
    let frag_func: MTLFunction
    do {
        input = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
        library = try device!.newLibraryWithSource(input!, options: nil)
        vert_func = library.newFunctionWithName("vertex_func")!
        frag_func = library.newFunctionWithName("fragment_func")!
        let rpld = MTLRenderPipelineDescriptor()
        rpld.vertexFunction = vert_func
        rpld.fragmentFunction = frag_func
        rpld.colorAttachments[0].pixelFormat = .BGRA8Unorm
        rps = try device!.newRenderPipelineStateWithDescriptor(rpld)
    } catch let e {
        Swift.print("\(e)")
    }
}
複製代碼

注意,咱們告訴playground去找到名爲Shaders類型爲metal的資源的存放路徑.而後,咱們將文件轉換爲一個長的String並用這些資源建立library.翻譯

最後,咱們到playground的主頁並建立一個帶有frame的MetalView.而後咱們告訴playground展現活躍視圖:

import Cocoa
import XCPlayground

let device = MTLCreateSystemDefaultDevice()!
let frame = NSRect(x: 0, y: 0, width: 300, height: 300)
let view = MetalView(frame: frame, device: device)
XCPlaygroundPage.currentPage.liveView = view
複製代碼

若是你顯示了Assistant editor區的Timeline,你會看到一個相似的視圖:

chapter08_2.png

源代碼source code 已發佈在Github上.

下次見!

相關文章
相關標籤/搜索