我无法 运行 在我的 phone 中使用任何金属计算着色器

I cannot run any metal compute shader in my phone

我正在尝试 运行 我的 iPhone SE 上的金属程序。

我尝试了很多 threadsPerThreadGroup 和 threadsPerGrid 大小的数字,但它们都给我这个错误:TLValidateFeatureSupport:3539: failed assertion `Dispatch Threads with Non-Uniform Threadgroup Size is only supported on MTLGPUFamilyApple4 and later.'

这是我的代码。

var threadsPerThreadGroup: MTLSize
var threadsPerGrid: MTLSize

computeCommandEncoder.setComputePipelineState(updateShader)

let w = updateShader.threadExecutionWidth

threadsPerThreadGroup = MTLSize(width: w, height: 1, depth: 1)
threadsPerGrid = MTLSize(width: Int(constants.bufferLength), height: 1, depth: 1)

if(frames % 2 == 0) {
    computeCommandEncoder.setBuffer(buffer1, offset: 0, index: 0)
    computeCommandEncoder.setBuffer(buffer2, offset: 0, index: 1)
} else {
    computeCommandEncoder.setBuffer(buffer2, offset: 0, index: 0)
    computeCommandEncoder.setBuffer(buffer1, offset: 0, index: 1)
}

 computeCommandEncoder.setBytes(&constants, length: MemoryLayout<MyConstants>.stride, index: 2)

computeCommandEncoder.dispatchThreads(threadsPerGrid, threadsPerThreadgroup: threadsPerThreadGroup)

frames += 1

我正在使用 iOS 13.4 和 XCode 11.4。

threadExecutionWidth 的计算结果为 32,constants.bufferLength 为 512。

Use [dispatchThreads] only if the device supports non-uniform threadgroup sizes.

措辞不够清晰。这意味着 dispatchThreads 在 A11 之前的 GPU 上不起作用

如果你想要一个适用于所有设备的解决方案,你必须自己计算有多少线程组进入一个网格,并使用 dispatchThreadgroups

如果您想在代码中同时使用这两种方法,可以detect the device's feature set at runtime