Skip to content

C API: Drawing with the SDK's backends

The host owns the device and hands it over in a sona_device_desc {backend, u, frames_in_flight}. Pointers are the API's own handles cast to void *. frames_in_flight 0 means 2.

Backend u. What to pass
SONA_BACKEND_GL, SONA_BACKEND_GLES gl get_proc(name); the context current
SONA_BACKEND_VULKAN vk instance, physical_device, device, queue, queue_family, api_version, get_instance_proc
SONA_BACKEND_D3D11 d3d11 ID3D11Device *, ID3D11DeviceContext *
SONA_BACKEND_D3D12 d3d12 ID3D12Device *, ID3D12CommandQueue *
SONA_BACKEND_METAL metal id<MTLDevice>
SONA_BACKEND_XENOS xenos struct XenosDevice *

A sona_target says where one draw lands: width, height, and per API a GL framebuffer (0: the default), a Vulkan image, view, format, layouts and command buffer (recorded into, not submitted), a D3D11 render target view, a D3D12 resource, RTV handle, states and command list (recorded into, not executed), a Metal texture and command buffer, or a Xenos surface. Row 0 is the top of the picture on every API except GL and GLES, where it is the bottom. The colour target must be UNORM RGBA8 or BGRA8, not sRGB: the present pass encodes sRGB itself, as on the web.

sona_draw_options holds clear[4] (premultiplied RGBA, default 0, 0, 0, 0), flags and credit_corner. Flags:

  • SONA_DRAW_SHADOW: the key light shadow (the web's shadow: true). The shader is the web's, with no depth bias beyond the shadow pass's front-face culling, so on a bare figure it self-shadows in fine diagonal stripes with a hard edge across the chest. 360sona.com's editor and sona_editor draw without it.
  • SONA_DRAW_NO_BLOOM.
  • SONA_DRAW_EDITOR_AO: the editor's look, G-buffer and SSAO, aoGain 1.
  • SONA_DRAW_NO_CREDIT: the app shows the credit itself; the licence still requires it.

There is no "do not clear" flag (bit 2 is reserved): present overwrites the target, colour and alpha, as the web does. To put avatars over the host's own pixels, draw into a texture and composite it. credit_corner is SONA_CORNER_BOTTOM_RIGHT (0), BOTTOM_LEFT, TOP_RIGHT or TOP_LEFT.

sona_renderer_create

sona_result sona_renderer_create(sona_ctx *ctx, const sona_device_desc *dev,
                                 sona_renderer **out);
Parameter and type Meaning
ctx
sona_ctx *
The context (required)
dev
const sona_device_desc *
The options, a sona_device_desc (required)
out
sona_renderer **
Receives the new renderer (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_UNSUPPORTED (what each means).

Example (also in Getting started):

sona_device_desc dev;
sona_renderer *renderer = NULL;
memset(&dev, 0, sizeof dev);
dev.backend = SONA_BACKEND_GL;
/* SDL_GL_GetProcAddress, glfwGetProcAddress, ... */
dev.u.gl.get_proc = my_gl_get_proc;
while (sona_renderer_create(ctx, &dev, &renderer) == SONA_PENDING)
  /* the shading data is still arriving */
  sona_update(ctx, 0, NULL, NULL);

Makes a renderer on the host's device. SONA_E_UNSUPPORTED for a backend not compiled into this library.

sona_renderer_destroy

void sona_renderer_destroy(sona_renderer *r);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)

Returns Nothing.

Example (also in Getting started):

sona_avatar_remove(avatar);       /* frees what only it used */
sona_renderer_destroy(renderer);  /* before your device goes */
sona_close(ctx);

Frees the renderer and its GPU objects.

sona_draw

sona_result sona_draw(sona_renderer *r, const sona_view *view,
                      const sona_target *target,
                      const sona_draw_options *opts);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
view
const sona_view *
The camera, a sona_view (required)
target
const sona_target *
Where the frame lands, a sona_target (required)
opts
const sona_draw_options *
The options, a sona_draw_options (required)

Returns SONA_OK, SONA_PENDING while it is still working. Errors SONA_E_ARGUMENT, SONA_E_CONTEXT (what each means).

Example (also in Device loss: getting a renderer back):

/* the device is gone: the draw said so */
if (sona_draw(r, &view, &target, NULL) == SONA_E_CONTEXT) {
  /* every GPU object of the renderer forgotten */
  sona_renderer_lost(r);
  /* make the new device (see below for when the old one may go) */
  sona_device_desc dev = my_new_device();
  if (sona_renderer_reset(r, &dev) != SONA_OK) { /* try again later,
      or another device */ }
}

Draws every visible ready avatar, as posed by the last sona_update, folding each palette with this view. Each sona_draw and sona_snapshot is one frame; frames count from 1.

It changes API state and does not restore it:

  • GL and GLES: framebuffer bindings, viewport, program, vertex array, array and uniform buffer bindings, texture units 0..5 with their samplers, active texture, blend, depth, cull, front face, colour mask. GL_FRAMEBUFFER_SRGB is left off.
  • D3D11: every stage's shaders, constant buffers, SRVs and samplers, input layout, topology, rasteriser, blend, depth-stencil, render targets, viewports.
  • D3D12: root signature, pipeline state, descriptor heaps, viewports, scissors, render targets, primitive topology.
  • Vulkan and Metal: only what is recorded; nothing is submitted. With sprites on (1.1, sona_lod_set) a draw that bakes new looks records the bake ahead of its frame and uses two frame indices.
  • With multisampling (1.1, on by default), GL and GLES also change the renderbuffer binding.

On Vulkan, D3D12 and Metal it returns SONA_PENDING once frames_in_flight frames are outstanding without sona_renderer_frame_done.

sona_renderer_frame_index

uint64_t sona_renderer_frame_index(const sona_renderer *r);
Parameter and type Meaning
r
const sona_renderer *
The renderer (required)

Returns uint64_t.

Example:

uint64_t x_got = sona_renderer_frame_index(r);
printf("sona_renderer_frame_index: %lld\n", (long long)x_got);

The index of the last frame recorded (0 before the first).

sona_renderer_frame_done

void sona_renderer_frame_done(sona_renderer *r, uint64_t completed_frame);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
completed_frame
uint64_t
The index of the last frame the host's GPU has finished

Returns Nothing.

Example:

sona_renderer_frame_done(r, 0);

Vulkan, D3D12 and Metal: every frame up to and including completed_frame has finished on the GPU. The per-frame rings and the GPU cache's releases wait for it. GL and D3D11 ignore it.

sona_renderer_lost

void sona_renderer_lost(sona_renderer *r);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)

