Stacked on #313, which this depends on. Found while checking whether the 10-bit HDR path actually works now that a client can obtain an HDR swapchain (see #314). **It does** — verified end to end rather than from the format list: a client requesting `A2B10G10R10` + `HDR10_ST2084` produces ``` pix_fmt=yuv420p10le color_range=pc color_space=bt2020nc color_transfer=smpte2084 color_primaries=bt2020 ``` which is a correctly tagged HDR10 stream, and the first pixel-level HDR check here with 10-bit rather than 8-bit input. Three ways it could have gone wrong instead. ## Unrecognised formats defaulted to BGRA `vk_format_to_input_format` returned `BGRA` for anything it did not know, which reads a packed 10-bit or FP16 buffer as eight-bit channels. It now returns `None`, and the encode loop drops those frames with one log line per format. A stalled stream is a complaint. A stream at full frame rate carrying nonsense is not, and that is the failure this area keeps producing. ## Bit depth and input format had drifted apart They were two separate matches on the same `VkFormat`. `A2R10G10B10` counted as ten-bit in one and had no entry in the other, so it fell back to eight-bit BGRA — the encoder configured for ten bits while the converter read eight. Depth now derives from the input format, so that disagreement is unrepresentable. `A2R10G10B10` stays unmapped deliberately: a WSI layer offers it as one of its HDR pairs and the compositor dmabuf list advertises it, but the converter has no red-first 10-bit input, so there is nothing correct to map it to. ## The CPU fallback could not read either HDR format It read four bytes per pixel for every format and encoded eight-bit regardless, so a packed 10-bit buffer became garbage and an FP16 one was half an image of misread floats. It now refuses what it cannot read. ## Recorded, not fixed: the colour space we see is not always the one requested A FIXME at the point the value is read. A WSI layer rewrites `imageColorSpace` to `SRGB_NONLINEAR` before calling down — deliberately, since it carries the real colour space to the compositor out of band. We sit below it, so we read the rewrite. Measured, all three lines from one run: ``` [Gamescope WSI] ... colorspace: VK_COLOR_SPACE_HDR10_ST2084_EXT swapchain created — format=A2B10G10R10 colorspace=SRGB_NONLINEAR (re)init encoder: H265 Yuv420 Ten Bt709 → P010 ``` Ten-bit right, BT.709 wrong: PQ samples encoded and tagged as SDR. The same client *without* the layer gives `Ten Bt2020` and an smpte2084 stream, so this is specific to the layer path — which is the path Proton titles take. The fix cannot be local; the true colour space only exists in the compositor, which does receive it, so it needs a channel from there. Layer ordering is not a fix — we do not control it, and the non-layer path still needs the Vulkan value. Left out of this PR as a design change rather than a bug fix. ## Verification - 4 new tests, 12 total, all passing. - No new clippy warnings (diffed against the base branch). - 10-bit HDR path: unchanged, still `Ten Bt2020` / smpte2084. - SDR path: `verify-chain.sh` passes, 835 frames, 8-bit BGRA, brightness agreement 0.57. <!-- greptile_comment --> <h3>Greptile Summary</h3> This PR makes Vulkan format handling fail safely instead of interpreting unsupported swapchain buffers as BGRA. - Maps supported Vulkan formats to explicit converter inputs and derives bit depth from that mapping. - Drops unsupported GPU frames with rate-limited logging. - Rejects unsupported HDR formats in the eight-bit CPU fallback. - Documents the color-space limitation caused by rewritten WSI metadata. - Adds tests covering unsupported, eight-bit, 10-bit, and FP16 formats. <h3>Confidence Score: 5/5</h3> The PR appears safe to merge, with no new actionable issues introduced since the previous review. The changes since the previous review are empty, the sole previous finding was manually resolved after Greptile conceded it based on the stacked PR dependency, and the full PR introduces no confirmed rule violations or remaining correctness failures. <h3>Important Files Changed</h3> | Filename | Overview | |----------|----------| | apps/nescapture/src/encode.rs | Replaces unsafe BGRA fallback behavior with explicit format validation, consistent bit-depth derivation, guarded CPU fallback, and focused tests. | | apps/nescapture/src/swapchain.rs | Documents the known WSI color-space rewrite limitation at the point where swapchain metadata is recorded. | <h3>Flowchart</h3> ```mermaid %%{init: {'theme': 'neutral'}}%% flowchart TD A[Captured Vulkan frame] --> B{Known converter input?} B -->|No| C[Log format change and drop frame] B -->|Yes| D[Derive input format and bit depth] D --> E{DMA-BUF path available?} E -->|Yes| F[GPU color conversion and encoding] E -->|No| G{Eight-bit RGBA or BGRA?} G -->|Yes| H[CPU conversion and encoding] G -->|No| I[Return recoverable error] ``` <sub>Reviews (3): Last reviewed commit: ["docs(nescapture): the colour-space note ..."](https://github.com/nestrilabs/nestri/commit/7b05908e0a94739aa7ee5bd7fe5764a0a2b95071) | [Re-trigger Greptile](https://app.greptile.com/api/retrigger?id=60377562)</sub> **Context used:** - Knowledge Base — [Vulkan capture layer](https://app.greptile.com/nestri/-/custom-context/knowledge-base/nestrilabs/nestri/-/docs/capture-layer.md) <!-- /greptile_comment -->
nescapture
A Vulkan implicit layer that captures frames from inside the workload's own
process, encodes them with Vulkan Video on the GPU that drew them, and
sends them to neshub over a Unix socket — no copy out to the
CPU and back.
Being a layer rather than a screen-scraper is the whole point: the frame is already on the GPU when we get it, and it never leaves.
Where it sits
Game process
│ Vulkan calls
▼
┌──────────────────────────────────────────────┐
│ nescapture implicit layer │
│ │
│ vkCreateShaderModule → SHA-256 hash │
│ vkCreateGraphicsPipelines → track hashes │
│ vkCmdBindPipeline → detect HUD │
│ vkQueuePresentKHR → capture+encode │
└──────────────────────────────────────────────┘
│ GPU blit, same device
▼
final_image (DMA-BUF exportable)
│ get_dmabuf_fd(final_memory)
▼
DmaBufImporter (pixelforge VkDevice)
│ import_or_reuse() → vk::Image
▼
ColorConverter (GPU compute shader)
│ BGRA/RGB10/FP16 → NV12/P010/YUV444
▼
Encoder (Vulkan Video: H.264 / H.265 / AV1)
│ Annex-B packets
▼
Unix datagram → neshub → the client
CPU fallback exists only for driver configurations without DMA-BUF external memory export.
Sockets
| path | direction | carries |
|---|---|---|
/tmp/nestri-video.sock |
nescapture → neshub | encoded frames |
/tmp/nestri-stats.sock |
nescapture → neshub | capture fps, encode ms, drops |
/tmp/nescapture-cmd.sock |
neshub → nescapture | IDR requests, encode settings |
nescapture binds the command socket and connects to the other two. The stats
path is derived from NESCAPTURE_IPC_PATH's directory, so moving the video
socket moves both.
Quick start
# 1. Build
cargo build --release -p nescapture
# 2. Install the layer manifest
sudo cp apps/nescapture/manifest/VK_LAYER_nescapture.json /usr/share/vulkan/implicit_layer.d/
# Point the manifest's `library_path` at target/release/libnescapture_layer.so
# 3. Run something that draws
export NESCAPTURE_ENABLE=1
export NESCAPTURE_CODEC=h265
export NESCAPTURE_BITRATE=10000
export RUST_LOG=info
./my-vulkan-app
The layer is inert unless NESCAPTURE_ENABLE=1. That is deliberate — an
implicit layer is loaded into every Vulkan process on the system.
Environment variables
| Variable | Default | Description |
|---|---|---|
NESCAPTURE_ENABLE |
(unset) | Set to 1 to activate the layer. Nothing happens otherwise |
NESCAPTURE_IPC_PATH |
/tmp/nestri-video.sock |
Where to send encoded frames |
NESCAPTURE_CODEC |
best available | h264, h265 or av1; probes if unset |
NESCAPTURE_FORMAT |
yuv420 |
yuv420 or yuv444 |
NESCAPTURE_DEPTH |
auto | 8 or 10; inferred from the swapchain VkFormat if unset |
NESCAPTURE_BITRATE |
10000 |
CBR target in kbps. Ignored when NESCAPTURE_QP is set |
NESCAPTURE_QP |
(unset) | Constant QP instead of CBR |
NESCAPTURE_FPS |
60 |
Target frame rate |
NESCAPTURE_IDR_INTERVAL |
4 |
Force an IDR every N seconds |
NESCAPTURE_TUNE |
(unset) | highquality, lowlatency, ultralowlatency, lossless |
NESCAPTURE_CONFIG |
(unset) | Path to the per-app shader-hash TOML |
NESCAPTURE_GAME_NAME |
exe basename | Override app identification for that config |
NESCAPTURE_DISCOVER |
(unset) | Set to 1 to log every draw, for finding HUD shaders |
RUST_LOG |
error |
Standard env_logger filter, e.g. nescapture_layer=debug |
Everything else is decided at runtime: the client asks neshub for a codec or
bitrate change and it arrives on the command socket, so the encoder is
reconfigured without a restart.
Per-app shader-hash config
HUD detection needs to know which pipelines draw the HUD, and that is
per-application. Run once with NESCAPTURE_DISCOVER=1 to log every shader,
then pick out the HUD pipelines by the [SUSPECT] marker — blend on, depth
off, six vertices or fewer.
# ~/.config/nescapture/apps.toml
[game."MyApp.exe"]
hud_fragment_shaders = ["0xaabbccddeeff0011"]
hud_vertex_shaders = ["0x1a2b3c4d5e6f7890"]
skip_fragment_shaders = ["0x1122334455667788"]
export NESCAPTURE_CONFIG=~/.config/nescapture/apps.toml
export NESCAPTURE_GAME_NAME=MyApp.exe
Modules
src/
├── lib.rs entry points, dispatch routing
├── dispatch.rs Vulkan function-pointer types and tables
├── state.rs global DashMaps, DeviceState, CbState
├── instance.rs vkCreateInstance / vkDestroyInstance
├── device.rs vkCreateDevice / vkDestroyDevice
├── shader.rs SPIR-V hashing
├── pipeline.rs graphics pipeline tracking
├── framebuffer.rs image view and framebuffer tracking
├── commands.rs vkCmdBind*, vkCmdDraw*, vkCmdBeginRenderPass
├── swapchain.rs vkCreateSwapchainKHR, image enumeration
├── capture.rs GPU blit to the capture image, DMA-BUF export
├── present.rs vkQueuePresentKHR, encode dispatch
├── encode.rs pixelforge pipeline, codec probing, IPC send
├── dmabuf_import.rs cross-device zero-copy import
├── config.rs per-app TOML shader-hash config
└── discovery.rs draw-call logging for shader discovery
Dependencies
| Crate | Purpose |
|---|---|
pixelforge |
Vulkan Video hardware encode |
ash |
Vulkan bindings |
nesprotocol |
The IPC frame format neshub reads |
sha2, bytemuck |
SPIR-V fingerprinting |
dashmap, once_cell |
Lock-free concurrent state |
serde, toml |
Per-app shader config |
ash and pixelforge are pinned to git revisions — both track Vulkan Video
support that has not landed in a release.
Licence
Apache 2.0. See LICENSE.