Skip to content

NVENC GPU Selection

Video Capture SDK .NET Video Edit SDK .NET Media Blocks SDK .NET

By default an NVENC encoder runs on the NVIDIA GPU the encoder element picks. Pass the setting described here and the encoder runs on the GPU you name instead.

When to use it

A machine with more than one NVIDIA GPU needs it: a laptop with an integrated GPU and a discrete card, a workstation with two cards, or a server where the display GPU should not encode. Without the setting the encoder can land on either GPU, and the stream then competes with the desktop for the display adapter.

Choosing the GPU on Windows

NVENCBaseEncoderSettings.AdapterLuid takes the DXGI adapter LUID of the GPU. DXGIAdapterEnumerator.Enumerate() lists the adapters the machine has; filter the list to NVIDIA entries (VendorId 0x10DE) that are not the software adapter (IsSoftware false) and show them to the user.

using System.Linq;
using VisioForge.Core.MediaBlocks.VideoEncoders;
using VisioForge.Core.Types.X.VideoEncoders;
using VisioForge.Core.Windows;

// The NVIDIA adapters this machine has.
var nvidia = DXGIAdapterEnumerator.Enumerate()
    .Where(a => a.VendorId == 0x10DE && !a.IsSoftware)
    .ToList();

// Pin the H.264 encoder to the GPU the user picked (its Luid is what the encoder takes).
var settings = new NVENCH264EncoderSettings
{
    Bitrate = 5000,
    AdapterLuid = nvidia[0].Luid
};

var encoder = new H264EncoderBlock(settings);

The same property exists on NVENCHEVCEncoderSettings and NVENCAV1EncoderSettings; it comes from NVENCBaseEncoderSettings and applies to the Media Blocks pipeline, VideoCaptureCoreX, VideoEditCoreX and the RTSP server output alike.

With AdapterLuid the SDK also writes the CUDA device id that CUDA itself reports for that adapter (cuDeviceGetLuid): the SDK delivers frames in system memory, and the auto-GPU element encodes that input through its CUDA device, so both values are needed to pin the session. The mapping comes from the CUDA driver, not from the DXGI enumeration order, so it stays correct on a machine with two NVIDIA cards. If both AdapterLuid and CudaDeviceId are set, AdapterLuid wins.

Choosing the GPU by CUDA device id

NVENCBaseEncoderSettings.CudaDeviceId takes the CUDA device id instead and works on Windows and Linux. On Linux it is the only option: AdapterLuid is a Windows property, and a Linux session that sets it fails to start with an error rather than encoding on another GPU.

var settings = new NVENCH264EncoderSettings
{
    CudaDeviceId = 1
};

What happens when the GPU cannot be applied

With a GPU set, the encoder runs through the auto-GPU NVENC element (nvautogpuh264enc, nvautogpuh265enc or nvautogpuav1enc) instead of the plain nvh264enc family, because only those take the property. The SDK reads the value back from the element: when the element keeps its own value - an LUID or device id that does not belong to a GPU on this machine, a GStreamer build without the auto-GPU elements, or AdapterLuid on a non-Windows system - the pipeline fails to start with an error naming the requested value and what the element kept. It never falls back to another GPU in silence.

Without either property nothing changes: the SDK keeps using the plain element, exactly as in earlier versions.

Limitations

  • AdapterLuid is Windows-only; CudaDeviceId works on Windows and Linux.
  • The GStreamer NVENC plugin registers the auto-GPU elements on Windows and Linux alike (gst-plugins-bad, sys/nvcodec/plugin.c): H.264 and HEVC since 1.22, AV1 since 1.26. The redistributed runtimes have them; an older plugin makes the encoder report that it is not available.
  • AdapterLuid is resolved to the CUDA device id by the CUDA driver (cuDeviceGetLuid), not by the DXGI enumeration order, so two NVIDIA cards cannot be confused. When nvcuda.dll is absent or no CUDA device has the LUID, the pipeline refuses to start instead of guessing.
  • This page covers NVENC only. The AMD AMF, Intel QSV, Media Foundation and D3D12 encoders pin a GPU by per-device element name (amfh264device1enc, qsvh264device1enc, ...), not by a settings property.