CPU Rendering with a Visualizer
What you build
A source application that floats a lit sphere, 25 cm in radius, in the middle of its own app volume. It finds every target app in the session (a Visualizer, a headset), gives each one a render sender, and renders every frame on the CPU from that target's eye poses and fields of view, with color and depth for both eyes.
What you will learn:
- why a source app finds its targets in the peer list instead of waiting for render requests
- how to declare the frame views of a stereo color-and-depth stream
- the frame cycle: begin a frame, read the pose and field of view of each view, write pixels, show
- which status codes mean "keep the sender and try again" and which mean "this sender is finished"
- which handles you destroy, and in which order
Before you start
- Windows, with QAROS installed on the Hub machine. The program runs on the Hub machine. Read Dynamic Loading first: this program loads the runtime the same way.
- A target. At least one target app in the session: open the Visualizer, or start a headset.
- Joining. The program joins the Hub on this machine as a local app. The first run of the executable opens the QAROS approval prompt; click Allow once. Later runs of the same executable join without a prompt. Nothing is stored. Joining as a local app starts the Hub when it is not running.
- This repository built with CMake: cmake --preset x64-windows, then cmake --build --preset x64-windows-debug --target cpu_rendering_visualizer.
Run it
--frames=N stops the program after N shown frames, counted over all targets; without it, the program stops after 300. --qaros-library=PATH loads one library file instead of the installed runtime. Flags take their value after =: --frames 300 is refused. Any other argument prints the usage and exits with 1. An executable that changes after a rebuild asks for Allow again.
Expected output, without the library's own log lines and the approval progress messages:
On each target, the "CPU Tutorial Volume" app volume shows the sphere.
Exit codes:
| Code | Meaning |
|---|---|
| 0 | the frames were shown |
| 1 | an unknown argument, a bad --frames value, qar_library_init or qar_runtime_create failed, or a failure after joining |
| 2 | no runtime loaded |
| 3 | the local-app join could not start or reach the Hub |
| 4 | the approval was denied |
| 5 | nobody answered the approval prompt in time |
| 6 | the program answering on this machine is not the installed QAROS |
| 7 | this executable no longer matches what QAROS approved |
Walkthrough
1. Declare the frame views
A render sender streams one video frame per display frame. The frame views say what is in it and where: which eye, color or depth, and in which texture. This program declares four views: both eyes' color side by side in texture 0, both eyes' depth side by side in texture 1.
The order matters. Every frame lists its views in the order you declared them, and the view index you pass to qar_render_frame_info_get_view_pose is that position: view 0 is the left eye, view 1 the right eye. A sender created with no views hands out frames without poses: reading the pose of view 0 then fails with QAR_STATUS_LOGIC_ERROR.
Send depth whenever you can. QAROS uses it for occlusion between app volumes and to warp your frame to the viewer's latest head pose.
2. Load the runtime and join
Every session comes from onboarding: there is no "connect" call. The program reads its arguments, loads the runtime, creates a runtime, and joins with the helpers in common.h:
example_join_session joins the Hub on this machine as a local app with the program's app tag:
The join itself, an onboard with QarOnboardLocalAppExt and QarOnboardLocalAppTagExt:
Local App Onboarding explains the local-app join, its app tag and its exit codes, 3 to 7.
3. Create the app volume
A render sender needs an app volume. The mixer composites the stream inside that box, and a sender for a target is what attaches the target to it.
qar_app_volumes_get_or_create is idempotent: the common name identifies the volume, so a second run gets the same one back. The pose puts its center 1 m in front of the room origin, in room space. The default size is 1 m along each axis.
4. Follow the target apps in the peer list
Your application decides which targets it renders for; QAROS does not choose for it. This program serves every running target app.
Do not wait for render requests to find targets. A target requests your content only once it is attached to your app volume, and a sender is what attaches it. Nothing is attached to a new application's own volume, so an application that waits for requests waits forever.
A peer is a target to render for when its role is QAR_PEER_ROLE_TARGET and its presence is QAR_PEER_PRESENCE_ONLINE, or QAR_PEER_PRESENCE_UNKNOWN on a Hub that has not reconciled it yet. The check is in common.h, shared with the D3D11 program:
The program collects those peers from the list:
qar_query_peer_specs takes an array you size with qar_query_peer_specs_count. Each spec it writes is a handle: destroy it with qar_peer_spec_handle_destroy once you have read it. A call that returns an error may still have written some specs: destroy those too. The list also holds this program's own peer, a source, which the role check skips.
On every listing, the program drops the senders of targets that left or stopped running, and creates a sender for every new one:
The main loop subscribes to peer updates before it lists the peers, so a target that joins in between is not missed. The callback runs on a library thread. It does nothing but raise a flag, with an Interlocked call because two threads touch it. The loop reads the list again after any update, and at least every 2 s:
One thread does all the QAROS work. The loop serves the targets in turn with the non-blocking begin (step 6), and sleeps 2 ms only when no target had a frame ready.
5. Create a render sender per target
qar_render_sender_init_default starts with auto-reconnect on, the side-by-side layout, BGRA8 color and D32 depth. The program sets the CPU graphics API, the target, the app volume, RGBA8 color and its views. QAR_GRAPHICS_API_CPU takes no extension in header.next.
qar_render_sender_create blocks until the target answers. A cancel token with a 10 s timeout bounds that wait. A target that does not answer gets no sender; the program prints No sender for ... yet and tries that target again 30 s later at the earliest.
This program creates senders on the thread that renders, to keep the code short. While a creation waits, no other target gets a frame: up to 10 s for each attempt. A target that keeps not answering therefore blocks the loop for up to 10 s, then leaves it alone for at least 30 s. A real application creates senders off its render thread, with qar_render_sender_create_async or with a thread per target, as the Vulkan cube does.
6. Begin, render, show
qar_render_sender_try_begin_frame starts the begin-frame wait on its first call and polls it on later calls. ready false means no frame yet: the target does not want one, or the receiver paused the stream. Poll again later.
A frame you get carries the pose and field of view to render each view with. They are in your app volume's app content space, the space you render in. qar_render_frame_info_get_view_pose and qar_render_frame_info_get_view_fov return them for views 0 and 1, the two eyes, as declared in step 1. A view of no eye, which the layout may add after the declared ones, gets the head pose and the left eye's field of view; the program renders such views from the head, or from the left eye when their pose or field of view cannot be read. The program turns each pose into rays through the pixels:
qar_render_sender_frame_cpu hands out the frame's textures as bytes with their pitch, and the views with their pixel rectangles. A view whose start lies past its end is mirrored. The program writes color views as RGBA or BGRA bytes and depth views as 32-bit floats:
The header does not specify the depth encoding. The program writes forward depth, 0 at the near plane and 1 at the far plane, as the Vulkan cube does:
qar_render_sender_show_frame presents the frame with the near and far planes the frame was rendered with, in app units. QAROS reprojects with exactly this metadata and your depth. Wrong values show up as swimming or misplaced content, never as an error code. Destroy the frame info after the show, not before.
A frame that began is always shown, because the show closes the frame cycle that begin opened. When the frame record or the view poses cannot be read, the program renders nothing into the frame, shows it anyway, and then treats the sender as finished: it destroys it and prints Stopped streaming to .... The D3D11 and Vulkan programs follow the same rule.
Tracing rays is what costs time: a frame has a ray for each of millions of pixels. So the program traces only where the sphere can be. Seen from above, the sphere is a circle, and the two rays from the eye that touch it bound the columns it can cover; seen from the side, two more rays bound the rows. Every other pixel keeps the background, which the program writes into a view's first row and copies to the others:
The color view and the depth view of an eye show the same pixels, so each ray is traced once and its hit written into both. The direction of a pixel's ray is the previous pixel's plus a fixed step, not rotated anew:
The frame rate follows how much of the view the sphere covers: up close, most pixels need a ray. The program renders on one thread to keep the code short. A Debug build calls each small vector function for every pixel; a Release build inlines them and runs several times faster. A real renderer splits the rows across threads, or renders on the GPU (see D3D11 Rendering).
7. Keep the sender, or end it
Some failures are part of the normal flow. The stream may be reconnecting, may have no transfer yet, or your app volume may not resolve yet where the receiver runs. For these, keep the sender and try the next frame. The check is in common.h:
| Status | What the program does |
|---|---|
| QAR_STATUS_MEDIA_STREAMING_STREAM_IS_PAUSED | keeps the sender; begin-frame waits through a pause, so it mostly shows up as ready false |
| QAR_STATUS_MEDIA_STREAMING_STREAM_IS_RECONNECTING | keeps the sender, tries the next frame |
| QAR_STATUS_MEDIA_STREAMING_NO_ACTIVE_TRANSFERS | keeps the sender, tries the next frame |
| QAR_STATUS_RENDERING_PRODUCER_APP_VOLUME_UNRESOLVED | keeps the sender, tries the next frame |
| QAR_STATUS_RENDERING_PRODUCER_STREAM_IS_CLOSED | destroys the sender; the next listing creates a new one if the target still runs |
| anything else | destroys the sender and prints Stopped streaming to ... |
Begin-frame reports the waiting codes wrapped inside QAR_STATUS_RENDERING_PRODUCER_UNABLE_TO_DO_BEGIN_FRAME. qar_result_has_code matches a wrapped code too, so check with it, not by comparing QarResult::code.
Ownership and teardown in C
- Senders first. Destroy each sender with qar_render_stream_handle_destroy before its session.
- The subscription before its state. The peer callback writes to peer_watch, which lives on main's stack. qar_cancel_token_handle_destroy ends the subscription and waits for a running callback, so after it returns nothing writes to peer_watch.
- Then the session, the runtime and the library, in that order. qar_session_handle_destroy leaves the session.
- One frame info per cycle. Destroy it with qar_render_frame_info_handle_destroy after the show. The texture bytes of qar_render_sender_frame_cpu belong to the sender: write them only between begin and show, and never free them.
- Peer specs are yours. Destroy every spec qar_query_peer_specs wrote, also when it returned an error.
- Callbacks run on library threads. Keep them short, and never call blocking QAROS functions or destroy the session from one.
Troubleshooting
| What you see | Cause | What to do |
|---|---|---|
| exit 4 or 5 after Joining the QAROS Hub on this machine as a local app | the approval prompt was denied, closed or not answered within 120 s | run again and click Allow |
| Could not start or reach QAROS on this machine. Start QAROS from the Start menu, then start this app again., exit 3 | the local-app join could neither start the Hub nor reach it | start QAROS from the Start menu and run again |
| No QAROS runtime loaded., exit 2 | no runtime found | install QAROS, fix or unset QAR_INSTANCE, or pass --qaros-library=PATH |
| stays at Looking for target apps ... | no running target app in the session | open the Visualizer or start a headset |
| No sender for <target> yet; trying again in 30 s, repeated | the target does not answer the sender creation within 10 s | check that the target is running; each attempt blocks the loop for up to 10 s |
| Stopped streaming to <target> | a failure that is not part of the normal flow, QAR_STATUS_RENDERING_PRODUCER_STREAM_IS_CLOSED included | the next listing creates a new sender while the target still runs |
| content swims or sits at the wrong depth | the near and far planes of the show do not match the frame, or poses not taken from this frame | render with this frame's poses and report the planes you used |
Next
- D3D11 Rendering: the same frame cycle with shared GPU textures, on a D3D11 device of your own.
- Rendering Streams: layouts, layout changes, hand tracking.
- CPU Rendering in Rust: the same program on the Rust binding.
Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.