# Overlays: a picture over the frame

`sona_renderer_overlay` gives a renderer a straight-alpha RGBA8 picture. Every `sona_draw` on that renderer blends it
over its target after the credit, on every backend. The player-facing editor's UI reaches the host's target this way
([Player editor](editor.md)). It also takes any HUD or 2D layer that a host has as pixels. The calls are in the [C reference: overlays](../reference.md#overlays). C++ calls them as they are and Rust through its raw layer
(`sona_sys::sona_renderer_overlay`). C#'s renderer keeps its handle to itself and the Python package draws headless
pictures only, so neither has a renderer to give an overlay to.

```c
sona_overlay o;
memset(&o, 0, sizeof o);
o.size = sizeof o;
o.width = hud_w;
o.height = hud_h;
o.rgba = hud_pixels;          /* straight RGBA8, row 0 at the top; read during the call only */
o.revision = hud_revision;    /* the same as the last call's: nothing is uploaded */
o.dirty[0] = x; o.dirty[1] = y; o.dirty[2] = w; o.dirty[3] = h;   /* what changed; w 0: all of it */
/* o.rect: where it lands in the target, pixels from the top left; w 0: the whole target */
sona_renderer_overlay(renderer, &o);
sona_draw(renderer, &view, &target, &opts);   /* the avatars, the credit, then the overlay */
sona_renderer_overlay(renderer, NULL);        /* the next draws have none (the texture is kept) */
```

```cpp
sona_overlay o{};
o.size = sizeof o;
o.width = hud_w;
o.height = hud_h;
o.rgba = hud.data();          // straight RGBA8, row 0 at the top; read during the call only
o.revision = hud_revision;    // the same as the last call's: nothing is uploaded
sona_renderer_overlay(r.raw(), &o);
r.draw(view, target, &opts);  // the avatars, the credit, then the overlay
sona_renderer_overlay(r.raw(), nullptr);   // the next draws have none (the texture is kept)
```

```csharp
// Not in C#: SonaRenderer keeps its native handle to itself, so Native.sona_renderer_overlay has no
// renderer to take. Draw the HUD with your engine after SonaRenderer.Draw, as any 2D layer.
```

```rust
let mut o: sona_sys::sona_overlay = unsafe { std::mem::zeroed() };
o.size = std::mem::size_of::<sona_sys::sona_overlay>() as u32;
o.width = hud_w;
o.height = hud_h;
o.rgba = hud.as_ptr();        // straight RGBA8, row 0 at the top; read during the call only
o.revision = hud_revision;    // the same as the last call's: nothing is uploaded
unsafe {
    sona_sys::sona_renderer_overlay(r.raw(), &o);
    r.draw(view, target, None)?;                               // the avatars, the credit, then the overlay
    sona_sys::sona_renderer_overlay(r.raw(), std::ptr::null()); // the next draws have none
}
```

```python
# Not in Python: the package draws headless pictures only, and an overlay belongs to a renderer that
# draws into your own target. Put the HUD over the picture with your imaging library.
```

## What it does

- **Blend:** `SRC_ALPHA, ONE_MINUS_SRC_ALPHA` on colour, `ONE, ONE_MINUS_SRC_ALPHA` on alpha. It samples nearest, with
  no depth and no culling. These are the credit badge's program and state, drawn right after the badge. Every picture
  without an overlay stays byte for byte what it was.
- **Where it lands:** the picture is stretched over `rect`, or over the whole target when `rect` has no width. At
  1:1 each texel lands on one pixel.
- **What it skips:** `sona_snapshot` and the renderer's other pictures (item previews, sprite sheets, stickers, the
  crowd's sheet pages) never draw it.
- **Uploads:** the renderer keeps one `SONA_GF_RGBA8` texture at the picture's size. A call uploads only when
  `revision` differs from the last call's, or when it is 0. On GL and Vulkan (`texture_region`) it uploads only the
  `dirty` rectangle. The other backends re-upload the whole texture. A new size makes a new texture, and the old one
  is released once the frames that used it have finished (the GPU cache's deferred release).
- **Lifetime:** the overlay holds until the next call, and `NULL` takes it away. After `sona_renderer_reset` or a lost
  device, set it again: the texture went with the device.
- **Errors:** `SONA_E_ARGUMENT` for no pixels, a zero size, a stride shorter than a row, a negative rect, or a dirty
  rectangle outside the picture. `SONA_E_LIMIT` for a picture larger than the device's largest texture.

## Backends

| Backend | How it draws the overlay |
|---|---|
| GL, GLES 3 | the credit's program at a second viewport, the overlay texture on unit 0 |
| Vulkan | a second quad in the host pass, with its own descriptor set for the step |
| D3D11, D3D12 | the step's SRV is the overlay's, at a second viewport |
| Metal | the third quad in the present encoder |
| Xenos | a second credit quad through its own vertex buffer, sampled with gamma like the badge |
