菜鸟教程小白 发表于 2022-12-11 16:38:56

iOS Metal Swift newComputePipelineStateWithFunction 不工作错误


                                            <p><p>我刚刚开始在 Swift 中使用 Metal 来尝试在 GPU 上进行一些并行计算,但我在使用 newComputePipelineStateWithFunction 时遇到了一些问题。我查看了多个网站,例如 <a href="https://developer.apple.com/library/ios/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/Compute-Ctx/Compute-Ctx.html#//apple_ref/doc/uid/TP40014221-CH6-SW6" rel="noreferrer noopener nofollow">Apple&#39;s Documentation for Data-Parallel Compute Processing</a>但我也遇到了错误。</p>

<p>这是我目前遇到的错误:</p>

<pre><code>Incorrect argument label in call (have &#39;_:error:&#39;, expected &#39;_:completionHandler:&#39;)
</code></pre>

<p>我尝试用完成处理程序替换“错误”,但在那里也遇到了困难。提前致谢。这是我的代码:</p>

<pre><code>import UIKit
import Metal

class ViewController: UIViewController {

    var device: MTLDevice! = nil
    var defaultLibrary: MTLLibrary! = nil
    var thefunc: MTLFunction! = nil
    var pipelineState: MTLComputePipelineState!
    var commandQueue: MTLCommandQueue! = nil

    override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.

      setupMetal()
    }

    override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
    }



    func setupMetal()
    {
      // Our default MTLDevice
      device = MTLCreateSystemDefaultDevice()
      defaultLibrary = device.newDefaultLibrary()
      commandQueue = device.newCommandQueue()

      // Define the kernel function
      let kernelFunction: MTLFunction = defaultLibrary.newFunctionWithName(&#34;particleRendererShader&#34;)!

      // Define the pipeline state
      let pipelineState: MTLComputePipelineState = device.newComputePipelineStateWithFunction(kernelFunction, error: nil)

      // Define the command buffer
      let commandBuffer: MTLCommandBuffer = commandQueue.commandBuffer()

      // Define the command encoder
      let commandEncoder: MTLComputeCommandEncoder = commandBuffer.computeCommandEncoder()
      commandEncoder.setComputePipelineState(pipelineState)



      // thefunc = library.newFunctionWithName(&#34;filter_main&#34;);
      // filterState = device.newComputePipelineStateWithFunction(thefunc, error: nil);
      // let kernelFunction = library.newFunctionWithName(&#34;filter_main&#34;)
      // pipelineState = device.newComputePipelineStateWithFunction(kernelFunction!, error: nil)



      // do {
      //   try pipelineState = device.newComputePipelineStateWithDescriptor(pipelineStateDescriptor)
      // } catch _ {
      //   print(&#34;Failed to create pipeline state, error&#34;)
      // }

    }

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为你需要:</p>

<pre><code>device.newComputePipelineStateWithFunction(function) { (state:MTLComputePipelineState?, error:NSError?) in

    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS Metal Swift newComputePipelineStateWithFunction 不工作错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37605155/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37605155/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS Metal Swift newComputePipelineStateWithFunction 不工作错误