Metal入门资料008-临时对象处理

作者: 张芳涛 | 来源:发表于2018-06-13 10:44 被阅读35次

    写在前面:

    对Metal技术感兴趣的同学,可以关注我的专题:Metal专辑
    也可以关注我个人的简书账号:张芳涛
    所有的代码存储的Github地址是:Metal

    正文

    (作者的原话:)我们的一位读者联系了我,看到他看到的一个显然很奇怪的行为:在我们的教程中运行代码时,MTLLibrary在几百次绘制调用后返回nil。 根据Metal文档,这让我意识到我没有考虑到一些Metal对象是临时的,有些不是。 感谢Mike让我注意到这一点!

    为了处理这个问题,我们需要重新组织代码! 这总是一件好事。 我们需要从drawRect(_ :)中获取非暂时性的Metal对象(设备,队列,数据缓冲区,纹理,状态和管道),并将它们放入只在视图加载时才运行一次的方法中。 命令缓冲区和编码器是为一次性使用设计的唯一两个瞬态对象,因此我们可以在每次绘制调用时创建它们。

    我们将在系列文章的第5部分中提到我们离开的地方。 首先,让我们创建一个新的方法 - 初始化程序 - 只在加载视图时运行一次:

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

    接下来,删除render()方法以及drawRect(_ :)内的调用,因为我们不再需要它了。 然后将所有的代码从sendToGPU()移到drawRect(_ :)并删除sendToGPU(),因为我们不需要这个。 通过这种方式,我们将所有非暂态对象从drawRect(_ :)移开,并且只保留command buffer(命令缓冲区)和encoder(编码器),它们是唯一的两个瞬态对象。

    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的新类,并将这两个结构体移到它,以便我们有一个更清晰的视图类。

    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
     }
    }
    

    运行该程序以确保您仍然可以看到前一部分中已经看到的绚丽的三角形。

    相关文章

      网友评论

        本文标题:Metal入门资料008-临时对象处理

        本文链接:https://www.haomeiwen.com/subject/ykyreftx.html