# Vulkan: optional features for cheap frames

`sona_renderer_vk_features` tells the Vulkan backend which optional device features the host enabled. Vulkan has
no call that asks a `VkDevice` what it was created with, so the backend cannot find out by itself, and using a
feature that was not enabled is invalid even where the driver allows it. The features are the host's own `VkDevice`'s,
so the code below is C; C++ uses it as it is, Rust and Python make the call through their raw layers
(`sona_sys::sona_renderer_vk_features`, `sona._native`), and C#'s renderer keeps its handle, so C# cannot make it.

- **`SONA_VK_FEATURE_BINDLESS`** needs `shaderSampledImageArrayDynamicIndexing` enabled on the device. Every texture
  sits in one image table, the draws read their palette rows, material and textures by index, and cloth, skin,
  reflective and reflective-skin are one pipeline. A pass binds one descriptor set and one pipeline, then only draws.
- **`SONA_VK_FEATURE_MULTI_DRAW`** (with BINDLESS) needs `multiDrawIndirect` and `drawIndirectFirstInstance`. Each run
  of draws is one `vkCmdDrawIndexedIndirect`. A frame of 1,000 different avatars is a handful of draw calls. On Intel
  GPUs it is left off and the bindless draws go one by one: there its indirect draws read the wrong textures and could
  hang the GPU.

The picture is the same byte for byte with either flag, both or neither, shadows and the editor's AO included.
Without the call, or with 0, the backend binds a descriptor set per draw; those sets are kept across frames.

## Turning them on

```c
VkPhysicalDeviceFeatures have, want = {0};
vkGetPhysicalDeviceFeatures(physical_device, &have);
uint32_t sona_features = 0;
if (have.shaderSampledImageArrayDynamicIndexing) {
  want.shaderSampledImageArrayDynamicIndexing = VK_TRUE;
  sona_features |= SONA_VK_FEATURE_BINDLESS;
  if (have.multiDrawIndirect && have.drawIndirectFirstInstance) {
    want.multiDrawIndirect = want.drawIndirectFirstInstance = VK_TRUE;
    sona_features |= SONA_VK_FEATURE_MULTI_DRAW;
  }
}
/* ... VkDeviceCreateInfo.pEnabledFeatures = &want (or VkPhysicalDeviceFeatures2.features), vkCreateDevice ... */

sona_renderer_create(ctx, &dev, &renderer);
sona_renderer_vk_features(renderer, sona_features);   /* from the next frame */
```

Engines that already enable these features (most do) only make the last call. Call it again after
`sona_renderer_reset`: the new device may not have them. It answers `SONA_E_UNSUPPORTED` when the physical device
lacks a feature (nothing changes) and on other backends.

## What it costs and saves

The crowd scene (1,000 avatars, each its own random outfit and body) on an RTX 4090 machine, `sona-bench` with
`SONA_BENCH_VK_FEATURES=0` for the per-draw path, the median of three runs each:

| Path | CPU ms a frame | ms a frame | Draws a frame |
|---|---|---|---|
| Per-draw path (sets kept across frames, pooled geometry) | 6.5 | 16.66 | 9,470 |
| BINDLESS and MULTI_DRAW | 3.48 | 16.15 | 9,470 |

The CPU column is the saving: the same draws, prepared and recorded with far less work. On this machine the GPU time is
the whole frame either way.

Memory: one descriptor set per frame slot holds the image table, sized from the device's limits (a limit of 65,535
textures); per draw a frame, draw data with a size set to 16 bytes and an indirect command of 20.

## When a frame falls back

A frame is drawn the per-draw way when one of its textures is not in the table (the table is full), a material
lives outside the first params buffer (past the 1 GiB limit of materials), or a buffer the table binds whole (the params
buffer, the frame's constants, the draw data) is past the device's `maxStorageBufferRange` (128 MiB at the least).
Freed table entries are reclaimed on either path. Sprite sheets and `frame_region` targets
are never in the table: crowd sprites draw with their own pipeline in between.
