# 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

```c
sona_result sona_renderer_create(sona_ctx *ctx, const sona_device_desc *dev, sona_renderer **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `dev` | `const sona_device_desc *` | The options, a [`sona_device_desc`](../fields.md#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](../../troubleshooting.md#error-codes)).

**Example** (also in [Getting started](../../getting-started.md)):

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

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

## sona_renderer_destroy

```c
void sona_renderer_destroy(sona_renderer *r);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |

**Returns** Nothing.

**Example** (also in [Getting started](../../getting-started.md)):

```c
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

```c
sona_result sona_draw(sona_renderer *r, const sona_view *view, const sona_target *target,
                      const sona_draw_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `view` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `target` | `const sona_target *` | Where the frame lands, a [`sona_target`](../fields.md#sona_target) | required |
| `opts` | `const sona_draw_options *` | The options, a [`sona_draw_options`](../fields.md#sona_draw_options) | required |

**Returns** `SONA_OK`, `SONA_PENDING` while it is still working. **Errors** `SONA_E_ARGUMENT`, `SONA_E_CONTEXT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Device loss: getting a renderer back](../../guides/device-loss.md)):

```c
/* the device is gone: the draw said so */
if (sona_draw(r, &view, &target, NULL) == SONA_E_CONTEXT) {
  sona_renderer_lost(r);            /* every GPU object of the renderer forgotten */
  /* 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

```c
uint64_t sona_renderer_frame_index(const sona_renderer *r);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `const sona_renderer *` | The renderer | required |

**Returns** `uint64_t`.

**Example**:

```c
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

```c
void sona_renderer_frame_done(sona_renderer *r, uint64_t completed_frame);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `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**:

```c
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

```c
void sona_renderer_lost(sona_renderer *r);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |

**Returns** Nothing.

**Example** (also in [Device loss: getting a renderer back](../../guides/device-loss.md)):

```c
/* the device is gone: the draw said so */
if (sona_draw(r, &view, &target, NULL) == SONA_E_CONTEXT) {
  sona_renderer_lost(r);            /* every GPU object of the renderer forgotten */
  /* 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

```c
sona_result sona_renderer_reset(sona_renderer *r, const sona_device_desc *dev);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `dev` | `const sona_device_desc *` | The options, a [`sona_device_desc`](../fields.md#sona_device_desc) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY`, `SONA_E_CONTEXT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Device loss: getting a renderer back](../../guides/device-loss.md)):

```c
/* the device is gone: the draw said so */
if (sona_draw(r, &view, &target, NULL) == SONA_E_CONTEXT) {
  sona_renderer_lost(r);            /* every GPU object of the renderer forgotten */
  /* 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

```c
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 | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `view` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#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`](../fields.md#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](../../troubleshooting.md#error-codes)).

**Example** (also in [The same picture natively and on the web](../../guides/parity.md)):

```c
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

```c
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 | Type | Meaning | Default |
|---|---|---|---|
| `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](../../troubleshooting.md#error-codes)).

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

```c
void sona_free(sona_ctx *ctx, void *p);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `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](../../guides/export-fbx.md)):

```c
sona_export_options o;
uint8_t *fbx = NULL;
size_t fbx_len = 0;
sona_export_defaults(&o);
o.format = SONA_EXPORT_FBX;
o.flags = SONA_EXPORT_CLIPS;          /* the clip it plays, as an animation on the skeleton */
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.
