gpt4 book ai didi

swift - MetalKit - Drawable.texture 断言错误

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:37 27 4
gpt4 key购买 nike

我是 MetalKit 的新手,正在尝试转换 this tutorialplayground 回到 OSX app:

    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)

failed assertion `frameBufferOnly texture not supported for compute.'

我该如何解决这个问题?

最佳答案

如果你想从一个计算函数写入一个可绘制对象的纹理,你需要告诉 MTKView 它应该配置它的层而不是 framebuffer-only:

metalView.framebufferOnly = false

将此值设置为 false,您的可绘制对象将为您提供设置了 shaderWrite 使用标志的纹理,这是从着色器函数写入纹理时所必需的。

关于swift - MetalKit - Drawable.texture 断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com