Rubylight Download 7.0.3
On this page
  1. Turning it on
  2. How it works
  3. Frame Warp 2
  4. Accuracy limits
  5. Measuring
  6. Wire formats
  7. Where the code is

Frame Warp #

Rubylight docs › Picture and smoothness

Frame Warp makes a camera turn show on the next refresh of the client's screen instead of a round trip and the game's own delay later. Nightjar shifts each picture by the turn that its own mouse, right-stick or gyro input has not reached the picture with yet. Rubylight learns, for the running game, how far the picture moves per mouse count and per second of stick, how long the game takes to show an input and, for a game drawn through a perspective camera, its field of view, and tags every frame with how much input it reflects. What stays put on screen while the camera turns (crosshair, health, minimap, the weapon) is found by the host and left unshifted.

On a test game the first Frame Warp removed nearly all of the picture's trail behind the mouse, also at the edges of a wide field of view, for a fraction of a millisecond of the host's GPU time a frame (Measuring). Frame Warp 2, which replaced it in 6.5, has not been measured that way yet.

Turning it on #

It is a setting in Nightjar (Video, Frame Warp), off by default, and needs a host that lists it (FrameWarpProtocol in serverinfo). The host's frame_warp setting (All settings › Picture › Frame pacing › Let devices use Frame Warp, on by default) lets clients use it; the host does nothing for Frame Warp until a client asks.

What a stream learned is kept for the next one: the host saves each game's gains, lags, stick curve, field of view and gyro gain in frame_warp.json in its configuration directory, by the app's uuid or else its name, for the 256 games streamed most recently. A later stream of that game applies the lags at once and locks the gains as soon as recent input confirms them. A file from before 6.5 is read and rewritten in the new format; a file that cannot be read is set aside as frame_warp.json.corrupt and the game is learned afresh.

How it works #

  1. The client asks for Frame Warp once the stream runs.
  2. The host records the input it receives.
  3. It measures how far each captured picture moved.
  4. It fits that motion to the input: a gain per axis and the game's lag.
  5. It learns the game's field of view from how the picture moves in a turn.
  6. It tags every frame with the input it reflects, and sends the model when it changes.
  7. It finds the HUD and sends the client a mask of it.
  8. The client shifts each picture by the input the picture does not show yet.

The request #

The client sends a request (0x5543) once the stream runs: on, the controller number whose right stick it tracks, the widest HUD mask it takes, and the pad whose gyro turns the camera.

Input history #

The host keeps the running sums of the relative mouse motion it received, and for the tracked pad the count of controller packets it received and their right stick, with the time each arrived.

Measuring motion #

For each capture the GPU makes a 256-pixel-wide luma thumbnail; the CPU measures how far the content moved from the previous one (rust/core/src/frame_warp/motion.rs): row and column profiles of a 3×3 grid of cells are correlated, refined to a tenth of a pixel by gradient steps, and the median cell wins.

The HUD found so far (the HUD mask) is left out of the profiles, since a wide static bar would pull every vertical shift toward zero, and a picture whose textured cells are mostly unchanged counts as standing still whatever moves in a corner of it. That mask is the one the client holds still, so it leaves out a HUD's rim and its plain parts, and a client may ask for none. The measurement therefore also finds what stands still itself, per thumbnail pixel: each move should bring a pixel what its cell's shift says lay that far away, and a pixel that should have changed by 24 luma codes in all (at most 6 from one move) and stayed within 2 is left out with its neighbours until it changes. A sky, a horizon during a pan or the middle of a plain panel, which a move brings more of the same, is not marked.

A profile takes the moving average out of every picture line before it averages the lines, and drops the samples whose average would reach over a left-out pixel: otherwise rows half under a HUD stand at another level than their neighbours, and that step, fixed on the screen, holds the cell's shift back as the HUD itself would.

Gain and lag #

The calibrator (calibrate.rs) fits the moves to the input over the last four seconds: for every lag from 0 to 200 ms, a least-squares gain per axis, and the lag that explains the most motion wins. An axis with less than a quarter of the other's input takes the other's gain: fitted to the slight drift of a sideways turn, it would mostly fit whatever else moves the picture that way, a ship's roll.

