首页 > 解决方案 > Metal GPU 帧时间行为不直观

问题描述

我在自己的应用程序中遇到了一个有趣的 Metal 性能问题,我只需对这个示例项目进行小幅调整就可以重现该问题。我的视图大小约为 1600x900,如下所示:

在此处输入图像描述

每帧有两个绘制调用,一个用于背景,一个用于线条。背景由 4 个顶点组成,线条大约有 2000 个顶点。当像上面那样绘制场景时,Xcode 的 GPU 帧捕获告诉我整个帧需要大约 4 毫秒(!)。一些意见:

这对我来说没有意义。为什么上述变化会对帧时间产生如此巨大的影响?这是100倍的差异。

我正在 2018 Mac mini(使用 Intel UHD Graphics 630 1536 MB)上运行代码,以防万一。


以下是对演示项目所做的更改:

  1. 在初始化期间创建两个MTLBuffers
AAPLVertex quadVertices[] = { ... 4 vertices omitted ... };
quadBuffer = [_device newBufferWithBytes:quadVertices length:4 * sizeof(AAPLVertex) MTLResourceStorageModeManaged];

AAPLVertex dataVertices[] = { ... ~2000 vertices omitted ... };
dataBuffer = [_device newBufferWithBytes:dataVertices length:2000 * sizeof(AAPLVertex) MTLResourceStorageModeManaged];
  1. 绘制两个缓冲区drawInMTKView
[renderEncoder setVertexBuffer:quadBuffer offset:0 atIndex:AAPLVertexInputIndexVertices];
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];

[renderEncoder setVertexBuffer:dataBuffer offset:0 atIndex:AAPLVertexInputIndexVertices];
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:2000];
  1. 打开 8x MSAA:mtkView.sampleCount = 8;pipelineStateDescriptor.sampleCount = 8;

  2. 将渲染通道的加载操作更改为MTLLoadActionLoadrenderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionLoad;

编辑:该项目在我的 Github 上可用

编辑 2:我在 2020 M1 Macbook 上运行了示例项目,但我无法重现任何要点。基本情况的总帧时间约为 100 µs。不过,我不得不使用 4 的 MSAA 系数,因为 M1 显然不支持 8。


为了透明起见,我还在 Apple Developer 论坛上问过这个问题:https ://developer.apple.com/forums/thread/695245 (我希望没关系)

标签: macosgpuintelmetalobjective-c++

解决方案


看起来这是一个错误,我在以下处理器上运行了您的示例项目:
M1、M1 Pro、M1 Max、Radeon pro 5500M

我无法重现您的问题。我建议你提交错误报告。


推荐阅读