# C API: The data API

For hosts and engines that draw with their own renderer. A posed avatar is a `sona_pose_data`:

| Field | Meaning |
|---|---|
| `build_version` | Bumps when meshes, materials or textures change |
| `world[16]` | `W = scale * rotateY(turn) * translate(position)`, row-vector |
| `palette` | 864 floats: per joint 3 float4, columns 0..2 of the row-vector skin matrix, in avatar space |
| `view_joints` | 72 x 3 floats: each joint's translation before `W` |
| `meshes`, `mesh_count` | In draw order |
| `materials`, `material_count` | The materials the meshes draw with: each a `sona_material` (shader family, `SONA_MAT_*` flags, parameters, textures); a mesh names one by index |
| `textures`, `texture_count` | The face sheet is one of them (its material has `SONA_MAT_FACE`) |
| `props`, `prop_count` | Palettes for held props (up to 4), then, with sway on, one for each swaying mesh ([sway](../../guides/sway.md)), so `prop_count` can pass 4; size arrays by it |
| `expression[5]` | Mouth, left eyebrow, right eyebrow, left eye, right eye |

A `sona_mesh` holds `item` (the item it came from), `mask` (its AssetMask), `batch`, `material` (index into materials), `pass` (SONA_PASS_OPAQUE or SONA_PASS_TRANSPARENT), `palette` (-1: the body; else an index into props), `vertex_count`, `index_count`, `vertices`, `indices` (uint16 triangle list) and `key`. `key` is stable while the geometry lives and is shared by avatars wearing the same item, so a host can cache uploads by it.

The runtime vertex is 76 bytes, in host byte order:

