Skip to main content
Version: Dev (main)

Render Loops

Language:

A render sender serves one viewer (a headset, a visualizer view). Your application serves every viewer that joins with a sender of its own, from one loop or one thread per viewer. This page shows the three ways to drive those senders, what each costs, and when to choose it. The sender itself — layouts, textures, show parameters — is in Rendering Streams.

Two modes: who decides when to render​

ModeBegin-frame waits forThe poses are forFits
Request-driven (default)The viewer's render request, which the mixer times for the display tick it needsThe display time the request names, predicted again when you first read the poseApplications that can render when asked
Free-runningOnly a free destination; the viewer keeps one readyPredicted from the newest head pose for when the frame will be shownApplications whose render loop runs at its own rate (a game loop, an engine's frame)

Why both exist:

  • Request-driven lets the mixer decide: it knows each viewer's display rate and when its next tick is, so it asks for each frame just in time, and your application renders exactly what each viewer will show.
  • Free-running lets your application decide: many engines own their frame loop and cannot wait for a request. The sender then hands you the poses whenever you render, and the viewer keeps a destination ready for the result.

In both modes the pose you read is predicted for when the frame will be shown: a request your loop took late is rendered that much further ahead, so a late read costs you no pose accuracy.

Creating a sender in each mode​

You choose the mode when you create the sender. Request-driven is the default; free-running chains the render loop extension:

/* Request-driven: the default init chains no extension. */
QarRenderSenderInit init = qar_render_sender_init_default();
init.peer_id = viewer_peer_id;
/* ... app volume, formats, views: see Rendering Streams ... */

/* Free-running: chain the render loop extension. */
QarRenderSenderRenderLoopExt loop = qar_render_sender_render_loop_ext_default();
loop.render_loop = QAR_RENDER_LOOP_FREE_RUNNING;
loop.header.next = init.header.next; /* keep any extension already chained */
init.header.next = &loop;

QarRenderSender* sender = NULL;
QarResult r = qar_render_sender_create(session, &init, NULL, &sender);

Implementing the request-driven mode​

The viewer asks for each frame; your application answers it. Every loop here renders for a viewer only after its request is in, and handles each stream state as Stream states lists.

Serving several viewers​

Every loop on this page, request-driven or free-running, serves a collection of senders, one per viewer. A viewer is a peer whose role is target; it joins when it comes online and leaves when it goes offline (Finding rendering targets). Peer updates arrive on a library thread: queue them, and create and destroy senders on the render thread.

/* Library thread: queue the viewer for the render thread. */
static void on_peer(QarPeerSpec* peer, void* user_state)
{
QarPeerRole role = QAR_PEER_ROLE_INTERNAL;
QarPeerPresence presence = QAR_PEER_PRESENCE_UNKNOWN;
QarPeerId id = qar_peer_id_default();
if(qar_result_is_error(qar_peer_spec_get_role(peer, &role)) || role != QAR_PEER_ROLE_TARGET
|| qar_result_is_error(qar_peer_spec_get_presence(peer, &presence))
|| qar_result_is_error(qar_peer_spec_get_id(peer, &id)))
return;
bool online = presence == QAR_PEER_PRESENCE_ONLINE || presence == QAR_PEER_PRESENCE_UNKNOWN;
viewer_queue_push(user_state, id, online); /* your thread-safe queue */
}

/* Render thread, once per pass: add joined viewers, drop the ones that left. */
static void update_viewers(Viewers* viewers)
{
QarPeerId id;
bool online;
while(viewer_queue_pop(&viewers->queue, &id, &online))
{
Viewer* viewer = viewers_find(viewers, id);
if(online && viewer == NULL)
viewers_add(viewers, id, create_sender_for(id)); /* Rendering Streams */
else if(!online && viewer != NULL)
drop_viewer(viewers, viewer);
}
}

/* Releases the viewer's frame and sender. Its next online update serves it again. */
static void drop_viewer(Viewers* viewers, Viewer* viewer)
{
if(viewer->ready != NULL)
qar_render_frame_info_handle_destroy(viewer->ready);
qar_render_stream_handle_destroy(viewer->sender);
viewers_remove(viewers, viewer);
}

Blocking: one thread per viewer​

begin_frame waits for the viewer's request, you render with the poses it hands out, and show.

/* One thread per viewer, started when the viewer joins. */
static void viewer_thread(Viewer* viewer)
{
while(!viewer->leaving)
{
QarRenderFrameInfo* info = NULL;
QarResult r = qar_render_sender_begin_frame(viewer->sender, viewer->cancel, &info);
if(r.code == QAR_STATUS_RENDERING_PRODUCER_STREAM_IS_CLOSED)
break; /* gone: the thread's owner destroys the sender */
if(qar_result_is_error(r))
continue; /* reconnecting, not set up yet, cancelled: retry */
render_views(viewer, info); /* your renderer, with the poses in info */
show(viewer->sender); /* also when rendering failed */
qar_render_frame_info_handle_destroy(info);
}
}

static void show(QarRenderSender* sender)
{
QarRenderFrameShow frame_show = qar_render_frame_show_default();
frame_show.rendered_near_far.near_plane = 0.1f;
frame_show.rendered_near_far.far_plane = 100.0f;
qar_result_log_if_error(qar_render_sender_show_frame(sender, &frame_show));
}
  • Pros: the simplest code; the mixer paces each viewer exactly, so each frame is rendered just in time for its tick.
  • Cons: one thread per viewer. Threads must not share a renderer that serialises them, or the slowest viewer sets every viewer's rate. In one thread serving several viewers in turn, each begin_frame waits for its viewer, and a paused viewer blocks the whole loop until it resumes.
  • Choose it for a single viewer, or a renderer that can render for each viewer on its own thread.

Non-blocking: one loop for every viewer​

Poll each sender with try_begin_frame; render only for the viewers whose frame is ready, and skip the rest.

The Vulkan cube example implements this loop in C++ on the C API with --render-loop request-driven, on a fixed schedule (--fps <n>, default 60) or as fast as the viewers take frames (--uncapped).

Arm the next wait right after you show. The first try_begin_frame call starts the wait for the viewer's next request, and only a later call can find it complete. Call it once more right after show_frame, so the wait runs while your loop is idle and the next pass finds the frame ready. Started only at the next pass, the wait completes one pass later, and a loop at its own rate (say 30 fps) serves every viewer every other frame. A frame the arming call already returns is kept for the next pass.

typedef struct Viewer
{
QarPeerId id;
QarRenderSender* sender;
QarRenderFrameInfo* ready; /* a frame begun and not yet rendered */
} Viewer;

typedef enum Poll { POLL_FRAME, POLL_NOT_NOW, POLL_CLOSED } Poll;

/* Never blocks. POLL_FRAME leaves the frame in viewer->ready. */
static Poll poll_frame(Viewer* viewer)
{
if(viewer->ready != NULL)
return POLL_FRAME; /* came back when the last show armed it */
bool ready = false;
QarResult r = qar_render_sender_try_begin_frame(viewer->sender, &viewer->ready, &ready);
if(r.code == QAR_STATUS_RENDERING_PRODUCER_STREAM_IS_CLOSED)
return POLL_CLOSED;
if(qar_result_is_error(r) || !ready)
{
viewer->ready = NULL; /* busy, paused, reconnecting, not set up yet */
return POLL_NOT_NOW;
}
return POLL_FRAME;
}

for(;;) /* your loop, at its own rate */
{
update_viewers(&viewers);
bool rendered = false;
for(size_t i = 0; i < viewers.count;)
{
Viewer* viewer = &viewers.items[i];
Poll poll = poll_frame(viewer);
if(poll == POLL_CLOSED)
{
drop_viewer(&viewers, viewer); /* swap-removes: item i is the next viewer */
continue;
}
if(poll == POLL_FRAME)
{
render_views(viewer, viewer->ready); /* only for a viewer that asked */
show(viewer->sender); /* also when rendering failed */
qar_render_frame_info_handle_destroy(viewer->ready);
viewer->ready = NULL;
poll_frame(viewer); /* arm the next wait now */
rendered = true;
}
++i;
}
if(!rendered)
sleep_ms(1); /* nobody needed a frame: do not spin (or wait for your frame tick) */
}
  • Pros: one thread and one renderer for every viewer; a slow, paused or reconnecting viewer holds back no other; frames are still rendered only when a viewer asked, and paced by the mixer.
  • Cons: you keep a frame slot per viewer and must arm the next wait after each show.
  • Choose it when one render loop serves several viewers and can render whenever a request is ready.

Sequential or parallel sending​

The loops above send sequentially: one thread visits every viewer. You can also send in parallel: a worker thread per viewer, each running the same per-viewer step on its own sender.

Sequential (one thread)Parallel (a worker per viewer)
RendererOne, shared; simplestOne per worker, or a renderer that takes work from several threads
A slow viewerHolds back no other with a non-blocking loop; with a blocking one, every viewerHolds back no other
Use it whenRendering for a viewer is quick next to a display frameRendering for each viewer takes a large part of a display frame

A sender is used from one thread at a time: keep each viewer's sender on its worker.

Implementing the free-running mode​

The Vulkan cube example is the reference implementation in C++ on the C API: it runs this loop by default, on a fixed 60 fps schedule whose waits are the time left until the next slot, so a frame that runs long does not shift the ones after it.

Your loop renders on its own schedule and sends to whichever viewers are ready. Create the senders free-running (Creating a sender in each mode), serve them as a collection as in Serving several viewers, and each pass, for each viewer:

  1. is_ready_for_next_frame — never blocks. True once the viewer handed back the previous frame's destination; the destination is then reserved for this viewer's next frame. False while it is busy, paused or reconnecting: skip the viewer, render nothing for it.
  2. next_render_pose — the poses for this frame, predicted for when it will be shown.
  3. Render into your own target.
  4. begin_frame — returns at once with the reserved destination and exactly the poses of step 2.
  5. Copy your target into the frame and show it.
for(;;) /* your loop, at its own rate, or uncapped */
{
update_viewers(&viewers);
bool rendered = false;
for(size_t i = 0; i < viewers.count;)
{
Viewer* viewer = &viewers.items[i];
bool ready = false;
QarResult r = qar_render_sender_is_ready_for_next_frame(viewer->sender, &ready);
if(r.code == QAR_STATUS_RENDERING_PRODUCER_STREAM_IS_CLOSED)
{
drop_viewer(&viewers, viewer); /* swap-removes: item i is the next viewer */
continue;
}
++i;
if(qar_result_is_error(r) || !ready)
continue; /* busy, paused, reconnecting: no pose, no render, no copy */

QarPose poses[2];
QarFov fovs[2];
if(qar_result_is_error(qar_render_sender_next_render_pose(viewer->sender, 2, poses, fovs)))
continue; /* no head pose yet, app volume unresolved: next pass */
render_into_own_target(viewer, poses, fovs);

QarRenderFrameInfo* info = NULL;
if(qar_result_is_error(qar_render_sender_begin_frame(viewer->sender, NULL, &info)))
continue; /* the stream changed state since the check: next pass */
copy_into_frame(viewer); /* your target into the sender's textures */
show(viewer->sender);
qar_render_frame_info_handle_destroy(info);
rendered = true;
}
if(!rendered)
sleep_ms(1); /* nobody was ready: do not spin (or wait for your frame tick) */
}
  • Pros: fits an engine that owns its frame loop; the viewer hands the destination back while you render, so a frame reaches the viewer as soon as it is done; a slow, paused or reconnecting viewer holds back no other.
  • Cons: each frame is copied from your target into the sender's; a loop capped well below the viewer's display rate delivers fewer frames, which the viewer's warp covers with older ones.
  • Choose it when your application renders on its own schedule, or wants to render one view of the scene and send it to several viewers as they become ready.

Sent in parallel, each worker runs this per-viewer step on its own sender; a sender is used from one thread at a time (Sequential or parallel sending).

Stream states​

A viewer's stream is not always ready for a frame. Each loop below handles every state the same way:

StateNon-blocking calls returnBlocking begin-frameThe loop
ReadyA frame (try_begin_frame), or ready (is_ready_for_next_frame)Returns the frameRenders and shows it
Busy: the viewer has not taken the previous frame yetNot readyWaitsSkips this viewer this pass
Paused: the viewer's display is not visibleNot ready, or QAR_STATUS_MEDIA_STREAMING_STREAM_IS_PAUSEDWaits until it resumesSkips it and keeps the sender
Reconnecting: the connection dropped, auto-reconnect is onNot ready, or QAR_STATUS_MEDIA_STREAMING_STREAM_IS_RECONNECTINGFails with that codeSkips it and keeps the sender; it resumes by itself
Not set up yet: no head pose yet, app volume not resolvedQAR_STATUS_RENDERING_PRODUCER_MISSING_POSE_DATA, QAR_STATUS_RENDERING_PRODUCER_APP_VOLUME_UNRESOLVEDFails with that codeRetries next pass
Closed for goodQAR_STATUS_RENDERING_PRODUCER_STREAM_IS_CLOSEDFails with that codeDestroys the sender and removes the viewer; creates a new sender for it while the peer list still shows it online, after a back-off (When a Target Restarts)
  • C# names the codes StatusCode.MediaStreamingStreamIsPaused, MediaStreamingStreamIsReconnecting, RenderingProducerMissingPoseData, RenderingProducerAppVolumeUnresolved and RenderingProducerStreamIsClosed; Rust StatusCode::MEDIA_STREAMING_STREAM_IS_PAUSED, MEDIA_STREAMING_STREAM_IS_RECONNECTING, RENDERING_MISSING_POSE_DATA, RENDERING_APP_VOLUME_UNRESOLVED and RENDERING_STREAM_IS_CLOSED.
  • Show every frame you began, also when your rendering failed: until it is shown, the viewer's destination stays with you and the viewer shows the source as not responding. The API has no invalid marker; show what you have.

Saving resources​

Every loop above renders for a viewer only when that viewer needs a frame.

ViewerRequest-driven, blockingRequest-driven, non-blockingFree-running
Slow (busy with the last frame)Its thread waits, renders nothingSkipped, nothing renderedSkipped by the readiness check, nothing rendered or copied
PausedIts thread waits, renders nothingSkippedSkipped
ReconnectingBegin fails, nothing renderedSkippedSkipped
GoneThread ends, sender destroyedSender destroyedSender destroyed
None connectedNo threadsThe pass finds nothing ready and sleepsThe pass finds nothing ready and sleeps
  • An uncapped free-running loop renders at most once per viewer frame: the readiness check is true only once the viewer handed back its last frame's destination.
  • When nothing was ready in a pass, sleep briefly or wait for your own frame tick; polling in a tight loop burns a core for nothing.

Which to choose​

Request-driven, blockingRequest-driven, non-blockingFree-running
Who sets the frame rateThe mixer, per viewerThe mixer, per viewerYour loop, limited by each viewer
ThreadsOne per viewerOneOne, or a worker per viewer
Calls per framebegin, showtry_begin, show, try_beginis_ready, next_render_pose, begin, show
Rendered intoThe sender's frameThe sender's frameYour own target, then copied
A slow or paused viewer holds back othersOnly if threads share a rendererNoNo
  1. One viewer, or a thread per viewer is easy for you: request-driven, blocking.
  2. One render loop for several viewers that can render when asked: request-driven, non-blocking, with the next wait armed after each show.
  3. Your engine owns the frame loop and renders on its own schedule: free-running, with the readiness check before every render.