SwiftUI 更新降低了金属窗口的 F​​PS

Lei*_*igh 8 swift metal swiftui

我正在试验 SwiftUI 和 Metal。我有 2 个窗口,一个带有各种列表和控件,另一个带有金属窗口。

视窗

我让滑块数据更新了 Metal 窗口,但是当我移动滑块时,FPS 从 60 下降到 25 左右。我删除了视图之间的所有链接,移动滑块仍然会降低金属窗口中的 FPS。

似乎列表视图也会减慢 FPS。

我在启动时使用以下方法创建金属窗口:

    metalWindow = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
                           styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                           backing: .buffered, defer: false)
    metalWindow.center()
    metalWindow.setFrameAutosaveName("Metal Window")
    metalWindow.makeKeyAndOrderFront(nil)

    mtkView = MTKView()
    mtkView.translatesAutoresizingMaskIntoConstraints = false
    metalWindow.contentView!.addSubview(mtkView)
    metalWindow.contentView!.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|[mtkView]|", options: [], metrics: nil, views: ["mtkView" : mtkView!]))
    metalWindow.contentView!.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[mtkView]|", options: [], metrics: nil, views: ["mtkView" : mtkView!]))

    device = MTLCreateSystemDefaultDevice()!
    mtkView.device = device

    mtkView.colorPixelFormat = .bgra8Unorm

    commandQueue = device.makeCommandQueue()!

    mtkView.delegate    = self
    mtkView.sampleCount = 4
    mtkView.clearColor  = MTLClearColor(red: 0.0, green: 0.5, blue: 1.0, alpha: 1.0)
    mtkView.depthStencilPixelFormat = .depth32Float
Run Code Online (Sandbox Code Playgroud)

控制窗口是一个 SwiftUI 视图:

struct ControlPanelView: View {

    @ObservedObject var controlPanel = ControlPanel()

    @State private var cameraPos   = Floatx3()
    @State private var lightingPos = Floatx3()

    var body: some View {

        HStack{
            VStack{
                VStack{
                    Text("Objects")
                    List(self.controlPanel.objectFiles) { object in
                        Text(object.name)
                    }
                }
                VStack{
                    Text("Textures")
                    List(self.controlPanel.textureFiles) { texture in
                        HStack {
                            Image(nsImage: texture.image).resizable()
                                .frame(width: 32, height: 32)
                            Text(texture.name)
                        }
                    }
                }
            }
            VStack{
                HStack{
                   XYZControl(heading: "Camera Controls",   xyzPos: $cameraPos)
                   XYZControl(heading: "Lighting Controls", xyzPos: $lightingPos)
                }.padding()
                HStack{
                    Text("Frames Per Second:")
                    //Text(String(renderer.finalFpsCount))
                }
            }
        }.border(Color.red).padding()
    }
}

struct XYZControl: View {

    var heading : String
    @Binding var xyzPos : Floatx3

    var body: some View {

        VStack{
            Text(heading).padding(.bottom, 5.0)
            PositionSlider(sliderValue: $xyzPos.x, label: "X", minimum: -15.0, maximum: 15.0)
            PositionSlider(sliderValue: $xyzPos.y, label: "Y", minimum: -15.0, maximum: 15.0)
            PositionSlider(sliderValue: $xyzPos.z, label: "Z", minimum: -15.0, maximum: 15.0)
        }.border(Color.yellow).padding(.leading)
    }
}

struct PositionSlider: View {

    @Binding var sliderValue : Float
    var label   : String
    var minimum : Float
    var maximum : Float

    static let posFormatter: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.maximumFractionDigits = 3
        return formatter
    }()

    var body: some View {
        VStack{
            Text(label)
            HStack{
                Text("\(Self.posFormatter.string(from:NSNumber(value: minimum))!)")
                Slider(value: $sliderValue, in: minimum ... maximum)
                Text("\(Self.posFormatter.string(from:NSNumber(value: maximum))!)")
            }.padding(.horizontal).frame(width: 150.0, height: 15.0, alignment: .leading)
            Text("\(Self.posFormatter.string(from:NSNumber(value: sliderValue))!)")
        }.border(Color.white)
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助解释为什么帧速率下降?

我移除了金属窗口并使用 NSViewRepresentable 将其合并到控制窗口中,因此现在看起来像这样: 一站式服务

这仍然有同样的问题。

渲染代码(没有做太多!)。即使不渲染茶壶,它仍然会变慢。

    func draw(in view: MTKView) {

        if let commandBuffer = commandQueue.makeCommandBuffer() {

            commandBuffer.label = "Frame command buffer"

            if let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: view.currentRenderPassDescriptor!) {

                renderEncoder.label = "render encoder"

                renderEncoder.endEncoding()
            }

            commandBuffer.present(view.currentDrawable!)

            commandBuffer.addCompletedHandler { completedCommandBuffer in
                self.computeFPS()
            }
            commandBuffer.commit()
        }
    }
Run Code Online (Sandbox Code Playgroud)

Lei*_*igh 3

正如 0xBFE1A8 所说,SwiftUI 阻塞了主线程。我使用 CVDisplayLink .global(qos: .userInteractive) 将绘制函数移至回调设置中。当滑块移动时,这会阻止渲染速度减慢。