2016-08-29 7 views
3

私はMetalKitに新しいですし、OSXアプリに戻ってplaygroundからthis tutorialを変換しようとしている:MetalKit - Drawable.textureのアサーションエラー

command_encoder.setTexture(drawable.texture, atIndex: 0) 
:私はラインでエラーが発生しました
import MetalKit 

public class MetalView: MTKView { 

    var queue: MTLCommandQueue! = nil 
    var cps: MTLComputePipelineState! = nil 

    required public init(coder: NSCoder) { 
     super.init(coder: coder) 
     device = MTLCreateSystemDefaultDevice() 
     registerShaders() 
    } 


    override public func drawRect(dirtyRect: NSRect) { 
     super.drawRect(dirtyRect) 
     if let drawable = currentDrawable { 
      let command_buffer = queue.commandBuffer() 
      let command_encoder = command_buffer.computeCommandEncoder() 
      command_encoder.setComputePipelineState(cps) 
      command_encoder.setTexture(drawable.texture, atIndex: 0) 
      let threadGroupCount = MTLSizeMake(8, 8, 1) 
      let threadGroups = MTLSizeMake(drawable.texture.width/threadGroupCount.width, drawable.texture.height/threadGroupCount.height, 1) 
      command_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) 
      command_encoder.endEncoding() 
      command_buffer.presentDrawable(drawable) 
      command_buffer.commit() 
     } 
    } 

    func registerShaders() { 
     queue = device!.newCommandQueue() 
     do { 
      let library = device!.newDefaultLibrary()! 
      let kernel = library.newFunctionWithName("compute")! 
      cps = try device!.newComputePipelineStateWithFunction(kernel) 
     } catch let e { 
      Swift.print("\(e)") 
     } 
    } 
} 

アサーションに失敗しました `frameBufferOnly textureは計算でサポートされていません。 '

どうすれば解決できますか?

答えて

6

あなたは計算機能から描画可能のテクスチャに書き込みたい場合、あなたはそれがフレームバッファだけではない、その層を構成する必要があることをMTKViewを伝える必要があります:この値を設定すると

metalView.framebufferOnly = false 

falseにすると、シェイダー機能からテクスチャを書き込むときに必要となる、shaderWrite使用フラグが設定されたテクスチャがドロアブルから出力されます。

+0

ここでこれを設定する必要がありますか? 'MetalView'クラスの中で? – sooon

+0

OK、私はそれを得た。私は 'self.frameBufferOnly = false'をセットして動作します。 – sooon

+0

これは奇妙なことですが、私がプレイグラウンドでコードを実行すると、私はこの行を設定する必要はありませんでしたが、私はアプリケーションにコードを採用する場合、これを追加する必要があります。 – XueYu

関連する問題