我正在写一个 View 来绘制Metal中的实时数据。我使用点图元绘制样本,并且对顶点和统一数据进行三重缓冲。我遇到的问题是,调用currentDrawable返回所需的时间似乎无法预测。几乎好像有时没有可绘制的东西,我必须等待整个框架才能使用。通常,currentDrawable返回的时间约为0.07毫秒(这与我的预期差不多),而其他时候则是完整的1/60 s。这导致整个主线程阻塞,至少可以说这不是很理想。

我在iPhone 6S Plus和iPad Air上看到此问题。我还没有在Mac上看到这种行为(我有2016年的MPB和AMD 460 GPU)。我的猜测是,这在某种程度上与iOS设备中的GPU基于TBDR的事实有关。我不认为我的带宽受到限制,因为无论我要绘制多少个样本,我都会得到完全相同的行为。

为了说明这个问题,我写了一个最小的示例,该示例绘制了一个静态正弦波。这是一个简化的示例,因为我通常会像处理制服一样将样本存储到当前的vertexBuffer中。这就是为什么我要对顶点数据和制服进行三重缓冲。但这仍然足以说明问题。只需将此 View 设置为 Storyboard 中的基本 View ,然后运行。在某些情况下,它可以正常运行。其他时间,currentDrawable的返回时间为16.67 ms,然后在几秒钟后跳转到0.07 ms,然后过一会返回16.67。如果出于某种原因旋转设备,它似乎从16.67跳到0.07。

MTKView子类

import MetalKit

let N = 500

class MetalGraph: MTKView {
    typealias Vertex = Int32

    struct Uniforms {
        var offset: UInt32
        var numSamples: UInt32
    }

    // Data
    var uniforms = Uniforms(offset: 0, numSamples: UInt32(N))

    // Buffers
    var vertexBuffers  = [MTLBuffer]()
    var uniformBuffers = [MTLBuffer]()
    var inflightBufferSemaphore = DispatchSemaphore(value: 3)
    var inflightBufferIndex = 0

    // Metal State
    var commandQueue: MTLCommandQueue!
    var pipeline: MTLRenderPipelineState!


    // Setup

    override func awakeFromNib() {
        super.awakeFromNib()

        device = MTLCreateSystemDefaultDevice()
        commandQueue = device?.makeCommandQueue()
        colorPixelFormat = .bgra8Unorm

        setupPipeline()
        setupBuffers()
    }

    func setupPipeline() {
        let library = device?.newDefaultLibrary()

        let descriptor = MTLRenderPipelineDescriptor()
        descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
        descriptor.vertexFunction   = library?.makeFunction(name: "vertexFunction")
        descriptor.fragmentFunction = library?.makeFunction(name: "fragmentFunction")

        pipeline = try! device?.makeRenderPipelineState(descriptor: descriptor)
    }

    func setupBuffers() {
        // Produces a dummy sine wave with N samples, 2 periods, with a range of [0, 1000]
        let vertices: [Vertex] = (0..<N).map {
            let periods = 2.0
            let scaled = Double($0) / (Double(N)-1) * periods * 2 * .pi
            let value = (sin(scaled) + 1) * 500 // Transform from range [-1, 1] to [0, 1000]
            return Vertex(value)
        }

        let vertexBytes  = MemoryLayout<Vertex>.size * vertices.count
        let uniformBytes = MemoryLayout<Uniforms>.size

        for _ in 0..<3 {
            vertexBuffers .append(device!.makeBuffer(bytes: vertices,  length: vertexBytes))
            uniformBuffers.append(device!.makeBuffer(bytes: &uniforms, length: uniformBytes))
        }
    }



    // Drawing

    func updateUniformBuffers() {
        uniforms.offset = (uniforms.offset + 1) % UInt32(N)

        memcpy(
            uniformBuffers[inflightBufferIndex].contents(),
            &uniforms,
            MemoryLayout<Uniforms>.size
        )
    }

    override func draw(_ rect: CGRect) {
        _ = inflightBufferSemaphore.wait(timeout: .distantFuture)

        updateUniformBuffers()

        let start = CACurrentMediaTime()
        guard let drawable = currentDrawable else { return }
        print(String(format: "Grab Drawable: %.3f ms", (CACurrentMediaTime() - start) * 1000))

        guard let passDescriptor = currentRenderPassDescriptor else { return }

        passDescriptor.colorAttachments[0].loadAction = .clear
        passDescriptor.colorAttachments[0].storeAction = .store
        passDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.2, 0.2, 0.2, 1)

        let commandBuffer = commandQueue.makeCommandBuffer()

        let encoder = commandBuffer.makeRenderCommandEncoder(descriptor: passDescriptor)
        encoder.setRenderPipelineState(pipeline)
        encoder.setVertexBuffer(vertexBuffers[inflightBufferIndex],  offset: 0, at: 0)
        encoder.setVertexBuffer(uniformBuffers[inflightBufferIndex], offset: 0, at: 1)
        encoder.drawPrimitives(type: .point, vertexStart: 0, vertexCount: N)
        encoder.endEncoding()

        commandBuffer.addCompletedHandler { _ in
            self.inflightBufferSemaphore.signal()
        }
        commandBuffer.present(drawable)
        commandBuffer.commit()

        inflightBufferIndex = (inflightBufferIndex + 1) % 3
    }
}

着色器
#include <metal_stdlib>
using namespace metal;

struct VertexIn {
    int32_t value;
};

struct VertexOut {
    float4 pos [[position]];
    float pointSize [[point_size]];
};

struct Uniforms {
    uint32_t offset;
    uint32_t numSamples;
};

vertex VertexOut vertexFunction(device   VertexIn *vertices [[buffer(0)]],
                                constant Uniforms *uniforms [[buffer(1)]],
                                uint vid [[vertex_id]])
{
    // I'm using the vertex index to evenly spread the
    // samples out in the x direction
    float xIndex = float((vid + (uniforms->numSamples - uniforms->offset)) % uniforms->numSamples);
    float x = (float(xIndex) / float(uniforms->numSamples - 1)) * 2.0f - 1.0f;

    // Transforming the values from the range [0, 1000] to [-1, 1]
    float y = (float)vertices[vid].value / 500.0f - 1.0f ;

    VertexOut vOut;
    vOut.pos = {x, y, 1, 1};
    vOut.pointSize = 3;

    return vOut;
}

fragment half4 fragmentFunction() {
    return half4(1, 1, 1, 1);
}

可能与此有关:在我所看到的所有示例中,inflightBufferSemaphore在信号通知信号之前(在我看来是有意义的),在commandBuffer的completionHandler中增加。当我有那条线时,我会得到奇怪的抖动效果,几乎就像帧缓冲区显示的顺序困惑一样。将此行移到绘制函数的底部可解决此问题,尽管对我而言并没有多大意义。我不确定这是否与currentDrawable的返回时间是否如此不可预测有关,但是我感到这两个问题是由同一个潜在问题引起的。

任何帮助将不胜感激!

最佳答案



嗯是的这是明确记录的。从Metal Programming Guide:



docs for CAMetalLayer.nextDrawable() :



除此之外,您的代码有些奇怪。您正在请求currentDrawable,但并未对其进行任何操作。 currentRenderPassDescriptor自动配置为使用currentDrawable的纹理。那么,如果您根本不自己请求currentDrawable会发生什么呢?

10-08 17:42