It locks at 80% explained and unlocks when the last 1.2 s of input with the locked model explain less than 50% (a menu, a cutscene, a cursor). While locked, a new fit replaces the model only when it explains at least 3 points more of the motion, or a quarter of what the model leaves unexplained when that is less, so the lag doesn't wander from fit to fit; otherwise the model keeps its lag and takes the least-squares gain there, which settles as turns add up after an early lock.

Field of view #

A game draws through a perspective camera, so a turn moves the edges of its picture farther than the centre, twice as far at the left and right of a 90 degree field of view. The host learns the field of view from the cells (lens.rs): for each move made while the player's turning was arriving, and for each field of view from 30 to 120 degrees in steps of 2, it fits the cells' shifts with a camera turn, a zoom about the centre (walking forward) and a roll, leaving out up to two cells where something else moves, and keeps the squared remainder, fading over the last few seconds of turning.

Each shift counts where its profile measured it rather than at the cell's centre: a profile follows its slopes, a lens stretches the picture toward its edge, so an outer cell's shift is that of a place nearer the middle (enough to read 90 degrees as 78), and a cell half under a HUD is measured beside it. No cell costs a field of view more than a cell left out does, so a cut or a menu opening costs them all alike.

Once 30 moves have added up, the field of view that leaves the least (between the steps, where a parabola through the best and its neighbours is lowest) is taken if it leaves at most 75% of what a flat picture leaves, changed for one at least 1.5 degrees away that leaves at most 98% of what it leaves, and dropped when it leaves more than 90% of the flat picture's remainder.

With a lens, each move is read as the turn at the picture's centre from that fit, not the median cell, so the gains are the centre's; a map or a side-scroller, which moves alike everywhere, gets no lens and stays as before. A client whose request does not say it draws the lens (Nightjar 5.0) is sent no focal length, and the centre's gains all the same: the median cell's, longer, would carry the middle of its picture too far.

Tags and model #

Each frame, as its number is assigned, is tagged with the mouse sums and the pad packet count as of its capture time less the game's lag, and how long that packet had been held (0x5545, with the three previous frames' tags, unreliable).

The model (0x5544, reliable) goes out when it locks, unlocks, a gain changes by more than 2%, the lag by more than 3 ms or the lens's focal length by more than 3%.

The HUD mask #

While the camera turns, the GPU compares every pixel with the same pixel in an earlier capture, within 2/255 per channel, since games add faint noise or dither to their HUD. The earlier capture is one the scene has moved at least a thumbnail pixel since: the previous one in a fast turn, a few back in a slow turn at a high frame rate, which would otherwise move too little between captures for a textured scene to change. A pixel whose value changed still counts as staying when it differs from its right and lower neighbours as it did: the edge of a translucent HUD, grey text over the sea, keeps its place and its contrast while its value follows the scene behind it.

A mask cell gains a point when its textured pixels (those that differ from a neighbour by more than the tolerance) mostly stayed, and loses half when they or a quarter of all its pixels changed. A smooth cell, a sky or fog, looks the same moved or not and keeps its score; with the tolerance it would otherwise become HUD and hide whatever crosses it. A cell that held through about 24 comparisons is HUD. The host joins HUD pieces up to 3% of the picture's height apart into one region, so text found letter by letter stays in place as a whole, and grows the mask by a cell for anti-aliased edges.

The mask is sent at the client's width or less, at most once a second when it changes (0x5546, reliable, run-length coded). The client lays it over the whole picture: a capture of another shape than the stream, letterboxed into it, has its grid placed where it shows, and the bars count as HUD, so they hold still and nothing is shifted in from them. Thumbnails and gains likewise cover only the capture's part of the picture.

Drawing the shift #

The client keeps its own sums of what it sent. For each picture it draws, as late as it can, the shift is

shift = mouse_gain * (mouse_sent - tag.mouse)
      + stick_gain * integral of curve(stick sent) from tau to now

where tau is the send time of the tagged pad packet plus how long the host had held it, clamped to 15% of the picture (less under a lens wider than 90 degrees, whose edges stretch sooner).

The client samples the picture at position - shift, or, when the model has a lens, where a camera turned by shift / focal shows that position: the centre moves by focal * tan(shift / focal) and the edges by more, as the game itself would draw the turn (the mapping). It samples at position where the HUD mask is set or where the source lands on it, so the HUD stays in place without a second copy beside it. The strip the shift uncovers mirrors the picture back from its edge, darkened by up to 35% over the first 8% of the picture, so it reads as an edge rather than a smear of the outermost pixels.

