# Lighting and shadows

By default an avatar carries its own fixed look: the original irradiance cube, a highlight that never moves and a
small self-shadow from a light tied to the camera. `sona_lighting` puts it in the host's scene instead: lit by its sun
and its lamps, under its sky, shadowing its ground and shadowed by its buildings. It is set per renderer and per view.

![Avatars lit by the host's lights, with shadows](../img/lighting.webp)

- **Lights.** A key and a fill (directional, colour and intensity each), an ambient colour, and a scale on the
  environment light.
- **Environment probe.** The built-in irradiance cube, or the host's environment as nine spherical harmonics (SH)
  or as a radiance cube, which the SDK projects onto the nine and convolves with the cosine lobe.
- **Our shadow map.** The key light's map of the avatars, fitted to the avatars drawn (or to a sphere the host
  names, or drawn through the host's own light matrix). The avatars receive it (they shadow each other and
  themselves), and the host reads it to shadow its own scene with them.
- **The host's shadow map.** Any depth texture the host already has, looked up with its own matrix
  (orthographic or perspective): the avatars receive its shadows.
- **A ground shadow** for a host with no shadow maps: a soft dark shadow on the plane `y = ground_y`, blended into
  the picture under the avatars.
- **Proxy casters** for crowds: the avatars cast through a few prisms on their bones instead of their meshes, which
  takes the shadow pass from most of a second main pass down to about nothing.

With nothing set, every picture is exactly what it was, byte for byte (the same programs, the same frame).

The sample is `samples/c/lighting_scene.c`.

## Using it

```c
sona_lighting l;
sona_lighting_defaults(&l);                  /* key and fill off, the built-in environment, a fitted 2048 map */
l.key.direction[0] = 0.5f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;   /* the way the light travels */
l.key.color[0] = 1.0f; l.key.color[1] = 0.93f; l.key.color[2] = 0.84f;
l.key.intensity = 0.9f;
l.fill.intensity = 0.2f;                     /* fill: default direction, white */
l.environment = 0.6f;                        /* the cube's light, scaled */
l.flags = SONA_LIGHTING_GROUND;              /* and a shadow on y = 0 */
sona_renderer_set_lighting(r, &l);           /* every draw and snapshot of r from now on */
/* ... */
sona_renderer_set_lighting(r, NULL);         /* the original look again */
```

```cpp
sona_lighting l;
sona_lighting_defaults(&l);                  // key and fill off, the built-in environment, a fitted 2048 map
l.key.direction[0] = 0.5f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;   // the way the light travels
l.key.color[0] = 1.0f; l.key.color[1] = 0.93f; l.key.color[2] = 0.84f;
l.key.intensity = 0.9f;
l.fill.intensity = 0.2f;                     // fill: default direction, white
l.environment = 0.6f;                        // the cube's light, scaled
l.flags = SONA_LIGHTING_GROUND;              // and a shadow on y = 0
sona_renderer_set_lighting(r.raw(), &l);     // every draw and snapshot of r from now on
// ...
sona_renderer_set_lighting(r.raw(), nullptr);   // the original look again
```

```csharp
var l = SonaRenderer.LightingDefaults();     // key and fill off, the built-in environment, a fitted 2048 map
l.key.direction[0] = 0.5f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;   // the way the light travels
l.key.color[0] = 1.0f; l.key.color[1] = 0.93f; l.key.color[2] = 0.84f;
l.key.intensity = 0.9f;
l.fill.intensity = 0.2f;                     // fill: default direction, white
l.environment = 0.6f;                        // the cube's light, scaled
l.flags = Native.SONA_LIGHTING_GROUND;       // and a shadow on y = 0
r.SetLighting(l);                            // every draw and snapshot of r from now on
// ...
r.ClearLighting();                           // the original look again
```

```rust
let mut l = lighting_defaults();             // key and fill off, the built-in environment, a fitted 2048 map
l.key.direction = [0.5, -1.0, 0.6];          // the way the light travels
l.key.color = [1.0, 0.93, 0.84];
l.key.intensity = 0.9;
l.fill.intensity = 0.2;                      // fill: default direction, white
l.environment = 0.6;                         // the cube's light, scaled
l.flags = sona_sys::SONA_LIGHTING_GROUND;    // and a shadow on y = 0
r.set_lighting(Some(&l))?;                   // every draw and snapshot of r from now on
// ...
r.set_lighting(None)?;                       // the original look again
```

