# Device loss: getting a renderer back

A GPU device can go away under a running game: a driver update or crash, a GPU reset (TDR on Windows), a machine
switching GPUs, an Android app paused with its EGL context. The SDK keeps everything that is not on the GPU: the
`sona_ctx`, its avatars, their poses and clips, a renderer's style, lighting, quality and overlay. Two calls bring the
GPU side back.

```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 */ }
}
```

```cpp
// the device is gone: the draw said so
try {
  r.draw(view, target);
} catch (const sona::Error &e) {
  if (e.code != SONA_E_CONTEXT) throw;
  r.lost();                                // every GPU object of the renderer forgotten
  // make the new device (see below for when the old one may go)
  r.reset(my_new_device());                // throws again if it fails: try later, or another device
}
```

```csharp
// the device is gone: the draw said so
if (r.Draw(view, target) == SonaResult.Context)
{
    r.Lost();                             // every GPU object of the renderer forgotten
    // make the new device (see below for when the old one may go)
    var dev = MyNewDevice();
    if (r.Reset(dev) != SonaResult.Ok) { /* try again later, or another device */ }
}
```

```rust
// the device is gone: the draw said so
if matches!(unsafe { r.draw(view, target, None) }, Err(ref e) if e.code == Code::CONTEXT) {
    r.lost();                             // every GPU object of the renderer forgotten
    // make the new device (see below for when the old one may go)
    let dev = my_new_device();
    if unsafe { r.reset(&dev) }.is_err() { /* try again later, or another device */ }
}
```

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

- `sona_renderer_lost`: forget every GPU object (textures, buffers, pipelines' targets, sprite sheets, shadow maps).
  The next `sona_draw` uploads again. When the device is still good (a GL context restored in place, a D3D11
  device the host kept), that is all: draw on.
- `sona_renderer_reset`: start the renderer again on a new device of the same backend. Its quality, style and
  lighting carry over; Vulkan's optional features (`sona_renderer_vk_features`) start at 0 again, since the new device
  may not have them.
- D3D11 and D3D12 find a removed device themselves, and Vulkan a lost one when it reads back (`sona_snapshot`): then
  `sona_draw` or `sona_snapshot` answers `SONA_E_CONTEXT`, with a `SONA_EVENT_ERROR` event. GL does not look for a
  reset, and Vulkan's `sona_draw` records into the host's command buffer: there the host finds the loss (its own
  `glGetGraphicsResetStatus` or `VK_ERROR_DEVICE_LOST`) and calls `sona_renderer_lost`.

## When the old device may go

| Backend | Order |
|---|---|
| GL, GLES | `sona_renderer_lost` makes no GL call (the context may be gone), so drop the old context first, make the new one current, then `sona_renderer_reset` |
| Vulkan | Keep the old `VkDevice` until `sona_renderer_reset` returns: the reset destroys the renderer's pipelines, layouts and pools on it |
| D3D11 | Either order; the renderer holds its own references |
| D3D12 | After a removal, `sona_renderer_lost` releases every reference the renderer held to the removed device. D3D12 makes one device per adapter, so release your own references too before you create the next one, then `sona_renderer_reset` |
