9

I know that in SceneKit, you can enable a banner on the side of the SKView to look at real time frame rates and other useful debugging information. But what about MTKView? I don't seem to find such a property to enable, or how I can query the current frame rate. (Because I am rendering something that have a frame rate of 0.5fps or so)

jackxujh
  • 983
  • 10
  • 31

1 Answers1

1

I don't think there is simple flag for you. Because you control the complete rendering pipeline when creating a command buffer, Metal can't know where to inject a rendering pass with some custom text.

You could inject your own rendering pass (based on a flag like var showDebugInformation = true) in your pipeline, but that sounds like a bit of work.

I would probably monitor frame times manually in the draw method and update a label every draw. A rough outline could look like this:

var previousFrameAtTime: Date
let lastFrameTime = CurrentValueSubject<TimeInterval, Never>(.infinity)

func draw(in view: MTKView) {

    lastFrameTime.send(Date().timeIntervalSince(previousFrameAtTime))
    previousFrameAtTime = Date()

    // ...
}

Then you can observe this value in your view, something like this:

import Combine

class MyViewController: UIViewController {

    let label = UILabel()
    var cancellables: [AnyCancellable] = []

    func subscribeToFrameTime() {
        renderer.lastFrameTime
            .sink { label.text = "\($0 * 1000) ms." }
            .store(in: &cancellables)
    }
}
CloakedEddy
  • 1,965
  • 15
  • 27