[MetalKit]8-Using-MetalKit-part-7使用MetalKit7

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

MetalKit系統文章目錄github


有一個讀者聯繫我說我看到一個很是奇怪的現象:當運行咱們教程的代碼時,MTLLibrary會在幾百次繪製調用後返回nil.這讓我意識到,我沒有考慮到根據文檔Metal documentation有些Metal對象是暫時的而有些則不是.謝謝Mike的提醒!swift

要解決這個問題,咱們須要再一次重構代碼.這實際上是件好事.咱們須要把非瞬時的Metal對象(devices, queues, data buffers, textures, states和pipelines)從drawRect(_:) 中拿出來,把它們放到視圖加載時只執行一次的方法裏.命令緩衝器和編碼器是僅有的兩個瞬時對象,設計出來供一次性使用的,因此咱們能夠在每次繪製調用時建立它們. 咱們將繼續從本系列的第5部分 part 5 開始.讓咱們建立一個新方法-一個初始化方法-它只在視圖加載時運行一次:app

required init(coder: NSCoder) {
    super.init(coder: coder)
    device = MTLCreateSystemDefaultDevice()
    createBuffers()
    registerShaders()
}
複製代碼

下一步,刪除render() 方法及在drawRect(_:)中的調用,由於咱們再也不須要它了.而後從sendToGPU() 移動全部代碼到drawRect(_:)中,並刪除sendToGPU()由於這個也不須要了.這樣咱們就從drawRect(_:)中移出了全部非瞬時的對象,只保留了command buffer命令緩衝器encoder編碼器在裏面,只有它們是瞬時對象.ide

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
    if let rpd = currentRenderPassDescriptor, drawable = currentDrawable {
        rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
        let command_buffer = device!.newCommandQueue().commandBuffer()
        let command_encoder = command_buffer.renderCommandEncoderWithDescriptor(rpd)
        command_encoder.setRenderPipelineState(rps)
        command_encoder.setVertexBuffer(vertex_buffer, offset: 0, atIndex: 0)
        command_encoder.setVertexBuffer(uniform_buffer, offset: 0, atIndex: 1)
        command_encoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
        command_encoder.endEncoding()
        command_buffer.presentDrawable(drawable)
        command_buffer.commit()
    }
}
複製代碼

最後,咱們建立一個新的類命名爲MathUtils,並將兩個結構體移動到裏面,這樣咱們就有了一個乾淨的視圖類.post

import simd

struct Vertex {
    var position: vector_float4
    var color: vector_float4
}

struct Matrix {
    var m: [Float]
    
    init() {
        m = [1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
        ]
    }
    
    func translationMatrix(var matrix: Matrix, _ position: float3) -> Matrix {
        matrix.m[12] = position.x
        matrix.m[13] = position.y
        matrix.m[14] = position.z
        return matrix
    }
    
    func scalingMatrix(var matrix: Matrix, _ scale: Float) -> Matrix {
        matrix.m[0] = scale
        matrix.m[5] = scale
        matrix.m[10] = scale
        matrix.m[15] = 1.0
        return matrix
    }
    
    func rotationMatrix(var matrix: Matrix, _ rot: float3) -> Matrix {
        matrix.m[0] = cos(rot.y) * cos(rot.z)
        matrix.m[4] = cos(rot.z) * sin(rot.x) * sin(rot.y) - cos(rot.x) * sin(rot.z)
        matrix.m[8] = cos(rot.x) * cos(rot.z) * sin(rot.y) + sin(rot.x) * sin(rot.z)
        matrix.m[1] = cos(rot.y) * sin(rot.z)
        matrix.m[5] = cos(rot.x) * cos(rot.z) + sin(rot.x) * sin(rot.y) * sin(rot.z)
        matrix.m[9] = -cos(rot.z) * sin(rot.x) + cos(rot.x) * sin(rot.y) * sin(rot.z)
        matrix.m[2] = -sin(rot.y)
        matrix.m[6] = cos(rot.y) * sin(rot.x)
        matrix.m[10] = cos(rot.x) * cos(rot.y)
        matrix.m[15] = 1.0
        return matrix
    }
    
    func modelMatrix(var matrix: Matrix) -> Matrix {
        matrix = rotationMatrix(matrix, float3(0.0, 0.0, 0.1))
        matrix = scalingMatrix(matrix, 0.25)
        matrix = translationMatrix(matrix, float3(0.0, 0.5, 0.0))
        return matrix
    }
}
複製代碼

運行程序確保你仍能看到壯麗的三角形,就像咱們上一部分看到的那樣. 源代碼source code 已發佈在Github上.學習

下次見!ui

相關文章
相關標籤/搜索