The same shift is drawn again on refreshes between pictures, so a 60 fps stream turns smoothly on a 120 Hz screen.

Frame Warp 2 #

Frame Warp 2 replaces the first Frame Warp, and it needs the current Nightjar and host together. A host from before 6.5 lists FrameWarpVersion instead of FrameWarpProtocol: Nightjar sends it nothing and says that Frame Warp needs the current version on the PC. An older Nightjar sees no Frame Warp on a 6.5 host, and the host ignores the older request if one comes. Either way the stream runs normally without Frame Warp, and the frame_warp settings on both sides keep their values.

Messages: the request (0x5543, 12 bytes) and the meta (0x5549, unreliable, one per frame); the protocol has their formats.

Accuracy limits #

Measuring #

rust/probes/src/look_probe.rs is a fake mouse-look game whose response is known exactly. Run it on a display an isolated test host captures (--injected-only, so only the host's moves turn it), and on the same PC stream that display at its own size with nightjar-cli, setting NIGHTJAR_CLI_FRAME_WARP=<csv> (ask for Frame Warp and log the shift each decoded picture gets) and NIGHTJAR_CLI_LOOK=<seed> (a mouse-look pattern of turns and pauses). The probe's barcode ties each logged picture to the input it shows, and rust/release/frame_warp_accuracy.py --probe OUT.json --client <csv> compares each shift with the ideal one: the learned gain and lag, how soon the model locks, and how much of the input-to-picture displacement Frame Warp removes. With --fov DEGREES the probe draws its world through a perspective camera, and the script also reports the learned field of view and what is left at the centre, over the whole picture and at its edge (a 7 by 7 grid of points, each compared with where the turned camera shows it).

The results of these runs with the first Frame Warp, how much of the displacement it removed at the centre and the edge, the learned fields of view and the GPU cost, are in the measurement record: Frame Warp and the lens.

Wire formats #

client/protocol/core/src/frame_warp.rs defines every message; control messages lists the ids, and Nightjar's protocol document gives the full rules for other hosts. Little-endian throughout.

Message Bytes
Request 0x5543 version 2, enabled 0/1, controller 0-15 or 0xFF, flags (bit 0: the client draws the lens, bit 1: the gyro turns the camera), mask max width u16, 0, 0, gyro controller, gyro axes, max offset u16
Model 0x5544 version, flags (bit 0 mouse locked, bit 1 stick locked), generation u16, picture width u16, height u16, then f32: mouse gain x, y (picture px per count), stick gain x, y (px per second of full deflection after the curve), stick dead zone, stick exponent (1 to 3 in halves), max shift x, y; lag µs u32; fit f32; focal length f32 (picture px, 0 for none; absent from hosts before it was added, and clients from before it ignore it)
Tags 0x5545 version, count 1-4, model generation u16, then per tag: frame u32, mouse sum x i32, y i32, pad packet count u32, held µs u32 (0xFFFFFFFF none)
Mask 0x5546 version, kind (0 none, 1 runs), generation u16, width u16, height u16, then per row LEB128 run lengths alternating clear and set, starting clear
Meta 0x5549 version 2, flags, model generation u16, frame u32, camera confidence u16, HUD stability u16, gyro seq u32, gyro held µs u32, gyro gain f32 x 2, omega mrad/s i32 x 2, edge fade u8, HUD guard u8, gyro tag u16

Where the code is #

Part Code
Wire formats, stick curve client/protocol/core/src/frame_warp.rs, frame_warp_v2.rs
Host history, motion, lens, calibration, gyro, confidence, tags, mask choice rust/core/media-policy/src/frame_warp/
Host GPU thumbnail and HUD passes rust/windows/graphics/src/compute_frame_warp.rs, rust/windows/abi/src/shaders/color.hlsl
Host wiring, setting and the per-game memory rust/host/stream/src/stream/frame_warp.rs, frame_warp_prior.rs, rust/core/config/settings.toml (frame_warp)
Client counting and shift client/protocol/client/src/frame_warp.rs, frame_warp_v2.rs, input/queue.rs, control/session.rs
Desktop presenters client/protocol/desktop-media/src/stream/ (frame_warp.rs, frame_warp_v2.rs)
Android renderers client/app/src/main/jni/vulkan/, GlesUpscaler.java, pyrowave-renderer/

View this page on GitHub