Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show current frame rate of a MTKView

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)

like image 464
jackxujh Avatar asked Mar 21 '19 21:03

jackxujh


Video Answer


1 Answers

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)
    }
}
like image 199
CloakedEddy Avatar answered Oct 21 '22 20:10

CloakedEddy