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

标签 swift2 xcode7 metal

我试图了解 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/

相关文章:

swift - 获取未知类型数组的元素和计数

ios - AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

c++ - 为什么在 C++ 顶点函数中有两个括号 [?

ios - 以相同的 Alpha 混合 Metal

ios - 将用户限制为一个小数点 UILabel Swift2

ios - Xcode 7.1 iOS 9.1 CLLocationManager 不总是工作?

ios - 在 swift 2.0 中构建自定义相机应用程序时出错

image - 将图像转换为数据,反之亦然

ios - xcode 7 TableView Controller

ios - 相当于 iOS Metal 中的 Open GL 精度属性(lowp、mediop、highp)