【问题标题】:Metal rendering really slow - how to speed it up金属渲染真的很慢 - 如何加快速度
【发布时间】:2015-07-17 04:57:42
【问题描述】:

我有一个运行速度极慢的金属应用程序,需要运行得更快。我相信问题是我创建了太多的 MTLCommandBuffer 对象。

我创建这么多 MTLCommandBuffer 对象的原因是我需要向像素着色器发送不同的统一值。我粘贴了一段代码来说明下面的问题。

  for (int obj_i = 0 ; obj_i < n ; ++obj_i)
  {
     // I create one render command buffer per object I draw so I can use  different uniforms
     id <MTLCommandBuffer> mtlCommandBuffer = [metal_info.g_commandQueue commandBuffer];
     id <MTLRenderCommandEncoder> renderCommand = [mtlCommandBuffer renderCommandEncoderWithDescriptor:<#(MTLRenderPassDescriptor *)#>]

     // glossing over details, but this call has per object specific data
     memcpy([global_uniform_buffer contents], per_object_data, sizeof(per_data_object));

     [renderCommand setVertexBuffer:object_vertices  offset:0 atIndex:0];
     // I am reusing a single buffer for all shader calls
     // this is killing performance
     [renderCommand setVertexBuffer:global_uniform_buffer offset:0 atIndex:1];

     [renderCommand drawIndexedPrimitives:MTLPrimitiveTypeTriangle
                               indexCount:per_object_index_count
                               indexType:MTLIndexTypeUInt32
                             indexBuffer:indicies
                       indexBufferOffset:0];
     [renderCommand endEncoding];
     [mtlCommandBuffer presentDrawable:frameDrawable];
     [mtlCommandBuffer commit];
}  

上述代码按预期绘制,但速度极慢。我猜是因为有比为每个对象创建一个 MTLCommandBuffer 更好的方法来强制像素着色器评估。

我考虑过简单地分配一个比单个着色器通道所需的缓冲区大得多的缓冲区,并简单地使用偏移量在一个渲染命令编码器中将多个调用排队,然后执行它们。这种方法看起来很不正统,我想确保我解决了需要以对 Metal 友好的方式发送每个对象的自定义数据的问题。

使用每次调用自定义统一数据使用同一像素/顶点着色器的多个通道进行渲染的最快方法是什么?

【问题讨论】:

    标签: ios metal


    【解决方案1】:

    不要为每个对象重复使用相同的统一缓冲区。这样做会破坏 CPU 和 GPU 之间的所有并行性并导致常规同步点。

    相反,为您要在帧中渲染的每个对象创建一个单独的统一缓冲区。实际上,您应该真正为每个对象创建 2 个对象,并在每一帧之间交替使用,以便 GPU 可以在您准备 CPU 上的下一帧时渲染最后一帧。

    完成此操作后,您只需重构循环,以便命令缓冲区和渲染命令工作每帧完成一次。您的循环应该只包括复制统一数据、设置顶点缓冲区和调用绘图原语。

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多