Headless rendering and video out¶
Pictures and videos of avatars on a server, in a bot, in a build step or in a container: no window, no display
server, no GPU needed. The library makes its own device, picks the best one it can find and falls back to a
software rasteriser. One call turns an avatar or a manifest into RGBA pixels; batches make thousands of gamerpics
or cards with flat memory; sequences make turntables and clips at a fixed frame rate; a video writer turns frames
into a PNG sequence, an animated GIF, or MP4 and WebM through ffmpeg. The sample is samples/c/headless_bot.c.

sona_headless *h;
/* best GPU, else lavapipe or llvmpipe */
sona_headless_open(ctx, NULL, &h);
sona_render_options ro;
/* 256 x 256, full body, transparent */
sona_render_defaults(&ro);
ro.preset = SONA_PRESET_BUST;
uint8_t *rgba = malloc(256 * 256 * 4);
/* waits for the avatar, draws it, removes it */
sona_headless_render_manifest(h, manifest, &ro, rgba);
sona_headless *h = nullptr;
// best GPU, else lavapipe or llvmpipe
sona_headless_open(ctx.raw(), nullptr, &h);
sona_render_options ro;
// 256 x 256, full body, transparent
sona_render_defaults(&ro);
ro.preset = SONA_PRESET_BUST;
std::vector<uint8_t> rgba(256 * 256 * 4);
// waits for the avatar, draws it, removes it
sona_headless_render_manifest(h, manifest, &ro, rgba.data());
// best GPU, else lavapipe or llvmpipe
using var h = new SonaHeadless(ctx);
// 256 x 256, transparent
var rgba = h.RenderManifest(manifest,
new SonaRenderOptions { Preset = SonaPreset.Bust });
// best GPU, else lavapipe or llvmpipe
let h = Headless::open(ctx, &HeadlessOptions::default())?;
// 256 x 256, transparent
let o = RenderOptions { preset: sona_sys::SONA_PRESET_BUST,
..Default::default() };
// waits for the avatar, draws it, removes it
let rgba = h.render_manifest(&manifest, &o)?;
# best GPU, else lavapipe or llvmpipe
h = sona.Headless(ctx)
# 256 x 256, transparent; waits for the avatar, draws it, removes it
rgba = h.render_manifest(manifest, preset="bust")
The device¶
sona_headless_open makes a device of the library's own and a renderer on it. Nothing is linked at build time:
the Vulkan loader (libvulkan.so.1, vulkan-1.dll), EGL (libEGL.so.1) and Direct3D 11 (d3d11.dll, dxgi.dll)
are loaded when the device is made, as the HTTP layer loads libcurl. Metal is a system framework on Apple.
device |
What it opens |
|---|---|
SONA_DEVICE_AUTO (0, default) |
A hardware device on any API first (Vulkan: discrete, then integrated, then virtual; then a GL DRM device; then each Direct3D 11 adapter on Windows, or Metal on Apple), and only then a software one (Vulkan lavapipe, then GL llvmpipe, then WARP on Windows) |
SONA_DEVICE_GPU |
Hardware only. SONA_E_UNSUPPORTED when there is none |
SONA_DEVICE_SOFTWARE |
A CPU rasteriser only: the same pixels on every machine with the same Mesa (or the same Windows), whatever GPU it has |
backend 0 tries Vulkan, then GL 3.3 core on an EGL device display (GLES 3.0 on Android), then Direct3D 11
(Windows) or Metal (macOS, iOS); SONA_BACKEND_VULKAN or SONA_BACKEND_GL keeps to one. Any other backend is
SONA_E_ARGUMENT, Direct3D 11 and Metal included: keep backend 0 and choose the device with adapter, its
index in sona_headless_devices. That call lists every device the renderer can open: Vulkan devices in the loader's
order, then EGL devices, then the Direct3D 11 hardware adapters in DXGI's order and WARP last, then the Metal devices
(the system default first). Each entry's backend says which API it is, so a host that wants Direct3D 11 on its
second GPU opens the index of that entry. SONA_HEADLESS_VALIDATION
turns on VK_LAYER_KHRONOS_validation where it is installed, its messages going to the ctx's log. When the device has
shaderSampledImageArrayDynamicIndexing (and multiDrawIndirect with drawIndirectFirstInstance), they are enabled
and handed to the renderer (sona_renderer_vk_features): the same pixels, less CPU a frame.
When no device fits, the error says what was tried, for example "No headless device: Vulkan: no CPU device of 1; GL: no software EGL device." To run in a container, install one software driver:
| Distribution | Vulkan (lavapipe) | GL (llvmpipe) |
|---|---|---|
| Debian, Ubuntu | libvulkan1 mesa-vulkan-drivers |
libegl1 libegl-mesa0 libgl1-mesa-dri |
| Arch | vulkan-icd-loader vulkan-swrast |
mesa |
| Fedora | vulkan-loader mesa-vulkan-drivers |
mesa-libEGL mesa-dri-drivers |
On Windows a GPU is opened through Vulkan when its driver ships the loader, else through Direct3D 11, and the software
device is WARP, which every Windows 10 and 11 has: a server or a CI runner with no GPU renders with no install.
An opened WARP device's sona_headless_info carries the name Windows gives it ("Microsoft Basic Render Driver"), and
the device list calls it "WARP (Basic Render Driver)"; software is 1 and type 4. A Direct3D 11 hardware adapter has
type 0. DXGI can list one GPU several times; each is listed once.
On macOS and iOS the device opens Metal on the machine's GPU (type 1 for Apple silicon's unified memory, 2 for
a discrete GPU). Apple ships no software Metal, so SONA_DEVICE_SOFTWARE answers SONA_E_UNSUPPORTED there, and a
machine with no Metal device (a VM) fails the open cleanly with the same code; platforms
says where the Apple builds stand. The WASM core answers SONA_E_UNSUPPORTED
(its host owns the device; use sona_snapshot on a renderer of your own). In a web page, SonaRenderer.headless() in
the web SDK makes the same pictures with no canvas on the page (Headless pictures on the web). On the Xbox 360 the device is the console's
one XenosDevice: the one a Xenos renderer of the ctx already uses, else one made with Xe_Init and kept for the
program. sona_headless_renderer hands out the renderer for
sona_renderer_set_quality and the like; sona_headless_info_get names the device.
Every headless call runs on the ctx's owner thread and blocks until its pictures are read back. With GL the context is made current on the calling thread at each call. Run one ctx and one headless renderer per thread to render in parallel; the process-wide Vulkan and EGL loaders are loaded under a lock, so renderers may open on several threads at once. With several devices of the kind asked for, the best is tried first and the next takes over when it will not open.
On some NVIDIA Linux drivers a thread that exits while another thread is inside sona_headless_close of a Vulkan
renderer can deadlock inside the driver: vkDestroyDevice joins a driver thread that waits on a lock the exiting
thread's EGL cleanup holds. Keep renderer threads alive until every close has returned, or open and close renderers
on long-lived threads.
One picture¶
sona_render_options (104 bytes, 100 with 4-byte pointers), from sona_render_defaults:
| Field | Meaning | Default |
|---|---|---|
size |
sizeof(sona_render_options) |
set |
width, height |
Pixels, 1 to 8192 | 256, 256 |
preset |
SONA_PRESET_FULL, UPPER, BUST or FACE: the console's framings |
FULL |
yaw_deg, pitch_deg |
The camera moved around the preset's target | 0, 0 |
zoom |
The preset's distance divided by this | 1 |
turn_deg |
The avatar turned about its feet under the camera | 0 |
time |
Seconds into clip |
0 |
draw |
sona_draw_options: clear (premultiplied RGBA, default transparent), flags (SONA_DRAW_SHADOW, NO_BLOOM, EDITOR_AO, NO_CREDIT), credit corner |
zero |
flags |
0 | 0 |
clip |
A clip key or id. NULL: an avatar the call adds stands still (Stand0), one of yours keeps its own clip and time | NULL |
sona_headless_render_manifestadds the avatar (hidden), waits for it to be READY, draws it and removes it. A manifest that cannot be built returns its code (SONA_E_MANIFEST, ...); one that is not ready withinwait_secondsreturnsSONA_E_NOT_READY.sona_headless_renderdraws an avatar of yours. It is drawn alone: the ctx's other avatars are left out for that picture and keep their visibility. Withoutclip, its placement, clock and pose are as before when the call returns. Withclipset the call first plays that clip fromtime, assona_avatar_play, and the avatar goes on playing it afterwards; only its placement is put back.- Pixels are straight RGBA8, row 0 at the top, the same as
sona_snapshot. The small "Powered by 360sona" badge is drawn unlessSONA_DRAW_NO_CREDIT; a bot that crops it credits 360sona in its about text instead. - The same inputs give the same bytes on the same device and driver: the frame never depends on wall time
(headless updates advance no clock, and the clock is set for each picture). Two things change pictures that look
alike: the face sheet's size follows how many avatars the ctx holds when it builds, unless
sona_config.face_sizeis set (set 512 for gamerpics); and a GPU and a software device round differently. The software device is the one for byte-exact checks across machines.
Batches¶
sona_headless_batch(h, manifests, count, &ro, fn, user) turns count manifests (1000 bytes each, one after
another) into pictures and calls fn(user, index, result, rgba, width, height) once per manifest, in index order.
batch_in_flight avatars (default 8, at most 32) build ahead on the workers while one is drawn; each is removed once
drawn, so a batch of a million holds only those in flight. A manifest that fails gives its code and rgba NULL, and
the batch goes on; only a device loss or a ctx-wide error stops it. The ctx's caches (decoded items, GPU buffers)
stay within their budgets (sona_budget), so memory stops growing once they are full.
Sequences: turntables and clips¶
sona_sequence_options (52 bytes), from sona_sequence_defaults: frames (0: the clip's length at fps, rounded;
36 when nothing plays and turn_deg is set; else 1), fps (default 30), turn_deg (the sweep over the frames: 360
is a turntable) and flags (SONA_SEQUENCE_ORBIT: the camera goes round the avatar instead of the avatar turning
under the camera, so the light stays on its front).
Frame i is the clip at its start time + i / fps (the render options' time with a clip, else the avatar's own
clock) and the turn at turn_deg * i / frames, so a 360 degree turntable of 36 frames loops without a repeated frame.
The same inputs give the same frames every run, and frame i is the same picture as sona_headless_render at that
time and turn. sona_headless_sequence hands every frame to a callback; sona_headless_record writes them to a
video opened at the render size.
sona_sequence_options so;
sona_sequence_defaults(&so);
so.turn_deg = 360;
so.frames = 36;
sona_video_options vo;
sona_video_defaults(&vo);
vo.width = ro.width; vo.height = ro.height; vo.fps = 18;
sona_video *v;
sona_video_open(ctx, "turntable.gif", &vo, &v);
sona_headless_record(h, avatar, &ro, &so, v);
sona_video_finish(v, NULL, NULL);
// sona.hpp has no wrapper for headless recording: the C calls on the
// handles
sona_headless *h = nullptr;
sona_render_options ro;
sona_render_defaults(&ro);
sona_headless_open(ctx.raw(), nullptr, &h);
sona_sequence_options so;
sona_sequence_defaults(&so);
so.turn_deg = 360;
so.frames = 36;
sona_video_options vo;
sona_video_defaults(&vo);
vo.width = ro.width; vo.height = ro.height; vo.fps = 18;
sona_video *v = nullptr;
sona_video_open(ctx.raw(), "turntable.gif", &vo, &v);
sona_headless_record(h, avatar.raw(), &ro, &so, v);
sona_video_finish(v, nullptr, nullptr);
using var headless = new SonaHeadless(ctx);
var ro = new SonaRenderOptions();
using var video = new SonaVideo(ctx, "turntable.gif", 256, 256, fps: 18);
headless.Record(avatar, video, ro, frames: 36, fps: 18, turnDeg: 360);
video.Finish();
let h = Headless::open(ctx, &HeadlessOptions::default())?;
let ro = RenderOptions::default();
let mut so = SequenceOptions::default();
so.0.turn_deg = 360.0;
so.0.frames = 36;
let mut vo = Video::defaults(ro.width, ro.height);
vo.fps = 18.0;
let mut v = Video::open(ctx, Some(std::path::Path::new("turntable.gif")),
&vo, None)?;
h.record(avatar, &ro, &so, &mut v)?;
v.finish()?;
with sona.Headless(ctx) as h:
video = sona.Video(ctx, "turntable.gif", 256, 256, fps=18)
h.record(avatar, video, frames=36, fps=18, turn_deg=360)
video.finish()
Video out¶
sona_video_open(ctx, path, &vo, &v), then sona_video_frame(v, rgba) per frame, then sona_video_finish(v, &bytes,
&len). A NULL path keeps a GIF, MP4 or WebM in memory and finish hands back the bytes (free with sona_free).
sona_video_formats(ffmpeg) says which formats the machine can write.
sona_video_options (80 bytes, 76 with 4-byte pointers): format (0: from the extension), width, height,
fps (default 30), loops (GIF: 0 forever, else this many plays), quality (the encoder's CRF; 0: 18 for H.264,
30 for VP9 and AV1), background (0xAARRGGBB under transparent pixels where they are flattened; default opaque
black), flags (SONA_VIDEO_OPAQUE: flatten even where the format keeps transparency; SONA_VIDEO_NO_DITHER),
colours (GIF: a limit of 2 to 256 a frame) and ffmpeg (NULL: $SONA_FFMPEG, else ffmpeg on the PATH).
| Format | How | Transparency |
|---|---|---|
| PNG sequence | sona_png_encode per frame; the path holds one %d, %u or %0Nd (frames/f%04d.png) |
Kept |
| GIF | The library's own encoder: a frame of at most colours distinct colours (one fewer with transparent pixels) gets exactly those colours and no dither, so it comes back exact; otherwise a median-cut palette per frame over a 15-bit histogram, every pixel mapped to the entry nearest its own colour, and an 8 x 8 ordered dither that stays put from frame to frame (no crawling on a turntable), variable-length LZW. Frame delays add up to the frame times (frame i ends at round((i + 1) * 100 / fps) centiseconds); a GIF's frame rate has a cap of 50 frames a second |
Pixels under half alpha are transparent |
| MP4 | Raw RGBA into ffmpeg's standard input; libx264 (-preset medium -crf 18), else libopenh264, else mpeg4; yuv420p, +faststart |
Flattened onto background |
| WebM | libvpx-vp9 (-crf 30 -b:v 0), else libaom-av1, else libsvtav1, else libvpx (VP8) |
Kept with VP9 and VP8 (yuva420p), else flattened |
ffmpeg is never linked: it is started as a child process when the video opens (asked first which encoders it has),
its output goes to a temporary file so it can never block, and it is fed through a socket that cannot raise SIGPIPE
in the host (Windows: a pipe, CREATE_NO_WINDOW). Odd sizes are padded to even ones for the encoder. When ffmpeg
cannot be run, sona_video_open returns SONA_E_UNSUPPORTED with "ffmpeg could not be run, so MP4 and WebM are
off." and the fix "Install ffmpeg, or set sona_video_options.ffmpeg or SONA_FFMPEG to it."; when it has no fitting
encoder it says which. A failed encode gives SONA_E_IO and ffmpeg's last line in sona_last_error.
The sample: a bot's renderer¶
headless_bot [--cast devs-cast.json] [--pick N | --b64 TEXT] [--device auto|gpu|software]
[--out prefix] [--check] makes <prefix>-gamerpic.png (256 x 256 bust, transparent), <prefix>-card.png
(320 x 480, turned 20 degrees, opaque background), <prefix>-turntable.gif (36 frames, 240 x 360) and
<prefix>-wave.mp4 (GenericWave, 480 x 480 at a frame rate of 30 fps, when ffmpeg can write MP4). With --serve it keeps the
device warm and answers one request a line on standard input, as a bot's worker process would:
gamerpic <base64 manifest> out/123.png -> ok out/123.png 41.7
turntable <base64 manifest> out/123.gif -> ok out/123.gif 702.0
clip <base64 manifest> out/123.mp4 -> error unsupported ffmpeg
could not be run, so MP4 and WebM are off.
Measured¶
sona-bench --scene gamerpics --backend vk --source dir:"<asset folder>" \
--samples devs-cast.json --outfits random --catalogue sona-catalogue.bin
--counts 1000
Each run makes a batch (16 in flight, 256 x 256 bust, a ctx with face_size 512), the same batch again with its items
decoded, and 200 pictures of one ready avatar (the draw and readback alone). On an RTX 4090 machine, one run:
| Run | Pictures a second | ms a picture |
|---|---|---|
| Batch, every outfit new | 310.84 | 3.217 |
| The same batch again | 345.59 | 2.894 |
| One ready avatar, the picture alone | 3,288.43 | 0.304 |
The batch is bound by building the avatars, not by drawing them: the picture alone is a small part of a new avatar's time in the batch.
Memory over the first batch, every outfit new, at its first and last tenth:
| Memory | After the first tenth (MB) | At the end (MB) |
|---|---|---|
The library's CPU memory (sona_stats cpu_mb) |
180 | 439 |
| The library's GPU memory | 64.7 | 66.8 |
| The process (RSS) | 505.6 | 972.1 |
| The bench's own copy of every file it read | 11.4 | 73.1 |
The library's CPU memory climbs as the decoded-item cache fills towards its budget, and its GPU memory stays flat. The process grows by more than the library and the bench's files together; ASan reports no leak, so it is most likely the allocator's and the driver's own heaps. A long-running server should watch its RSS over its first few thousand pictures.
The headless module touches no draw path: sona_draw and the 1,000-avatar frame run the same code as before.
Bindings¶
- Python:
sona.Headless(ctx, device="software")withrender,render_manifest,batch,sequence,record;sona.Video(ctx, path, width, height, format=...)withframeandfinish;sona.headless_devices(),sona.video_formats(). - C#:
SonaHeadless,SonaRenderOptions,SonaVideo,SonaHeadless.Devices(),SonaVideo.Formats(). - Rust:
sona::Headless,sona::RenderOptions,sona::SequenceOptions,sona::Video,sona::headless_devices,sona::video_formats; the raw declarations insona360-sys(sona_sysin code).
Limits¶
- Metal on macOS and iOS: the device builds from the source, and the shaders compile on the device the first time they run; there is no macOS or iOS download. The iOS simulator build needs full Xcode.
- Device loss: a lost device ends the call with
SONA_E_CONTEXT; close and open the renderer again. - Each picture is one blocking snapshot; frames are not pipelined across pictures.
- GIF frames are whole frames with their own palette (no frame differencing), and the PNG writer stores, it does not compress.
- Android has no ffmpeg path, and the WASM build has no headless device.
This page as Markdown · All the docs as one file (llms-full.txt)