Skip to content

Latest commit

 

History

History
185 lines (152 loc) · 12 KB

api-c.md

File metadata and controls

185 lines (152 loc) · 12 KB

C API

crates/flyhigh-capi/ builds flyhigh.dll / libflyhigh.so / libflyhigh.dylib and a static library; the header is include/flyhigh.h. C++, C#, Python and other FFI hosts call the same functions (P/Invoke, ctypes). The engine's layers are opaque handles: FhEngine (the device), FhModel, FhSession (two frames in, one frame at phase t out), FhStream (frames with timestamps in, intervals out), FhPacer (what to present on this tick, what to generate) and FhGenerator (the CPU convenience: source frames in, the frames to display out, in order). Frames cross the ABI as RGBA8 bytes (width * height * 4); GPU frames come in through the interop calls (fh_session_import_d3d11, fh_stream_import_d3d11; Vulkan interop from the host's own Vulkan device). Single-threaded use per handle; a failed call returns NULL or -1 and fh_last_error() has the message. Options always start from the fh_*_options_default() struct. All options and defaults: options.md.

Core

#include "flyhigh.h"

FhEngineOptions eo = fh_engine_options_default();
FhEngine* engine = fh_engine_create(&eo);                       // the device, once; NULL = defaults
FhModel* model = fh_model_load("models");                       // graph.json, flyhighfg.json, flyhighfg.bin
FhSessionOptions so = fh_session_options_default(1920, 1080);   // internal auto, k 1, repair 1, the model's sampler
FhSession* s = fh_session_create(engine, model, &so);           // the plans for one size

int a = fh_session_add_input(s), b = fh_session_add_input(s), out = fh_session_add_output(s);
fh_session_write_rgba(s, a, frame0, 1920 * 1080 * 4);
fh_session_write_rgba(s, b, frame1, 1920 * 1080 * 4);
fh_session_pair(s, a, b);                                       // the motion of the interval: the expensive half
fh_session_mid(s, 0.5f, out);                                   // phase 0.5 into the output slot: the cheap half
fh_session_read_rgba(s, out, rgba, 1920 * 1080 * 4);           // waits for the GPU
  • fh_engine_create(&FhEngineOptions): backend, tensor cores, autotune, adapter selector, precision, tune cache, compact memory, DXC path, as EngineOptions. fh_engine_info writes the device as JSON (name, kernel class tc-f16 / portable-f16 / portable-f32, timestamp queries, and on DX12 shader_compiler: dxc, or fxc when dxcompiler.dll was not found); fh_engine_report_json adds kernels compiled, autotune entries and tensor bytes, with live and peak allocation bytes in memory. Destroy sessions and streams before the engine.
  • fh_model_load(dir) / fh_model_from_bytes(graph, len, manifest, len, weights, len) load and check a checkpoint (format version, tensor table); fh_model_info is the model as JSON. Several models may live beside one engine.
  • FhSessionOptions: width, height, internal_w / internal_h (0, 0 = automatic: the 480-line rung above 960 px), k, repair, render (0 model, 1 bilinear, 2 bicubic), keep_static with static_frames / static_tolerance, fast_render, linear_t, keep_maps, protection_mask (the mask comes through fh_session_write_mask / fh_stream_write_mask, one byte per pixel), tune (the autotune table text). An option out of range fails fh_session_create with the field named in fh_last_error.
  • fh_session_pair_timed / fh_session_mid_timed give GPU milliseconds from timestamp queries (1 measured, 0 none on this device). fh_session_warm_up compiles every kernel before the first real frame. fh_session_generate is pair and mid in one call. fh_session_report_json: sizes, plan steps, slots.

Pacer recording

fh_pacer_record(pacer, "my host") makes the pacer record every call from then on; fh_pacer_trace_json writes the trace v1 so far, the file flyhigh-dev replay takes and the one to attach to a bug report.

Stream