Returns Nothing.

Example (also in Device loss: getting a renderer back):

/* the device is gone: the draw said so */
if (sona_draw(r, &view, &target, NULL) == SONA_E_CONTEXT) {
  /* every GPU object of the renderer forgotten */
  sona_renderer_lost(r);
  /* make the new device (see below for when the old one may go) */
  sona_device_desc dev = my_new_device();
  if (sona_renderer_reset(r, &dev) != SONA_OK) { /* try again later,
      or another device */ }
}

After a device loss: forget every GPU object. The next sona_draw uploads again.

sona_renderer_reset

sona_result sona_renderer_reset(sona_renderer *r,
                                const sona_device_desc *dev);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
dev
const sona_device_desc *
The options, a sona_device_desc (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_MEMORY, SONA_E_CONTEXT (what each means).

Example (also in Device loss: getting a renderer back):

/* the device is gone: the draw said so */
if (sona_draw(r, &view, &target, NULL) == SONA_E_CONTEXT) {
  /* every GPU object of the renderer forgotten */
  sona_renderer_lost(r);
  /* make the new device (see below for when the old one may go) */
  sona_device_desc dev = my_new_device();
  if (sona_renderer_reset(r, &dev) != SONA_OK) { /* try again later,
      or another device */ }
}

Starts the renderer again on a new device of the same backend, after sona_renderer_lost. Another backend is SONA_E_ARGUMENT.

sona_snapshot

sona_result sona_snapshot(sona_renderer *r, const sona_view *view,
                          uint32_t width, uint32_t height,
                          const sona_draw_options *opts, uint8_t *rgba_out);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
view
const sona_view *
The camera, a sona_view (required)
width
uint32_t
The picture's width in pixels
height
uint32_t
The picture's height in pixels
opts
const sona_draw_options *
The options, a sona_draw_options (required)
rgba_out
uint8_t *
Receives straight RGBA8, row 0 at the top: width * height * 4 bytes (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_CONTEXT, SONA_E_READBACK (what each means).

Example (also in The same picture natively and on the web):

sona_quality q;
sona_draw_options o;
sona_quality_defaults(&q);
q.msaa = 1;                      /* one sample */
q.flags = 0;                     /* no mips */
sona_renderer_set_quality(renderer, &q);
memset(&o, 0, sizeof o);
o.flags = SONA_DRAW_NO_CREDIT;   /* no shadow, no badge; bloom stays on */
sona_camera_preset(avatar, SONA_PRESET_FULL, &view);
sona_snapshot(renderer, &view, 512, 512, &o, rgba);

Draws into an internal target, folding palettes with this view, and reads it back as straight RGBA8, row 0 at the top. Blocks until the GPU finishes this one frame. On Vulkan it submits on the device's VkQueue, so no other thread may use that queue during the call. The credit is drawn unless SONA_DRAW_NO_CREDIT. rgba_out holds width * height * 4 bytes.

sona_png_encode

sona_result sona_png_encode(sona_ctx *ctx, const uint8_t *rgba,
                            uint32_t width, uint32_t height, uint8_t **out,
                            size_t *out_len);
Parameter and type Meaning
ctx
sona_ctx *
The context (required)
rgba
const uint8_t *
Straight RGBA8, row 0 at the top, width * height * 4 bytes (required)
width
uint32_t
The picture's width in pixels
height
uint32_t
The picture's height in pixels
out
uint8_t **
Receives the bytes; free them with sona_free (required)
out_len
size_t *
Receives how many bytes *out holds (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_MEMORY (what each means).

PNG bytes of an RGBA8 picture, with the web's three tEXt chunks after IHDR: Software "360sona", Source "https://360sona.com", Comment "Powered by 360sona". Free *out with sona_free.

sona_free

void sona_free(sona_ctx *ctx, void *p);
Parameter and type Meaning
ctx
sona_ctx *
The context (required)
p
void *
Memory the library handed out (required)

Returns Nothing.

Example (also in FBX, Roblox and clips in the export):

sona_export_options o;
uint8_t *fbx = NULL;
size_t fbx_len = 0;
sona_export_defaults(&o);
o.format = SONA_EXPORT_FBX;
/* the clip it plays, as an animation on the skeleton */
o.flags = SONA_EXPORT_CLIPS;
if (sona_export_avatar(avatar, &o, &fbx, &fbx_len, NULL) == SONA_OK) {
  FILE *f = fopen("avatar.fbx", "wb");
  if (f) { fwrite(fbx, 1, fbx_len, f); fclose(f); }
  sona_free(ctx, fbx);
}

Frees memory the library handed out (sona_png_encode), through the ctx's allocator.

This page as Markdown · All the docs as one file (llms-full.txt)