# Rendering quality

The quality settings remove the white specks small avatars otherwise show: single pixels of rim light on the edge of the figure, and texels that sparkle as a figure moves. The defaults are the web SDK's:

- **Multisampling (MSAA).** The main pass draws 4 samples a pixel on a hardware GPU and 1 on a software rasteriser. It is then resolved into one sample.
- **Centroid sampling.** Under MSAA the avatar shaders read their varyings inside the triangle, so an edge pixel no longer picks up texture from outside the item or a normal from past the silhouette.
- **A coverage-aware present.** At a silhouette pixel the resolved colour is the figure's colour times its coverage. The present divides by alpha before it doubles and encodes the colour, so edges keep their colour and the alpha says how much of the pixel is covered.
- **Mip chains.** Item textures carry every mip level and are filtered between levels, so a small figure reads a smaller level instead of skipping texels.
- **Coverage-preserving mips for cut-outs.** A colour texture with see-through parts (alpha below 128) gets its edge colours bled outwards, then mips where a texel passes the alpha test only if most texels under it pass. Lace and mesh thin out at a distance instead of filling in. The web SDK does the same, byte for byte.

The calls are in the [C reference: rendering quality](../reference.md#rendering-quality).

## What it changes

With quality off (`msaa` 1, no flags), small avatars show stray near-white pixels along the silhouette. The defaults
(4 samples, mips, cutout mips) remove nearly all of them. Multisampling does most of that; mips alone remove few,
and matter for textures seen small. Vulkan and GL give the same picture.

## Types

### sona_quality

```c
typedef struct sona_quality {
  uint32_t msaa;    /* SONA_MSAA_AUTO, or 1 (off), 2, 4 or 8 samples */
  uint32_t flags;   /* SONA_QUALITY_* */
} sona_quality;
```

8 bytes on every ABI, no padding.

- `msaa`: samples a pixel in the main pass. `SONA_MSAA_AUTO` (0) is 4 on a hardware GPU and 1 on a software rasteriser (llvmpipe, lavapipe, SwiftShader, the D3D software adapter), as the web's `antialias` default. An explicit count is used on a software device too, as the web's `antialias: true`.
- `flags`:
  - `SONA_QUALITY_MIPS` (1): item textures carry mip chains and the four address samplers filter between levels.
  - `SONA_QUALITY_CUTOUT_MIPS` (2): colour textures with a cut-out get edge bleed and coverage-preserving mips. It has no effect without `SONA_QUALITY_MIPS`.

## Functions

### sona_quality_defaults

```c
void sona_quality_defaults(sona_quality *q);
```

Fills `q` with the web SDK's defaults: `msaa = SONA_MSAA_AUTO`, `flags = SONA_QUALITY_MIPS | SONA_QUALITY_CUTOUT_MIPS`. Does nothing when `q` is NULL. A new renderer starts with these.

### sona_renderer_set_quality

```c
sona_result sona_renderer_set_quality(sona_renderer *r, const sona_quality *q);
```

Sets the quality for the next `sona_draw` and `sona_snapshot`. `q` NULL means the defaults. The struct is copied; it need not outlive the call.

- `SONA_E_ARGUMENT` when `r` is NULL, `msaa` is not 0, 1, 2, 4 or 8, or `flags` has a bit outside `SONA_QUALITY_*`. The renderer keeps its old quality then, and `sona_last_error` says why.
- More samples than the device has are clamped down to the largest it has.
- A change of `flags` uploads each avatar's textures again at its next draw. The old uploads go idle and are freed by the gpu idle budget, as unused items are.
- Call it on the thread that owns the ctx, like every renderer call.
- `sona_renderer_reset` keeps the quality and works the sample count out again for the new device.

### sona_renderer_quality

```c
sona_result sona_renderer_quality(const sona_renderer *r, sona_quality *out);
```

What the renderer draws with. `out->msaa` is the sample count the backend really uses: never `SONA_MSAA_AUTO`, and 1 when the device cannot multisample. `out->flags` are the flags as set, with `SONA_QUALITY_CUTOUT_MIPS` cleared when `SONA_QUALITY_MIPS` is not set. `SONA_E_ARGUMENT` when `r` or `out` is NULL.

`out->msaa` is asked of the backend at each call, so it shows two things that happen while drawing:

- **A fallback.** When a backend cannot make its multisampled target, it logs once and draws one sample from then on. Every backend does this (GL, GLES, Vulkan, D3D11, D3D12, Metal). After that `out->msaa` is 1, also after `sona_renderer_set_quality`. `sona_renderer_reset` asks the new device again.
- **A large frame.** A frame larger than 4096 x 4096 divided by the sample count halves its samples until it fits, as the web does. After such a `sona_draw` or `sona_snapshot`, `out->msaa` is the halved count, until the next smaller frame or `sona_renderer_set_quality`.

Before the first frame it is the count the device will use.

## What each backend does

- Vulkan: MSAA into a multisampled colour and depth with a resolve attachment; mip chains; centroid sampling in the SPIR-V. Drawn and tested on a hardware GPU, under the validation layer.
- GL 3.3, GLES 3.0, WebGL2: MSAA into multisampled renderbuffers, resolved with `glBlitFramebuffer`; mip chains; centroid sampling in the GLSL. Drawn and tested on a hardware GPU.
- D3D11: MSAA resolved with `ResolveSubresource`; mip chains; centroid sampling in the HLSL. Compiles (mingw-w64); not drawn.
- D3D12: MSAA resolved with `ResolveSubresource`; mip chains; centroid sampling in the HLSL. Compiles (mingw-w64); not drawn.
- Metal: MSAA resolved into a texture (`MTLStoreActionMultisampleResolve`); mip chains; centroid sampling in the MSL. Written, not compiled (no Mac).
- Xenos: no MSAA (one sample); mip level 0 only; no centroid sampling (its own microcode). Not compiled (no libxenon toolchain here).

Notes:

- At one sample the shaders' centroid qualifier is the pixel centre: the one-sample pictures of every replay case match the build before it byte for byte.
- GL also changes the renderbuffer binding when it multisamples (the state list of `sona_draw` grows by that one item).
- The mip chains are built on the CPU, so every backend draws the same levels. The renderer keeps one working buffer for them, the size of the largest texture's chain and its working memory (by format size set to 3.8 MB for 512 x 512), so steady frames do not allocate. A re-baked face sheet rebuilds its chain.
- On Xbox 360 the chain is built and only level 0 is used. A host there may set `flags` to 0 to save the CPU time.

## Example

```c
sona_renderer *r = NULL;
sona_quality q;

if (sona_renderer_create(ctx, &dev, &r) == SONA_OK) {
  sona_renderer_quality(r, &q);
  printf("drawing with %u samples\n", q.msaa);      /* 4 on a GPU, 1 on llvmpipe */

  /* the 1.0 look, for a comparison picture */
  q.msaa = 1;
  q.flags = 0;
  sona_renderer_set_quality(r, &q);
  sona_snapshot(r, &view, 1280, 720, &opts, before);

  /* back to the web's defaults */
  sona_renderer_set_quality(r, NULL);
  sona_snapshot(r, &view, 1280, 720, &opts, after);
}
```

```cpp
sona_device_desc dev;                      // your device
std::memset(&dev, 0, sizeof dev);
sona::Renderer rr;
if (sona::Renderer::create(ctx, dev, rr) == SONA_OK) {
  sona_quality q;
  sona_renderer_quality(rr.raw(), &q);     // sona.hpp has no quality wrapper: the C calls on the handle
  std::printf("drawing with %u samples\n", q.msaa);   // 4 on a GPU, 1 on llvmpipe

  // the 1.0 look, for a comparison picture
  q.msaa = 1;
  q.flags = 0;
  sona_renderer_set_quality(rr.raw(), &q);
  std::vector<uint8_t> before, after;
  rr.snapshot(view, 1280, 720, before);

  // back to the web's defaults
  sona_renderer_set_quality(rr.raw(), nullptr);
  rr.snapshot(view, 1280, 720, after);
}
```

```csharp
// r: a renderer of the ctx (ctx.CreateRenderer)
var q = SonaRenderer.QualityDefaults();
Console.WriteLine($"asking for {q.msaa} samples");   // SONA_MSAA_AUTO: 4 on a GPU, 1 on llvmpipe

// the 1.0 look, for a comparison picture
q.msaa = 1;
q.flags = 0;
r.SetQuality(q);
var before = r.Snapshot(view, 1280, 720);

// back to the web's defaults
r.SetQuality();
var after = r.Snapshot(view, 1280, 720);
```

```rust
let q = r.quality()?;
println!("drawing with {} samples", q.msaa);     // 4 on a GPU, 1 on llvmpipe

// the 1.0 look, for a comparison picture
let mut one = q;
one.msaa = 1;
one.flags = 0;
r.set_quality(Some(&one))?;
let before = r.snapshot(view, 1280, 720, None)?;

// back to the web's defaults
r.set_quality(None)?;
let after = r.snapshot(view, 1280, 720, None)?;
```

```python
# The Python package has no renderer on a device of your own: it reads avatars as data (avatar.pose()) for an engine
# to draw, and draws pictures with sona.Headless.
q = sona.quality_defaults()
```