FhStreamOptions sto = fh_stream_options_default();              // realtime, scene cuts at 0.08, 4 + 4 slots
FhStream* st = fh_stream_create(engine, model, &so, &sto);
// per source frame, with its timestamp (media or capture time, milliseconds):
int slot = fh_stream_next_input(st, still_shown, n_still_shown);
fh_stream_write_rgba(st, engine, slot, frame, len);
FhClassification c; int kind = fh_stream_classify(st, engine, previous, slot, &c) == 1 ? c.kind : 0;   // motion / cut / hold
FhPush p; fh_stream_push(st, engine, slot, pts_ms, &p);          // runs the pair; p.ready = an interval can be generated
if (p.stats_pending) fh_stream_resolve_stats(st, engine);      // video + cadence
if (fh_stream_generate(st, engine, 0.5f, out) == 1) present_output(out); else present_source(p.base_slot);

FhStreamOptions: mode (0 realtime, 1 video with one frame of lookahead), cadence, scene_cut (the threshold; 0 = never), held_frames with held_mean / held_max, inputs / outputs created up front. fh_stream_generate returns 1 written, 0 the source frame is enough (t = 0 without re-timing). fh_stream_read_rgba, fh_stream_warm_up, fh_stream_reset (seek, decoder gap), fh_stream_report_json. Details: ARCHITECTURE.md.

Camera warp (games): fh_stream_mouse(st, engine, t_ms, dx, dy) per mouse event, on the clock of pts (arrival times in host ms), fh_stream_set_mouse_captured(st, 0 / 1), and at present time fh_stream_warp(st, engine, output, slot, out, now_ms): the frame you would present (input slot, or output slot with output 1) turned by the mouse motion since, into output slot out; 1 = present out, 0 = present the frame as it is (integration guide).

Vulkan interop

For a host with its own Vulkan device (an emulator, a game, a capture on Vulkan) on Windows and Linux: frames cross on the GPU through memory and semaphores the host exports, with no copy through the CPU. The engine stays on its own device (its own queue, its tensor-core kernels); the two devices must be on the same GPU and driver.

uint8_t uuid[16];
if (fh_engine_vulkan_interop(engine, uuid) != 1) { /* the engine's device cannot import: another path */ }
/* compare uuid with the host's VkPhysicalDeviceIDProperties::deviceUUID; pick the engine's GPU with the
   adapter option ("10de:2803") when there are several */

uint32_t pitch = fh_vk_row_pitch(width);                  /* bytes per row, a multiple of 256 */
FhVkMemory m = { handle, allocation_size, memory_type_index };   /* the host's exported allocation */
FhVkBuffer* in = fh_vk_buffer_import(engine, &m, (uint64_t)pitch * height);
FhVkSemaphore* ready = fh_vk_semaphore_import(engine, ready_handle, 1);   /* 1 = timeline */

/* host: copy the frame into the buffer (RGBA8 rows of pitch bytes), signal `ready` to v on its queue */
fh_stream_copy_in(stream, engine, slot, in, pitch, ready, v);   /* the engine's copy waits for v */
fh_stream_push(stream, engine, slot, pts, &push);
fh_stream_generate(stream, engine, 0.5f, out);
fh_stream_copy_out(stream, engine, out, out_buffer, pitch, done, w);   /* signals `done` to w after it */
/* host: its copy out of out_buffer waits for `done` to reach w */
  • The host allocates each buffer as a dedicated allocation (VkMemoryDedicatedAllocateInfo) for a buffer with TRANSFER_SRC | TRANSFER_DST usage, exported as OPAQUE_WIN32 (an NT handle) on Windows or OPAQUE_FD on Linux, and passes its allocation size and memory type index: Vulkan requires the import to repeat them. Its device needs VK_KHR_external_memory_win32 / _fd and VK_KHR_external_semaphore_win32 / _fd.
  • Buffers rather than images: an image's layout and compression belong to the device that made it; a buffer is the same bytes on both. The copies cost about 0.05 ms per 1080p frame each.
  • A Windows handle stays the host's (close it after the import); a file descriptor belongs to the engine once the import succeeds.
  • Semaphores: a timeline semaphore takes the value to wait for or signal (above 0), and the engine may wait before the host has submitted the signal. A binary one takes 0, and its signal must be submitted before the engine's wait. One semaphore per direction keeps each signalled by one queue only, so the values never have to be ordered across the two queues.
  • fh_vk_signal signals with nothing copied. fh_vk_semaphore_destroy waits for the engine's queue first, so a pending wait or signal never outlives the semaphore. fh_session_copy_in / fh_session_copy_out do the same for a session's slots.
  • Rust: vulkan_interop::{supported, device_uuid, import_buffer, import_semaphore, copy_in, copy_out, signal}.