```python
h = sona.Headless(ctx)                       # Python draws on the library's own device
l = sona.lighting_defaults()                 # key and fill off, the built-in environment, a fitted 2048 map
l.key.direction[:] = (0.5, -1.0, 0.6)        # the way the light travels
l.key.color[:] = (1.0, 0.93, 0.84)
l.key.intensity = 0.9
l.fill.intensity = 0.2                       # fill: default direction, white
l.environment = 0.6                          # the cube's light, scaled
l.flags = 1                                  # SONA_LIGHTING_GROUND: and a shadow on y = 0
h.set_lighting(l)                            # every picture h makes from now on
# ...
h.set_lighting(None)                         # the original look again
```

`sona_draw_lit` and `sona_snapshot_lit` take a lighting for one frame without touching the renderer's own: one per
view of a split screen, or a preview lit differently from the game. Headless pictures, sprite sheets and stickers draw
through `sona_snapshot`, so they take the renderer's lighting (`sona_headless_renderer` gives the renderer).

World space is the SDK's own (y up, metres). Colours are linear RGB. A light's `direction` is the way the light
travels (a noon sun is `0, -1, 0`); it does not need to be unit length. An intensity of 0 turns a light off.

### On the web

The web SDK draws the original look. Its one light setting is the key light's shadow on the avatar itself,
`shadow` when the scene is made, and a property to change later:

```js
import { SonaRenderer } from './sona-renderer.js';

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas, shadow: true });
const avatar = sona.add(manifestBase64, { clip: 'GenericStand5' });
await avatar.ready;
sona.camera.preset('bust', avatar);
sona.shadow = false;   // from the next frame
sona.shadow = true;
```
### The light model

Per pixel, for an avatar's albedo `a` and normal `n` (`s` is 1 lit, 0 fully shadowed):

```text
light  = environment(n) * envGain * (1 - shadow_ambient * (1 - s))
       + ambient
       + key.color * key.intensity * max(n . -key.dir, 0) * (1 - shadow_strength * (1 - s))
       + fill.color * fill.intensity * max(n . -fill.dir, 0)
colour = a * light * ao + rim * materialRim + highlight
```

`environment(n)` is the built-in cube (looked up with the camera-relative normal, as the original does) or the host's
SH (looked up with the world normal). Skin takes `skin_red` (default 1.2) times more red from it, as the original
cube's alpha channel does. The highlight is the original's `dot(h, n)^8` lobe: with the key on, its half vector and
colour are the key's and the key's shadow takes it away; with the key off it is the original's fixed one.
`sona_lighting_defaults` with the key off and no shadow computes what the original shaders compute, to within one
level on every pixel, on every backend.

