【问题标题】:Why the Metal triple buffering model matters in official examples?为什么金属三重缓冲模型在官方示例中很重要?
【发布时间】:2020-02-15 11:45:24
【问题描述】:

Metal Best Practices 建议对动态数据缓冲区使用三重缓冲。但是文档中提供的清单和 Xcode 生成的默认 Metal 示例正在阻塞等待 GPU 完成工作的每一帧:

- (void)render
{
    // Wait until the inflight command buffer has completed its work
    dispatch_semaphore_wait(_frameBoundarySemaphore, DISPATCH_TIME_FOREVER);

   // TODO: Update dynamic buffers and send them to the GPU here !

   __weak dispatch_semaphore_t semaphore = _frameBoundarySemaphore;
    [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) {
        // GPU work is complete
        // Signal the semaphore to start the CPU work
        dispatch_semaphore_signal(semaphore);
    }];

    // CPU work is complete
    // Commit the command buffer and start the GPU work
    [commandBuffer commit];

}

那么三重缓冲是如何改进这里的呢?

【问题讨论】:

    标签: gpu rendering metal


    【解决方案1】:

    您在示例中没有发现的重要一点是:

    _frameBoundarySemaphore = dispatch_semaphore_create(kMaxInflightBuffers);

    正如dispatch_semaphore_create 的文档所说:

    传递大于零的值对于管理有限的资源池很有用,其中池大小等于该值。

    kMaxInflightBuffers 设置为 3 用于三重缓冲。对 dispatch_semaphore_wait 的前 3 次调用将成功,无需等待。

    【讨论】:

      猜你喜欢
      • 2011-07-04
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2019-02-02
      相关资源
      最近更新 更多