Pacer (fh_pacer_*)

The engine's scheduler for a host with its own display loop (ARCHITECTURE.md): pure logic, no clock, every call takes now in the host's monotonic milliseconds.

FhPacerOptions o = fh_pacer_options_default();          // mode 0 auto; 1 factor, 2 display rate, 3 target_fps
o.min_present_interval_ms = 0;                           // the host's present spacing limit, if it has one
FhPacer* pacer = fh_pacer_create(&o);

// per display tick (vsync):
FhTick tick; fh_pacer_tick(pacer, now_ms, &tick);
if (tick.has_present) present(tick.frame_kind == 0 ? source_slot(tick.frame_id) : output_slot(tick.frame_id));
FhRequest req[64]; int n = fh_pacer_requests(pacer, req, 64);
for (int i = 0; i < n; i++) {
    int out = next_output();
    if (fh_stream_generate(st, engine, req[i].t, out) == 1) fh_pacer_generated(pacer, now_ms, req[i].id, out);
    else fh_pacer_skipped(pacer, req[i].id);
}

// per source frame, once its kind (motion / cut / hold) is known:
FhSourceFrame f = { slot, pts_ms, kind, 1, motion, 0 };
n = fh_pacer_source_frame(pacer, now_ms, &f);           // requests of the previous pair: generate them first
fh_pacer_requests(pacer, req, 64); /* generate, report */ fh_stream_push(st, engine, slot, pts_ms, &p);

fh_pacer_cost_sample feeds measured GPU times (stage 0 pair, 1 mid, 2 present; 1 taken, 0 ignored inside the settle window), fh_pacer_mode_change opens the settle window after a fullscreen change, fh_pacer_reset forgets the history on a seek, fh_pacer_report_json writes the report (what was decided, measured and dropped, and why). fh_pacer_set_options changes options live. The C surface decides exactly what the Rust one does: crates/flyhigh-capi/tests/pacer_parity.rs replays a recorded host trace through both and compares the reports.

Generator: the CPU convenience

FhOptions go = fh_options_default(1920, 1080);
FhGenerator* g = fh_generator_create(engine, model, &go);
for (each source frame) {
    int n = fh_generator_push(g, rgba, 1920 * 1080 * 4, 2);    // factor 2 = one in-between per source frame
    float phase; int real; uint8_t out[1920 * 1080 * 4];
    while (fh_generator_pop(g, out, sizeof out, &phase, &real) == 1) present(out, phase, real);
}
fh_generator_destroy(g);

FhOptions mirrors the session options plus mode (0 realtime, 1 video) and cadence; push queues the frames to display in order (realtime: the in-betweens of (previous, this), then this frame; video: the interval two frames back after the third push, fh_generator_warmup says how many). It uploads and reads back every frame; GPU hosts use the Core and the Stream instead.

Zero-filled structs

A zero-filled FhSessionOptions or FhOptions is valid but turns repair off (its default is 1) and asks for k 0 (FhOptions: the default 1; FhSessionOptions: an error, k must be 1 to 3). Start from the defaults.

Bindings

bindings/python/flyhigh.py (ctypes, standard library only) and bindings/cpp/flyhigh.hpp (header only, RAII) wrap this ABI one to one; examples/c/ is a C host with a CMakeLists.txt. See bindings/README.md.

Tests

crates/flyhigh-capi/tests/: defaults.rs and core.rs check that the C defaults match the Rust ones and that every option struct maps both ways without loss (no GPU); core.rs also generates an in-between through the Core and the Stream from C on a GPU (cargo test -p flyhigh-capi -- --ignored); pacer_parity.rs replays a recorded host trace through the C Pacer and the Rust one.