我无法在 macOS Designed for iPad
目标上运行在 iOS 上运行良好的应用程序,但在 MTKView
中运行 M1 处理器的 Mac 上运行。
尽管出现以下消息,但它似乎仍在工作,但它非常不稳定且缓慢:
<块引用>[14917:391638] [GPUDebug] 执行内核的设备加载无效 函数“计算”编码器:“0”,调度:0,偏移量 896024
缓冲区:“” [14917:391037] [生命周期] 场景状态更改错误:场景不再连接。 (NSMenuBarScene_D600133B-F270-427C-A7E8-1DCF225C886F) [14917:391635] [GPUDebug] 执行内核函数“计算”编码器的设备加载无效:“0”,调度:0,偏移量 896024
然后他们重复:
<块引用>缓冲区:“” ::: delegate_identifier:GPUToolsDiagnostics [14917:391640] [Metal Diagnostics] 消息:执行内核函数“计算”编码器的设备加载无效:“0”,调度:0,在 偏移量 896024
还有许多其他 Metal
函数正在使用(包括内核类型),但它们没有报告任何错误。
内核定义为:
kernel void compute ( device Point *points [[buffer(0)]],
device Particle *out [[buffer(1)]],
device atomic_int *counter [[buffer(2)]],
device Sample *samples [[buffer(3)]],
device Settings *settings[[buffer(4)]],
device Record *records[[buffer(5)]],
constant ComputeParameters *params [[buffer(6)]],
texture2d<half> colorMap [[ texture(0) ]],
texture2d<half> colorMap2 [[ texture(1) ]],
uint id [[thread_position_in_grid]]) {
管道配置为:
do {
guard let library = device.makeDefaultLibrary(),
let function = library.makeFunction(name: "compute") else { return }
computeState = try device.makeComputePipelineState(function: function)
} catch let error {
print(error.localizedDescription)
}
它被调用:
guard let computeBuffer = commandQueue.makeCommandBuffer() else { return }
guard let computeEncoder = computeBuffer.makeComputeCommandEncoder() else { return }
computeEncoder.setComputePipelineState(computeState)
let width = computePipelineState.threadExecutionWidth
let threadsPerGroup = MTLSizeMake(width, 1, 1)
func dispatchThreads(particleCount: Int) {
let threadsPerGrid = MTLSizeMake(particleCount, 1, 1)
computeArtPointsEncoder.dispatchThreads(threadsPerGrid,
threadsPerThreadgroup: threadsPerGroup)
}
computeEncoder.setBuffer(pointBuffer, offset: MemoryLayout<Point>.stride, index: 0)
computeEncoder.setBuffer(outBuffer, offset: 0, index: 1)
computeEncoder.setBuffer(counterBuffer, offset: 0, index: 2)
computeEncoder.setBuffer(sampleBuffer, offset: 0, index: 3)
computeEncoder.setBuffer(settingsBuffer, offset: 0, index: 4)
computeEncoder.setBuffer(recordBuffer, offset: 0, index: 5)
computeEncoder.setBytes(¶ms, length: MemoryLayout<ComputeParameters>.size, index: 6)
computeEncoder.setTexture(colorTexture, index: 0)
computeEncoder.setTexture(colorTexture2, index: 1)
if device.supportsFeatureSet(.iOS_GPUFamily4_v1) {
dispatchThreads(particleCount: Int(params.length))
} else {
let threads = min(computePipelineState.threadExecutionWidth, Int(params.length))
let threadsPerThreadgroup = MTLSize(width: threads,
height: 1,
depth: 1)
let groups = Int(params.length) / threads + 1
let groupsPerGrid = MTLSize(width: groups,
height: 1,
depth: 1)
computeEncoder.dispatchThreadgroups(groupsPerGrid, threadsPerThreadgroup: threadsPerThreadgroup)
}
computeEncoder.endEncoding()
computeBuffer.commit()
computeBuffer.waitUntilCompleted()