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.
/* the device is gone: the draw said so */
if (sona_draw(r, &view, &target, NULL) == SONA_E_CONTEXT) {
/* every GPU object of the renderer forgotten */
sona_renderer_lost(r);
/* 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 */ }
}
// 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;
// every GPU object of the renderer forgotten
r.lost();
// make the new device (see below for when the old one may go)
// throws again if it fails: try later, or another device
r.reset(my_new_device());
}
// the device is gone: the draw said so
if (r.Draw(view, target) == SonaResult.Context)
{
// every GPU object of the renderer forgotten
r.Lost();
// 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 */ }
}
// the device is gone: the draw said so
if matches!(unsafe { r.draw(view, target, None) },
Err(ref e) if e.code == Code::CONTEXT) {
// every GPU object of the renderer forgotten
r.lost();
// 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 */ }
}
# 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 nextsona_drawuploads 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): thensona_draworsona_snapshotanswersSONA_E_CONTEXT, with aSONA_EVENT_ERRORevent. GL does not look for a reset, and Vulkan'ssona_drawrecords into the host's command buffer: there the host finds the loss (its ownglGetGraphicsResetStatusorVK_ERROR_DEVICE_LOST) and callssona_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 |
This page as Markdown · All the docs as one file (llms-full.txt)