2016-08-29 2 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)") 
     } 
    } 
} 

assertion 'frameBufferOnly 텍스처가 계산에 지원되지 않습니다.'

어떻게 해결할 수 있습니까?

답변

6

당신이 계산 기능에서 당김의 질감에 작성하려는 경우, 당신이 프레임 버퍼 전용으로하지의 레이어를 구성해야한다는 MTKView에게해야합니다 :이 설정 한 값으로

metalView.framebufferOnly = false 

false로 설정하면 drawable은 shaderWrite 사용 플래그가 설정된 텍스처를 제공합니다.이 플래그는 셰이더 함수에서 텍스처를 작성할 때 필요합니다.

+0

어디에서 설정해야합니까? MetalView 클래스 안에 있습니까? – sooon

+0

좋아, 알았다. 'self.frameBufferOnly = false'를 설정하면 작동합니다. – sooon

+0

놀랍습니다. 놀이터에서 코드를 실행하면이 행을 설정할 필요가 없습니다. 코드를 앱에 적용하면이 코드를 추가해야합니다. – XueYu