| Field | Default | Meaning |
|---|---|---|
| `key`, `fill` | intensity 0, white | `direction`, `color`, `intensity` |
| `ambient` | 0, 0, 0 | added everywhere |
| `environment` | 1 | scales the environment light |
| `specular` | 1 | the highlight |
| `rim` | 1 | the materials' rim light |
| `shadow` | `SONA_SHADOW_FIT` | `NONE`, `FIT`, `SPHERE`, `MATRIX` |
| `shadow_size` | 2048 | 512, 1024, 2048 or 4096 texels a side |
| `shadow_strength` | 1 | the share of the key a shadow takes |
| `shadow_ambient` | 0.5 | the share of the environment a shadow takes (the original's) |
| `shadow_bias` | 0.03 m | toward the light |
| `shadow_center`, `shadow_radius` | 0, 10 | `SONA_SHADOW_SPHERE` |
| `shadow_matrix` | identity | `SONA_SHADOW_MATRIX` |
| `ground_y`, `ground_opacity` | 0, 0.6 | `SONA_LIGHTING_GROUND` |

`sona_renderer_set_lighting` refuses (`SONA_E_ARGUMENT`, with the reason in `sona_last_error`) a wrong `size`, an
unknown flag or mode, a negative or non-finite colour or intensity, shares outside 0 to 1, a shadow size not in the
list, a zero key direction with shadows on, a sphere of radius 0, a matrix that is not orthographic or is singular,
and non-zero `reserved`. NULL is always accepted. Every backend has the lit programs; the host's map and ours have
no Metal handle, so there `sona_renderer_set_host_shadow` and `sona_renderer_shadow_map` return `SONA_E_UNSUPPORTED`
(see Backends).

## The environment

```c
sona_environment e;
sona_environment_defaults(&e);
e.kind = SONA_ENV_SH9;                       /* or SONA_ENV_CUBE with cube_size and cube */
memcpy(e.sh, my_sh, sizeof e.sh);            /* 9 x RGB, radiance */
sona_renderer_set_environment(r, &e);        /* NULL: the built-in cube */
```

```cpp
sona_environment e;
sona_environment_defaults(&e);
e.kind = SONA_ENV_SH9;                          // or SONA_ENV_CUBE with cube_size and cube
std::memcpy(e.sh, my_sh, sizeof e.sh);          // 9 x RGB, radiance
sona_renderer_set_environment(r.raw(), &e);     // nullptr: the built-in cube
```

```csharp
r.SetEnvironmentSh(mySh);                       // 9 x RGB, radiance; null: the built-in cube
```

```rust
// the crate has no environment wrapper: the C calls through sona_sys
let my_sh = [0.743f32; 27];                   // 9 x RGB, radiance: here a uniform sky
let mut e: sona_sys::sona_environment = unsafe { std::mem::zeroed() };
unsafe { sona_sys::sona_environment_defaults(&mut e) };
e.kind = sona_sys::SONA_ENV_SH9;              // or SONA_ENV_CUBE with cube_size and cube
e.sh = my_sh;
unsafe { sona_sys::sona_renderer_set_environment(r.raw(), &e) };   // null: the built-in cube
```

```python
h = sona.Headless(ctx)                          # Python draws on the library's own device
h.set_environment_sh(my_sh)                     # 9 x RGB, radiance; None: the built-in cube
```

SH coefficients are radiance in this basis of the world direction `(x, y, z)`, y up, in this order: `0.282095`,
`0.488603 y`, `0.488603 z`, `0.488603 x`, `1.092548 xy`, `1.092548 yz`, `0.315392 (3z^2 - 1)`, `1.092548 xz`,
`0.546274 (x^2 - y^2)`. Engines differ in their order and constants (Unity's `SphericalHarmonicsL2`, Unreal's
sky light, three.js `SphericalHarmonics3`); reorder to this one. A cube is radiance, six faces `+X -X +Y -Y +Z -Z` of
`cube_size` squared RGB floats, rows from the top in the D3D and GL face layout; each texel counts by its solid angle.
A uniform radiance of 0.743 lights about as the built-in cube does (its mean value).

`sona_environment_irradiance` returns the convolved coefficients the shader evaluates (divided by pi, so that a
uniform radiance comes back as itself): a host can light its own scene with exactly the same function.

The environment is used only while a lighting is set; with none, the built-in cube is always used.

## Shadows

The key light casts. Every avatar drawn as a mesh draws into our shadow map in a depth pass before the main pass, and
every lit avatar looks itself up in it (four bilinear compare taps). Where the map sits:

- `SONA_SHADOW_FIT` (default): around the avatars drawn this frame, refitted every draw. The box is square across
  the light, sized in steps of an eighth of an octave and snapped to whole texels, so a still scene keeps its
  texels and a crowd that walks does not shimmer. A crowd of 1,000 over a 40 m square gets about 2 cm texels at 2048.
- `SONA_SHADOW_SPHERE`: around `shadow_center`, `shadow_radius`: the same texels whatever moves, for a fixed arena.
- `SONA_SHADOW_MATRIX`: the host's own light, as a row-vector matrix taking a world point to `x, y` in -1..1 across
  the map and `z` in 0..1 (orthographic: last column `0, 0, 0, 1`). Use the host's light view times its projection,
  with the depth range mapped to 0..1. Our map then lines up with the host's.
- `SONA_SHADOW_NONE`: no map, no pass.

`SONA_DRAW_SHADOW` (the original self-shadow) is ignored while a lighting is set: the `shadow` field decides.

### Shadowing the host's scene with the avatars

```c
sona_shadow_map m;
m.size = sizeof m;
if (sona_renderer_shadow_map(r, &m) == SONA_OK) {
  /* m.matrix: world point (row vector) to (u, v, depth, 1); m.u.vk.view etc: the depth texture to sample */
}
```

```cpp
sona_shadow_map m;
m.size = sizeof m;
if (sona_renderer_shadow_map(r.raw(), &m) == SONA_OK) {
  // m.matrix: world point (row vector) to (u, v, depth, 1); m.u.vk.view etc: the depth texture to sample
}
```

```csharp
if (r.ShadowMap is Native.sona_shadow_map m)
{
    // m.matrix: world point (row vector) to (u, v, depth, 1); m.u.vk.view etc: the depth texture to sample
}
```

```rust
if let Some(m) = r.shadow_map()? {
    // m.matrix: world point (row vector) to (u, v, depth, 1); m.u.vk.view etc: the depth texture to sample
}
```

```python
h = sona.Headless(ctx)                     # Python draws on the library's own device
m = h.shadow_map()                         # None before a lit frame with shadows; else the matrix and the texture
```

After a lit frame with shadows, the host samples `m`'s texture at `(u, v)` and compares `depth` (lit where the
reference is below what is stored) in its own ground and wall shaders. `texel_metres` sizes a filter; `frame` is the
index of the last frame that drew the map (a frame the backend refused leaves the earlier one). The texture is
the renderer's and is drawn again by its next lit frame: Vulkan and D3D12 hosts read it after that frame's commands
(in the same command buffer, or once the frame is done). It is left in `DEPTH_STENCIL_READ_ONLY_OPTIMAL` (Vulkan),
`PIXEL_SHADER_RESOURCE` (D3D12, its SRV format `R24_UNORM_X8_TYPELESS`), and is a `GL_DEPTH_COMPONENT24` texture on GL
and an `R24G8_TYPELESS` texture with an SRV on D3D11.

