Ductane v1.6.0
Contents

Docs / Checking your game

Measuring performance

A frame rate read off an overlay tells you little, and the next reading can't be compared with it. The engine gives you the parts of a measurement you can trust: what each frame cost, split three ways, readouts a person can read, memory measured from inside the app, and a fixed camera route timed the same way every run.

One frame, three numbers

FrameRenderer.lastFrameTimes is a FrameTimes for the last frame drawn:

PropertyWhat it is
intervalmilliseconds from the previous frame shown to this one; 0 for the first
cpumilliseconds of CPU work: the game's steps and the frame's encoding, without the wait for a frame in flight
gpumilliseconds the GPU spent on the most recent frame it has finished; nil until one has

One number can't say what a frame cost. The interval is what a player sees, but the display paces it: at 120 Hz, a frame that needed 4 ms and one that needed 8 both read 8.3 ms. That answers "is it smooth", not "how much room is left". A frame's cost is the larger of its CPU and GPU time (FlythroughReport.cost(_:)), and the interval is what that came to on this display.

gpu runs a frame or two behind, since a frame in flight hasn't finished. It counts only this frame's share of the GPU's busy time: frames in flight overlap on the GPU, and timing each from start to end would count the overlap twice.

A frame rate a person can read

FrameRateReadout turns intervals into a reading a few times a second. Each Reading holds framesPerSecond, average and worst:

var readout = FrameRateReadout()     // smoothing: 0.05, framesPerReading: 10, longest: 250

// each frame, in beginFrame — lastFrameTimes is still the frame before
if readout.add(engine.lastFrameTimes.interval), let reading = readout.reading {
    fpsLabel = String(format: class="p">"%.0f fps  %.1f ms  worst %.1f",                // your game
                      reading.framesPerSecond, reading.average, reading.worst)
}

It makes three choices for you:

add(_:) skips the first frame's 0 and counts any frame longer than longest as longest: a window hidden for a minute reports one enormous gap.

Averages for a statistics panel

A statistics panel wants all three numbers over a stretch of frames. Add each frame to a FrameTimesAverage, read mean (a FrameTimes) and slowest when the panel refreshes, then reset(). It averages gpu only over the frames that reported one. WorldRenderer keeps one for you: takeFrameAverage() returns the mean and the slowest since the last call and starts a new stretch.

Memory, from inside the app

let memory = MemoryUse.measure(device: engine.device)   // cheap enough for a few times a second

On Apple silicon the CPU and GPU share one memory, so graphics is part of footprint, not added to it. MemoryUse.bytes(of:) adds up a list of allocations, each counted once, so a texture two materials share is one texture; WorldRenderer.graphicsMemory() uses it for the scene's textures and meshes. Shown beside the graphics settings, these numbers say what turning a setting down really saved.

A fixed route

Two readings from slightly different places, one windowed and one full screen, each look like a result. A Flythrough makes the route the constant: the same waypoints at the same speed every run, each frame recorded against the leg it was drawn on.

let route = Flythrough(waypoints: [SIMD2(0, 0), SIMD2(100, 0), SIMD2(100, 50)], speed: 10, eyeHeight: 2)
let loop = Flythrough.circuit(minimum: SIMD2(-200, -200), maximum: SIMD2(200, 200))

Waypoints are x and z in meters; the defaults are 18 m/s and 3.5 m up. circuit(minimum:maximum:) loops around a piece of ground a fifth of the way in from its edges, then crosses corner to corner. The camera follows the ground the whole way, not just at the waypoints: a straight line would pass through any hill between them, and frames drawn inside a hill cost nothing. It looks two meters ahead, so it pitches with the ground.

To fly it on screen, keep a FlythroughRun and tick it each frame:

var run = FlythroughRun(route: loop)

// each frame
let pose = run.pose(ground: { x, z in world.groundHeight(x: x, z: z) })
world.camera = WorldRenderer.View(eye: pose.eye, target: pose.target)
let times = engine.lastFrameTimes
run.record(times, seconds: Float(times.interval / 1000))
if run.isFinished { print(run.report.text(title: class="p">"Harbor", settings: settings.summary)) }   // your game

Each pass first holds the camera at the start for settleSeconds (0.75), so its first frames aren't the cost of whatever came before. record(_:seconds:) advances by the frame's own time, so every leg is flown at the same speed whatever the frame rate. FlythroughRun draws nothing, so it runs in swift test.

To fly it off screen, FrameRenderer.flyThrough(_:passes:width:height:framesPerSecond:ground:place:switchPass:) draws each frame into a texture and waits for it, so a hidden or covered window changes nothing. Each interval is then CPU and GPU time added together, not the display's pace, and the report says so.

The report

FlythroughReport.text(title:settings:) prints the whole route and then every leg. Pass GraphicsSettings.summary as settings, so the report says what it measured. The headline is the mean cost. With frames queued, the median describes the near-empty frames the CPU returns from while the queue has room, and only the mean is throughput. The 95th percentile and the worst sit beside it. Per leg is the point: a slow place shows up as a slow leg instead of being averaged into one number.

interval(pass:leg:), cost(pass:leg:), cpu(pass:leg:) and gpu(pass:leg:) each return a Summary with frames, mean, p95, worst and fps. For one fixed viewpoint, timeFrames(width:height:count:warmUp:) returns sorted times; read the median and the fastest.

What each thing costs

Give a run more than one pass to measure what each thing costs:

let report = engine.flyThrough(loop, passes: [class="p">"baseline", class="p">"no shadows"], width: 1920, height: 1080,
                               ground: { x, z in world.groundHeight(x: x, z: z) },
                               place: { world.camera = WorldRenderer.View(eye: $0.eye, target: $0.target) },
                               switchPass: { world.castsShadows = $0 != class="p">"no shadows" })

The route is flown once per pass, with that one thing off. switchPass is told each pass's name as it begins, and nil at the end to put everything back. The run adds FlythroughRun.baselineAgain as a last pass, because a machine warms over minutes of flying, and a warm GPU is slower: a suspect measured last can look as if it costs less than nothing. baseline(forPass:leg:) joins the two baselines with a straight line, saving(pass:leg:) compares each pass with the baseline of its own time, and drift says how far the baseline moved. A saving smaller than the drift is noise. The table is per leg too, so a cost that lives in one corner shows up as one expensive leg.

What it does not do

None of these draw anything; a readout is yours to put in the HUD. The GPU time is one number a frame, not a breakdown by pass. Switching things off is how the engine attributes cost; for a GPU breakdown, use Instruments or Xcode's GPU capture.