gpt4 book ai didi

swift2 - 为什么我没有得到这个 Metal 内核的结果

转载 作者:行者123 更新时间:2023-12-02 06:39:44 25 4
gpt4 key购买 nike

我试图了解 Metal 计算着色器的工作原理,因此我编写了以下代码:

class AppDelegate: NSObject, NSApplicationDelegate {


var number:Float!
var buffer:MTLBuffer!


func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
let metalDevice = MTLCreateSystemDefaultDevice()!
let library = metalDevice.newDefaultLibrary()!
let commandQueue = metalDevice.newCommandQueue()
let commandBuffer = commandQueue.commandBuffer()
let commandEncoder = commandBuffer.computeCommandEncoder()


let pointlessFunction = library.newFunctionWithName("pointless")!
let pipelineState = try! metalDevice.newComputePipelineStateWithFunction(pointlessFunction)
commandEncoder.setComputePipelineState(pipelineState)
number = 12

buffer = metalDevice.newBufferWithBytes(&number, length: sizeof(Float), options: MTLResourceOptions.StorageModeShared)
commandEncoder.setBuffer(buffer, offset: 0, atIndex: 0)


commandEncoder.endEncoding()

commandBuffer.commit()
commandBuffer.waitUntilCompleted()

let data = NSData(bytesNoCopy: buffer.contents(), length: sizeof(Float), freeWhenDone: false)
var newResult:Float = 0
data.getBytes(&newResult, length: sizeof(Float))
print(newResult)




}

通过使用 StorageModeShared 创建缓冲区,我希望对 Metal 缓冲区所做的更改反射(reflect)在我的 Swift 代码中,但是当我填充 newResult 变量时,看起来缓冲区的值仍然与在开头 (12) 而应该是 125 :

#include <metal_stdlib>
using namespace metal;



kernel void pointless (device float* outData [[ buffer(0) ]]) {

*outData = 125.0;
}

我做错了什么?

最佳答案

除非您分派(dispatch)内核函数,否则它不会运行。我认为你假设如果你有一个函数,那么 Metal 应该运行它一次,直到你另有说明,但这不会发生。相反,它根本不会运行。在 endEncoding 之前添加此内容,就可以开始了!

let size = MTLSize(width: 1, height: 1, depth: 1)
commandEncoder.dispatchThreadgroups(size, threadsPerThreadgroup: size)

关于swift2 - 为什么我没有得到这个 Metal 内核的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34554941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com