### The host's shadows on the avatars

```c
sona_host_shadow h;
memset(&h, 0, sizeof h);
h.size = sizeof h;
memcpy(h.matrix, my_light_view_proj_to_uv, sizeof h.matrix);   /* world point to (u, v, depth, w) */
h.bias = 0.0015f;
h.width = h.height = 4096;
h.u.vk.view = my_depth_view;                 /* .gl.texture, .d3d11.srv, .d3d12.resource + .srv_format */
h.u.vk.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
sona_renderer_set_host_shadow(r, &h);
```

```cpp
sona_host_shadow h;
std::memset(&h, 0, sizeof h);
h.size = sizeof h;
std::memcpy(h.matrix, my_light_view_proj_to_uv, sizeof h.matrix);   // world point to (u, v, depth, w)
h.bias = 0.0015f;
h.width = h.height = 4096;
h.u.vk.view = my_depth_view;                    // .gl.texture, .d3d11.srv, .d3d12.resource + .srv_format
h.u.vk.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
sona_renderer_set_host_shadow(r.raw(), &h);
```

```csharp
var myLightViewProjToUv = new float[16];        // world point to (u, v, depth, w)
var myDepthView = IntPtr.Zero;
var h = new Native.sona_host_shadow();
h.size = (uint)sizeof(Native.sona_host_shadow);
for (var i = 0; i < 16; i++) h.matrix[i] = myLightViewProjToUv[i];
h.bias = 0.0015f;
h.width = h.height = 4096;
h.u.vk.view = myDepthView;                      // .gl.texture, .d3d11.srv, .d3d12.resource + .srv_format
h.u.vk.layout = 1000117000;                     // VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
r.SetHostShadow(h);                             // r.ClearHostShadow(): none
```

```rust
let my_light_view_proj_to_uv = [0.0f32; 16];   // world point to (u, v, depth, w)
let my_depth_view: *mut std::ffi::c_void = std::ptr::null_mut();
let mut h: sona_sys::sona_host_shadow = unsafe { std::mem::zeroed() };
h.size = std::mem::size_of::<sona_sys::sona_host_shadow>() as u32;
h.matrix = my_light_view_proj_to_uv;
h.bias = 0.0015;
h.width = 4096;
h.height = 4096;
h.u.vk = sona_sys::sona_host_shadow_vk { view: my_depth_view, layout: 1000117000 };   // VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
unsafe { r.set_host_shadow(Some(&h))? };
```

```python
# Python draws on the library's own device (sona.Headless), which has no host scene to take a shadow map from.
```

The lit avatars look the host's map up with its own matrix (divided by `w`, so a spot light's perspective map works
too) and take the darker of it and their own map. The matrix gives the texture coordinates the host samples with:
for a D3D-style light projection, `u = x * 0.5 + 0.5` and `v = 0.5 - y * 0.5`. Depth is 0 near; a reversed-Z map
needs its matrix to give `1 - z` and the texture flipped likewise, or our own map instead. The host keeps the texture
alive and in the stated state while frames that use it run. Call `sona_renderer_set_host_shadow` again whenever the
texture or its view is made anew (a resize, a quality change), even when the new handle has the old value: each
call rebinds it on every path, the bindless one too. On Vulkan, set it once and keep the view otherwise: each new
view (each call) makes the per-draw descriptor sets anew.

