gpt4 book ai didi

SwiftUI 更新降低了 Metal 窗的 FPS

转载 作者:行者123 更新时间:2023-12-01 03:04:11 28 4
gpt4 key购买 nike

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

Windows

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

似乎 ListView 也会减慢 FPS。

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

    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

控制窗口是一个 SwiftUI View :
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)
}
}

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

我删除了 Metal 窗口并使用 NSViewRepresentable 将其合并到控制窗口中,所以现在看起来像这样:
All in one window

这仍然有同样的问题。

渲染代码(没做太多!)。即使不渲染茶壶,它仍然会变慢。
    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()
}
}

最佳答案

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

关于SwiftUI 更新降低了 Metal 窗的 FPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59212294/

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