将 MetalView 与 SwiftUI 结合使用?我如何在那里放置一些东西来展示?

sto*_*hel 5 macos core-image swift metal swiftui

我一直坚持使用 SwiftUI 和 Metal,甚至快要放弃了。

我从https://developer.apple.com/forums/thread/119112?answerId=654964022#654964022得到了这个示例:

import MetalKit
struct MetalView: NSViewRepresentable {
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    func makeNSView(context: NSViewRepresentableContext<MetalView>) -> MTKView {
        let mtkView = MTKView()
        mtkView.delegate = context.coordinator
        mtkView.preferredFramesPerSecond = 60
        mtkView.enableSetNeedsDisplay = true
        if let metalDevice = MTLCreateSystemDefaultDevice() {
            mtkView.device = metalDevice
        }
        mtkView.framebufferOnly = false
        mtkView.clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)
        mtkView.drawableSize = mtkView.frame.size
        mtkView.enableSetNeedsDisplay = true
        return mtkView
    }
    func updateNSView(_ nsView: MTKView, context: NSViewRepresentableContext<MetalView>) {
    }
    class Coordinator : NSObject, MTKViewDelegate {
        var parent: MetalView
        var metalDevice: MTLDevice!
        var metalCommandQueue: MTLCommandQueue!
        
        init(_ parent: MetalView) {
            self.parent = parent
            if let metalDevice = MTLCreateSystemDefaultDevice() {
                self.metalDevice = metalDevice
            }
            self.metalCommandQueue = metalDevice.makeCommandQueue()!
            super.init()
        }
        func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
        }
        func draw(in view: MTKView) {
            guard let drawable = view.currentDrawable else {
                return
            }
            let commandBuffer = metalCommandQueue.makeCommandBuffer()
            let rpd = view.currentRenderPassDescriptor
            rpd?.colorAttachments[0].clearColor = MTLClearColorMake(0, 1, 0, 1)
            rpd?.colorAttachments[0].loadAction = .clear
            rpd?.colorAttachments[0].storeAction = .store
            let re = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd!)
            re?.endEncoding()
            commandBuffer?.present(drawable)
            commandBuffer?.commit()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

...但我无法理解如何使用这个 MetalView() 来显示数据,当我从 SwiftUI 视图调用它时,它似乎确实有效。我想用它来显示一个 CIImage,它将被 CIFilters 过滤和操作...

有人可以为我指出正确的方向,告诉我如何告诉这个视图如何显示某些东西吗?我想我需要它来显示纹理的内容,但尝试了无数个小时,最终从头开始无数次......

这就是我现在运行图像滤镜的方式,但它会导致滑块非常慢,这就是我决定尝试学习 Metal 的原因......但这确实非常耗时。由于缺乏文档而令人沮丧......

func ciExposure (inputImage: CIImage, inputEV: Double) -> CIImage {
    let filter = CIFilter(name: "CIExposureAdjust")!
    filter.setValue(inputImage, forKey: kCIInputImageKey)
    filter.setValue(inputEV, forKey: kCIInputEVKey)
    return filter.outputImage!
}
Run Code Online (Sandbox Code Playgroud)

我想我需要获取该filter.outputImage并以某种方式将其传递到MetalView?

任何帮助真的非常非常感谢......

sto*_*hel 1

好吧,这对我有用:

func draw(in view: MTKView) {
            guard let drawable = view.currentDrawable else {
                return
            }
            
            let colorSpace = CGColorSpaceCreateDeviceRGB()

            let commandBuffer = metalCommandQueue.makeCommandBuffer()
            
            let rpd = view.currentRenderPassDescriptor
            rpd?.colorAttachments[0].clearColor = MTLClearColorMake(0, 1, 0, 1)
            rpd?.colorAttachments[0].loadAction = .clear
            rpd?.colorAttachments[0].storeAction = .store
            
            let re = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd!)
            re?.endEncoding()
                
            context.render((AppState.shared.rawImage ?? AppState.shared.rawImageOriginal)!,
                to: drawable.texture,
                commandBuffer: commandBuffer,
                bounds: AppState.shared.rawImageOriginal!.extent,
                colorSpace: colorSpace)
            
            commandBuffer?.present(drawable)
            commandBuffer?.commit()
        }
Run Code Online (Sandbox Code Playgroud)

AppState.shared.rawImage 是我从过滤函数获得的 CIImage 纹理。

上下文是在其他地方创建的,但应该是:

context = CIContext(mtlDevice: metalDevice) 
Run Code Online (Sandbox Code Playgroud)

接下来是添加 Frank Schlegel 提供的代码的居中部分。