To have the avatars both cast into the host's world and receive it, give the host's light matrix as
`SONA_SHADOW_MATRIX`, sample our map in the host's shaders (above) and pass the host's map here.

### Proxy casters

`SONA_LIGHTING_PROXY_CASTERS` keeps everything above but changes what draws into our map: instead of every mesh of
every avatar (the same skinning as the main pass, over all of each avatar's vertices), each avatar casts through 17
hexagonal prisms on its bones (hips to head, the legs to the toes, the arms to the hands; 340 triangles), four
avatars to a draw. The prisms follow the pose exactly (they ride the joints the frame was posed with), so walking,
sitting and waving crowds cast the right shape; what they lose is the detail: hair, hats, skirts, held props and
loose clothing cast nothing beyond the body, and the shadow of a limb is a rounded bar. The avatars still receive
every shadow (each other's, the host's), shadowed by the prisms; on a lit side the result matches the meshes'
closely, because the map holds the prisms' far sides.

```c
sona_lighting_defaults(&l);
l.key.direction[0] = 0.4f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;
l.key.intensity = 0.9f;
l.flags = SONA_LIGHTING_GROUND | SONA_LIGHTING_PROXY_CASTERS;   /* a crowd's ground shadows for almost nothing */
sona_renderer_set_lighting(r, &l);
```

```cpp
sona_lighting l;
sona_lighting_defaults(&l);
l.key.direction[0] = 0.4f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;
l.key.intensity = 0.9f;
l.flags = SONA_LIGHTING_GROUND | SONA_LIGHTING_PROXY_CASTERS;   // a crowd's ground shadows for almost nothing
sona_renderer_set_lighting(r.raw(), &l);
```

```csharp
var l = SonaRenderer.LightingDefaults();
l.key.direction[0] = 0.4f; l.key.direction[1] = -1.0f; l.key.direction[2] = 0.6f;
l.key.intensity = 0.9f;
l.flags = Native.SONA_LIGHTING_GROUND | Native.SONA_LIGHTING_PROXY_CASTERS;   // a crowd's ground shadows for almost nothing
r.SetLighting(l);
```

```rust
let mut l = lighting_defaults();
l.key.direction = [0.4, -1.0, 0.6];
l.key.intensity = 0.9;
l.flags = sona_sys::SONA_LIGHTING_GROUND | sona_sys::SONA_LIGHTING_PROXY_CASTERS;   // a crowd's ground shadows for almost nothing
r.set_lighting(Some(&l))?;
```

```python
h = sona.Headless(ctx)                          # Python draws on the library's own device
l = sona.lighting_defaults()
l.key.direction[:] = (0.4, -1.0, 0.6)
l.key.intensity = 0.9
l.flags = 1 | 2                                 # SONA_LIGHTING_GROUND | SONA_LIGHTING_PROXY_CASTERS: a crowd's ground shadows for almost nothing
h.set_lighting(l)
```

The prisms are sized from the first caster's rest pose and built once per renderer (one small vertex and index
buffer); each frame adds one cb block per four casters. Their ground shadow covers most of what the meshes' shadow
covers and lies almost wholly inside it, and the figures draw as they do without them.

### A crowd's shadows

With neither flag, a frame with fewer than 64 casters casts every mesh. Past 64, each avatar smaller than a quarter
of the picture's height casts through the prisms above and the nearer ones keep their meshes, so a crowd costs about
what proxy casters cost while the avatars close to the camera keep their full shadows. A near avatar keeps every mesh
it draws: its body, its playing props, its swaying meshes and the props attached to it (3.32); loading placeholders
keep theirs too. The shadow pass then draws those opaque meshes first, then the prisms of the rest.
`SONA_LIGHTING_MESH_CASTERS` keeps every mesh casting at any size;
`SONA_LIGHTING_PROXY_CASTERS` turns every caster to prisms. The two together are `SONA_E_ARGUMENT`.

