feat(host/windows): NVENC D3D11 hardware encoder (--features nvenc)

Zero-copy capture->encode on the GPU via the raw NVENC API (nvidia_video_codec_sdk sys + ENCODE_API; the safe wrapper is CUDA-only). Opens an NV_ENC_DEVICE_TYPE_DIRECTX session on the SAME ID3D11Device as the DXGI capturer (carried on the new FramePayload::D3d11), registers a pool of BGRA textures once, CopyResources each captured texture in and encode_picture; CBR/ULL, infinite GOP, P-only, forced-IDR for RFI. The DXGI capturer gains a D3D11 zero-copy output (selected, like the encoder, by PUNKTFUNK_ENCODER=nvenc) so capture+encode share textures.

OFF by default (the nvenc feature pulls the NVENC SDK + cudarc): the default Windows host links without it (openh264 path). cudarc builds toolkit-less via the SDK ci-check feature (dynamic-loading). At link time --features nvenc needs nvencodeapi.lib (NVENC SDK, or an import lib generated from the driver's nvEncodeAPI64.dll) on PUNKTFUNK_NVENC_LIB_DIR. Both default and --features nvenc builds validated to compile+link GPU-less on the VM (import lib generated from the driver DLL). Runtime needs a real NVIDIA GPU.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-15 01:39:46 +00:00
parent 0aff2a8d5e
commit 3d3b9e8a5f
6 changed files with 498 additions and 9 deletions
+18 -5
View File
@@ -162,15 +162,26 @@ pub fn open_video(
.unwrap_or_default()
.to_ascii_lowercase();
if matches!(pref.as_str(), "nvenc" | "hw" | "nvidia") {
anyhow::bail!(
"NVENC hardware encode is not yet implemented on Windows — omit PUNKTFUNK_ENCODER \
or set it to 'software' to use the openh264 encoder"
);
// Hardware path: NVENC over D3D11. The DXGI capturer switches to its zero-copy
// FramePayload::D3d11 output under the same env var so capture + encode share textures.
#[cfg(feature = "nvenc")]
{
let enc =
nvenc::NvencD3d11Encoder::open(codec, format, width, height, fps, bitrate_bps)?;
return Ok(Box::new(enc) as Box<dyn Encoder>);
}
#[cfg(not(feature = "nvenc"))]
{
anyhow::bail!(
"NVENC requested but this host was built without it — rebuild with \
`--features nvenc` (needs the NVENC SDK's nvencodeapi.lib at link time)"
);
}
}
anyhow::ensure!(
codec == Codec::H264,
"the Windows software encoder supports H.264 only; client negotiated {codec:?} \
(request H264, or use a GPU host once NVENC lands)"
(set PUNKTFUNK_ENCODER=nvenc for a GPU host, or request H264)"
);
// Software H.264 realistically caps far below the negotiated hardware rates.
const SW_BITRATE_CEIL: u64 = 100_000_000;
@@ -189,6 +200,8 @@ pub fn open_video(
mod linux;
#[cfg(target_os = "windows")]
mod sw;
#[cfg(all(target_os = "windows", feature = "nvenc"))]
mod nvenc;
#[cfg(test)]
mod tests {