Apple Metal API“setVertexBuffer:offset:atIndex:”中的问题

Posted

技术标签:

【中文标题】Apple Metal API“setVertexBuffer:offset:atIndex:”中的问题【英文标题】:issues in Apple Metal API "setVertexBuffer:offset:atIndex:" 【发布时间】:2019-11-08 13:32:18 【问题描述】:

我是 Apple Metal 的新手,在运行苹果示例代码“Creating and Sampling Textures”时,我喜欢一些奇怪的东西,“图 1”是我的 Macmini 2018 中的结果( GPU是Intel(R) UHD Graphics 630),可以看到quad的左下角被挤压,origin源使用setVertexBuffer:offset:atIndex:设置顶点缓冲区,我将其替换为setVertexBytes:length:atIndex:运行,结果显示为“图 2”,似乎一切正常,然后我在另一台机器(Macbookpro,GPU 是 GeForce GT 750M)上构建并运行源代码,quad渲染是否正确,结果显示为“图 2”,那么源是否缺少某些内容或函数 setVertexBuffer:offset:atIndex: 中是否存在一些问题?

图一

图2

// Apple's origin source
//
static const AAPLVertex quadVertices[] =
    
        // Pixel positions, Texture coordinates
           250,  -250 ,   1.f, 1.f  ,
          -250,  -250 ,   0.f, 1.f  ,
          -250,   250 ,   0.f, 0.f  ,

           250,  -250 ,   1.f, 1.f  ,
          -250,   250 ,   0.f, 0.f  ,
           250,   250 ,   1.f, 0.f  ,
    ;
...
...
...
//Create a vertex buffer, and initialize it with the quadVertices array
_vertices = [_device newBufferWithBytes:quadVertices
                                length:sizeof(quadVertices)
                               options:MTLResourceOptionCPUCacheModeDefault];
...
...
// set the vertex buffer
[renderEncoder setVertexBuffer:_vertices
                        offset:0
                      atIndex:AAPLVertexInputIndexVertices];

我将 quadVertices 作为全局数组,并替换 "setVertexBuffer:offset:atIndex:" 到 "setVertexBytes:length:atIndex:"

[renderEncoder setVertexBytes:&quadVertices
                       length:sizeof(quadVertices)
                       atIndex:AAPLVertexInputIndexVertices];

【问题讨论】:

我怀疑这是英特尔 GPU 的驱动程序错误。 有趣的是,如果您每帧将顶点复制到缓冲区中,它就会“工作”(在我的 Intel HD Graphics 530 上)。驱动程序端的某些东西似乎只破坏了缓冲区中的第 5 个浮点数(将其设置为值 2^-134)。如果您愿意浪费缓冲区的前 256 个字节,则将数据偏移到该位置是比复制每一帧更便宜的解决方法。但这绝对应该通过反馈报告。 问题设备是否有可用的 OSX 更新?也许这是一个已经修复的驱动程序错误。 【参考方案1】:

最后,我在缓冲区创建函数中将MTLResourceOptions类型改为“MTLResourceCPUCacheModeWriteCombined”,问题解决了。

_vertices = [_device newBufferWithBytes:quadVertices
                                 length:sizeof(quadVertices)
             options:MTLResourceCPUCacheModeWriteCombined];

现在该示例在 MacMini 2018(GPU 是 Intel(R) UHD Graphics 630)和 Macbookpro(GPU 是 GeForce GT 750M)中都可以正常工作。

【讨论】:

听起来更像是一种创可贴,而不是真正的解决方案。我仍然鼓励你file Feedback。 反馈已提交。

以上是关于Apple Metal API“setVertexBuffer:offset:atIndex:”中的问题的主要内容,如果未能解决你的问题,请参考以下文章

METAL渲染是什么?

metal 优化数据分析

Apple Metal元素矩阵乘法(Hadamard产品)

Metal并行计算以及Metal程序的命令行编译

metal docs--Synchronization&memory management

Metal2剖析:基于MPS的GPU加速光线追踪(Accelerating Ray Tracing)