| Offset | Type | What |
|---|---|---|
| +0 | float3 | Position (bind pose, avatar space) |
| +12 | uint8 x4 | Joint index x 3 (the palette's float4 row) |
| +16 | uint8 x4 | Weight (sum 255) |
| +20 | uint8 x4 | Normal, `(n * 0.5 + 0.5) * 255`; byte 23 is 0 |
| +24 | uint8 x4 | Vertex colour (baked occlusion and alpha) |
| +28 | float4 | uv0.xy, uv1.xy |
| +44 | float4 | uv2.xy, uv3.xy (the HQ shaders read uv2 only) |
| +60 | float4 | uv4.xy, uv5.xy |

A `sona_material` holds `shader_id` (0..4, the app's material family), `flags` (SONA_MAT_*), `params[40]` (AvatarShaderParams, 10 float4; the baked shaders read only 36..39), `transparency`, `color_tex`, `intensity_tex`, `decal_tex`, `color_addr`, `intensity_addr` and `decal_addr`. A texture index of -1 means neutral: white for colour, (0,0,0,255) for intensity and decal. The decal slot holds the matcap for reflective materials. Address modes: bit 0 set means U wraps, bit 1 set means V wraps; clear means clamp.

Material flags: `SONA_MAT_SKIN` (the skin program), `SONA_MAT_REFLECTIVE` (the decal slot holds the matcap), `SONA_MAT_FACE` (the colour texture is the face sheet), `SONA_MAT_TRANSPARENT`, `SONA_MAT_CARRYABLE` (drawn with a prop palette, or not drawn when it has none).

A `sona_texture` holds `key` (stable while it lives), `version` (bumps when the pixels change, as the face sheet does), `format`, `width`, `height`, `data` and `bytes`. Formats: `SONA_TEX_RGBA8_SRGB` (decoded and preprocessed, as the web uploads it; `width * height * 4` bytes), `SONA_TEX_RGBA16F_CUBE` (the irradiance cube: 6 faces, +X -X +Y -Y +Z -Z). `SONA_TEX_BC1_SRGB` and `SONA_TEX_BC3_SRGB` are reserved for 1.1.

A `sona_prop_palette` holds `item`, `playing` and `palette` (864 floats). While `playing` is 0 the prop's meshes are not drawn.

## sona_pose

```c
sona_result sona_pose(sona_avatar *a, const sona_pose_data **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `out` | `const sona_pose_data **` | Receives a pointer to the `sona_pose_data`, which the library keeps | required |

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

**Example** (also in [Drawing avatars with your own renderer](../../guides/own-renderer.md)):

```c
static float positions[65536 * 3], normals[65536 * 3];
const sona_pose_data *pose = NULL;
if (sona_pose(avatar, &pose) == SONA_OK && pose->mesh_count > 0) {
  const sona_mesh *m = &pose->meshes[0];
  if (m->vertex_count <= 65536 &&
      sona_pose_vertices(avatar, 0, SONA_SPACE_WORLD, positions, normals, 3) == SONA_OK)
    my_draw_positions(positions, normals, m->vertex_count, m->indices, m->index_count);
}
```

The avatar's pose data, valid until the next `sona_update`. `SONA_E_NOT_READY` before `READY`.

## sona_pose_vertices

```c
sona_result sona_pose_vertices(sona_avatar *a, uint32_t mesh, uint32_t space,
                               float *positions, float *normals, size_t stride_floats);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `mesh` | `uint32_t` | A mesh index of the pose data | - |
| `space` | `uint32_t` | `SONA_SPACE_*`: the space the vertices come back in | - |
| `positions` | `float *` | Receives the positions | required |
| `normals` | `float *` | Receives the normals, or NULL | `NULL`: not written |
| `stride_floats` | `size_t` | Floats from one vertex to the next in the output | - |

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

**Example** (also in [Drawing avatars with your own renderer](../../guides/own-renderer.md)):

```c
static float positions[65536 * 3], normals[65536 * 3];
const sona_pose_data *pose = NULL;
if (sona_pose(avatar, &pose) == SONA_OK && pose->mesh_count > 0) {
  const sona_mesh *m = &pose->meshes[0];
  if (m->vertex_count <= 65536 &&
      sona_pose_vertices(avatar, 0, SONA_SPACE_WORLD, positions, normals, 3) == SONA_OK)
    my_draw_positions(positions, normals, m->vertex_count, m->indices, m->index_count);
}
```

CPU skinning, for hosts that cannot skin on the GPU. Writes `vertex_count` positions (and normals, when `normals` is not NULL) for one mesh, `stride_floats` apart (at least 3). `space` is `SONA_SPACE_AVATAR` or `SONA_SPACE_WORLD`. Uses SIMD where the CPU has it.

## sona_shading_get

```c
sona_result sona_shading_get(sona_ctx *ctx, sona_shading *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `out` | `sona_shading *` | Receives the [`sona_shading`](../fields.md#sona_shading) | required |

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

**Example** (also in [Drawing avatars with your own renderer](../../guides/own-renderer.md)):

```c
size_t len = 0;
const void *vs = sona_shader_source(SONA_LANG_GLSL_330, SONA_PROG_CLOTH, 0, &len);
const void *fs = sona_shader_source(SONA_LANG_GLSL_330, SONA_PROG_CLOTH, 1, &len);
sona_shading shading;
if (vs && fs && sona_shading_get(ctx, &shading) == SONA_OK) {
  /* shading.irradiance (a cube, 16 x 16 per face), shading.rim[4], shading.shadow_rows[12]:
     the constants those shaders read */
}
```

The fixed shading inputs, for hosts that rebuild the look in their own shaders: `irradiance` (16x16 per face, RGBA16F), `irradiance_gain` (1.0), `ao_gain` (0 in the SDK), `bloom_gain` (0.1875), `shadow_rows[12]` (the 3x4 light lookup), `rim[4]` (0.549, 0.502, 0.4039, 1.6). `SONA_PENDING` until the data files have loaded.

## sona_shader_source

```c
const void *sona_shader_source(sona_shader_lang lang, sona_program prog, int stage, size_t *len);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `lang` | `sona_shader_lang` | The shading language | - |
| `prog` | `sona_program` | Which program of the frame | - |
| `stage` | `int` | 0 vertex, 1 fragment | - |
| `len` | `size_t *` | Receives how many bytes were written | required |

**Returns** `const void *`.

**Example** (also in [Drawing avatars with your own renderer](../../guides/own-renderer.md)):

```c
size_t len = 0;
const void *vs = sona_shader_source(SONA_LANG_GLSL_330, SONA_PROG_CLOTH, 0, &len);
const void *fs = sona_shader_source(SONA_LANG_GLSL_330, SONA_PROG_CLOTH, 1, &len);
sona_shading shading;
if (vs && fs && sona_shading_get(ctx, &shading) == SONA_OK) {
  /* shading.irradiance (a cube, 16 x 16 per face), shading.rim[4], shading.shadow_rows[12]:
     the constants those shaders read */
}
```

The SDK's own shaders, for hosts that draw themselves. `lang` is `SONA_LANG_GLSL_ES300`, `SONA_LANG_GLSL_330`, `SONA_LANG_SPIRV`, `SONA_LANG_HLSL` or `SONA_LANG_MSL`; `prog` is one of `SONA_PROG_SHADOW`, `GBUFFER`, `SSAO`, `BRIGHT`, `BLUR`, `PRESENT`, `CLOTH`, `SKIN`, `REFL`, `REFL_SKIN`, `CREDIT`; `stage` is 0 for vertex, 1 for fragment. Returns text, or SPIR-V bytes, with its length in `*len`; NULL when that language is not in this build.