The shadow pass gets exactly the near pair's opaque draws, both rows of boxes among them, no far mesh and no
tetrahedron, and prisms for the rest; with `MESH_CASTERS` no prisms, with `PROXY_CASTERS` no meshes. What the shadows
cost is under [Cost](#cost).

### The ground shadow

`SONA_LIGHTING_GROUND` draws one quad on the plane `y = ground_y` across our map's footprint, after the opaque
avatars and before the transparent ones, depth tested against them: black, as opaque as `ground_opacity` where the
key is fully blocked, softened by a five-tap filter and faded at the map's edge. Over a transparent clear the picture
then carries the shadow as premultiplied black with alpha, so compositing it over the host's own background lays
the shadow on the host's ground. It needs our map (`shadow` not `NONE`) and a key that is not horizontal.

## Backends

Every backend draws the lights, the environment, our shadow map and the ground; all but Metal take the host's
shadow map.

- Vulkan: per-draw and bindless/multi-draw paths, every MSAA count.
- GL 3.3 and GLES 3.0, D3D11, D3D12: all of it.
- Xenos: our map is capped at 1024 texels a side, so its colour and depth fit in the EDRAM. The host's map is
  `u.xenos.surface`, a `struct XenosSurface *` in k_8_8_8_8 with the depth packed as ours
  (`depth = r + g / 255 + b / 65025`), both ways. Drawn on a software model of the GPU
  ([platforms](../platforms.md#xbox-360-homebrew)).
- Metal: no host shadow map (`SONA_E_UNSUPPORTED` from `sona_renderer_set_host_shadow` and
  `sona_renderer_shadow_map`). Built; see [platforms](../platforms.md#macos-and-ios).

The lit programs are new optional programs past the public set (`SONA_PROG_LIT_*`, `SONA_PROG_GROUND` in
`sona_gpu.h`); the original programs and every original picture are untouched. PassParams grows by 34 vec4 of light
rows only for them (the GL text reads them as the loose uniform `sonaLight`). Lit frames use a second depth target
(`shadow_size` a side) beside the original 1,024 one.

## Cost

The crowd bench (`sona-bench --scene crowd --outfits random` over the full corpus and catalogue, 1,000 avatars, 1920 x
1080, Vulkan bindless and multi-draw) on an RTX 4090 machine, `--lighting` 0: none; 3: a key and a fill with no
shadow; 1: the same with the fitted 2048 map, the meshes casting; 2: that and the ground shadow; 4 and 5: 1 and 2 with
proxy casters. Each row is the median of three runs.

| `--lighting` | ms a frame (CPU, GPU) |
|---|---|
| 0, none | 16.15 (3.48, 16.13) |
| 3, lights, no shadow | 16.17 (3.59, 16.16) |
| 1, key shadows, meshes | 16.27 (3.85, 16.26) |
| 2, and the ground, meshes | 16.28 (3.91, 16.25) |
| 4, key shadows, proxies | 16.29 (3.75, 16.27) |
| 5, and the ground, proxies | 16.26 (4.07, 16.25) |

What it says:
- With nothing set the frame costs what it did (the same programs, the same frame).
- On this machine the GPU time is the whole frame in every row, and the lights and the shadows hardly move it: the
  shadow pass is a depth-only pass over the casting meshes (the same skinning as the main pass) into the 2048 map,
  and the frame stays on the bindless and multi-draw path.
- Proxy casters draw prisms in place of the far meshes in the shadow pass; the ground shadow is one quad.

For a crowd: proxy casters. For more: a 1024 map, `SONA_SHADOW_SPHERE` around the area that matters, crowd sprites
for the far avatars (which cast none), or shadows off for a far camera.

Crowd sprites (`sona_lod_set`) keep the look they were baked with and cast no shadow; an avatar culled out of the
view casts no shadow into it.

## Limits

- A [styled](styles.md) avatar keeps its style under a lighting: its toon bands are not lit by the fill,
  the ambient or the environment. Loading placeholders' figures are lit, but with proxy casters they cast nothing.
- No point or spot light of the SDK's own: the host's spot map is read, and the SDK's map is the key light's,
  directional.
- No cascades of the SDK's map for very large worlds: `SONA_SHADOW_MATRIX` per view, or `SPHERE`, covers a region.
- On Metal neither the host's map nor the SDK's has a handle.
- Reversed-Z host maps need the matrix and the texture flipped.
- Proxy casters are sized from the first caster's rest pose (every avatar gets the same prisms, on its own
  joints); hair, hats, skirts and held props cast nothing through them. The CPU cost in very large scenes (Cost).
