我是MetalKit的新手,并试图将本教程从playground转换回OSX应用程序:
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)")
}
}
}我在线路上有个错误:
command_encoder.setTexture(drawable.texture, atIndex: 0)失败的断言“`frameBufferOnly纹理不支持计算”。
我怎么解决这个问题?
发布于 2016-08-29 18:53:36
如果您想从计算函数中写入绘图的纹理,则需要告诉MTKView,它应该将其层配置为不只是帧缓冲区:
metalView.framebufferOnly = false如果将此值设置为false,您的可绘制将为您提供带有shaderWrite用法标志集的纹理,这是从着色器函数编写纹理时所必需的。
发布于 2019-09-03 07:18:01
即使我将frameBufferOnly设置为false,并在使用可绘制之前打印布尔值以进行双重检查,我也有相同的问题。
但是,当我关闭GPU frame capture时,就不会再有断言了(至少现在是这样)。还是不知道为什么。
Menu > Scheme > Edit Scheme > Run > Options > GPU Frame Capture:禁用
https://stackoverflow.com/questions/39206935
复制相似问题