# C API: Crowds: level of detail and sprites

The guide, with the measured stadium numbers, is [Crowds](../../guides/crowd.md).

Each `sona_update` with a view gives every avatar in view a step from its height on screen in pixels:

| Step | When | Posed | Drawn as |
|---|---|---|---|
| `SONA_LOD_FULL` (0) | at or above `full_px` | every frame | its meshes |
| `SONA_LOD_SAMPLED` (1) | from `quarter_px` up to `full_px` | when the clip reaches a new frame | its meshes |
| `SONA_LOD_QUARTER` (2) | below `quarter_px` | as SAMPLED, and at most every 4th frame | its meshes |
| `SONA_LOD_SPRITE` (3) | below `sprite_px`, its look baked | never | one textured quad |

A sprite is a picture of the avatar, baked once per look (the manifest's bytes) and turn. Avatars cross between mesh and sprite over `fade_seconds`. A ctx opens with sprites off (`sprite_px` 0) and thresholds 120 and 40 until `sona_lod_set`.

`sona_lod_options`, 32 bytes:

| Field | Range | Default | Meaning |
|---|---|---|---|
| `full_px` | `>= quarter_px` | 120 | At or above: FULL |
| `quarter_px` | `0 .. full_px` | 40 | Below: QUARTER |
| `sprite_px` | `>= 0` | 48 | Below: SPRITE. 0: never |
| `fade_seconds` | `0 .. 10` | 0.25 | The mesh-to-sprite crossfade; 0 is a cut |
| `sprite_angles` | `4 .. 32` | 8 | Turns baked per look |
| `sprite_height` | `32 .. 512`, even | 64 | Pixels per baked picture; half as wide |
| `max_looks` | `1 .. 65536` | 16384 | Baked looks kept per renderer; avatars past it stay meshes |
| `bake_ms` | `>= 0` | 4 | Baking time per `sona_draw`; at least one batch of looks is baked when one is needed |

`sprite_angles * sprite_height / 2` must be 4096 or less.

`sona_lod_stats`, 32 bytes: `full`, `sampled`, `quarter`, `sprite` (avatars in view per step at the last `sona_update`), `fading` (of those, crossing between a mesh and a sprite), `looks` (baked looks over every renderer), `bakes` (looks baked by the last `sona_draw` or `sona_snapshot`), `bake_ms` (the time those took).

Rules for sprites:

- **Bakes happen inside `sona_draw` and `sona_snapshot`,** straight into the renderer's atlas pages. `sona_draw` records the bake into the host's frame, ahead of the picture (on Vulkan into the target's command buffer: nothing is submitted and nothing waits); a draw that bakes uses two frame indices. `sona_snapshot` bakes through the backend's readback, as it draws.
- **Sheets belong to a renderer.** An avatar becomes a sprite only when every renderer that drew it since the last `sona_update` has its look baked. While a renderer without sprites draws the ctx, far avatars stay meshes on every renderer, and no renderer ever loses an avatar.
- **Backends.** Vulkan, GL and GLES draw sprites. D3D11, D3D12, Metal and Xenos do not; far avatars stay meshes at the QUARTER rate there.
- **The bake must fit the device.** One look's sheet is `sprite_angles * (h / 2 + 2 * (h / 16))` pixels wide and `h + 2 * (h / 16)` tall (`h` = `sprite_height`), and a bake pass holds up to 32 sheets in a column. A device whose largest texture is smaller bakes fewer looks a pass; one that cannot fit a sheet draws no sprites.

## sona_lod_defaults

```c
void sona_lod_defaults(sona_lod_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_lod_options *` | The [`sona_lod_options`](../fields.md#sona_lod_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_lod_options x_lod_options;
memset(&x_lod_options, 0, sizeof x_lod_options);
sona_lod_defaults(&x_lod_options);
```

Writes the defaults in the table above, with sprites on at 48 px. NULL is ignored.

## sona_lod_set

```c
sona_result sona_lod_set(sona_ctx *ctx, const sona_lod_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `o` | `const sona_lod_options *` | The options, a [`sona_lod_options`](../fields.md#sona_lod_options) | `NULL`: the defaults |

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

**Example** (also in [Crowds: level of detail and sprites](../../guides/crowd.md)):

```c
/* 10,000 avatars in rows round a pitch, facing in */
sona_lod_set(ctx, NULL);
for (i = 0; i < 10000; i++) {
  sona_add_options o;
  sona_add_defaults(&o);
  seat(i, o.place.position, &o.place.turn_deg);   /* your layout */
  o.start_at = -1;                                 /* random start: a crowd out of step */
  sona_avatar_add_base64(ctx, looks[i % 12], &o, &av[i]);
}
```

Sets the options; NULL sets the defaults. Takes effect at the next `sona_update`. Changing `sprite_angles` or `sprite_height` drops every baked sheet; they are baked again as needed.

- `SONA_E_ARGUMENT`: `ctx` is NULL, a value is out of range or NaN, or `sprite_px` is above 0 and a renderer already on the ctx has a device too small for one sheet of the bake. Nothing changes then.
- `SONA_E_MEMORY`: the allocator failed (first call only).
- A renderer made later on a device too small for the options draws no sprites. It logs a warning once.

## sona_lod_get

```c
sona_result sona_lod_get(const sona_ctx *ctx, sona_lod_options *out);
```

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

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

**Example**:

```c
sona_lod_options x_lod_options;
memset(&x_lod_options, 0, sizeof x_lod_options);
sona_result result = sona_lod_get(ctx, &x_lod_options);
if (result != SONA_OK) printf("sona_lod_get: %s\n", sona_result_name(result));
```

Copies the current options: before any `sona_lod_set`, thresholds 120 and 40 and no sprites (`sprite_px` 0). `SONA_E_ARGUMENT` for a NULL argument.

## sona_avatar_lod

```c
uint32_t sona_avatar_lod(const sona_avatar *a, float *fade);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `fade` | `float *` | Receives the crossfade between two levels, 0 to 1; may be NULL | `NULL` allowed (below) |

**Returns** `uint32_t`.

The avatar's step at the last `sona_update`, and in `*fade` how far it is from its mesh (0) to its sprite (1). `SONA_LOD_FULL` and 0 for NULL, an avatar that is not READY, and one that was not in view at that update (culled or hidden). Back in view it has its step again. `fade` may be NULL.

## sona_lod_stats_get

```c
void sona_lod_stats_get(const sona_ctx *ctx, sona_lod_stats *out);
```

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

**Returns** Nothing.

**Example**:

```c
sona_lod_stats x_lod_stats;
memset(&x_lod_stats, 0, sizeof x_lod_stats);
sona_lod_stats_get(ctx, &x_lod_stats);
```

Fills `*out`. All zero before the first `sona_lod_set`. NULL arguments are ignored.
