RI::listGpus¶
What it does¶
RI::listGpus() returns the GPUs in this computer that can run RI, as a list of plain
RI::Gpu descriptions: name, vendor, kind (discrete, integrated, software),
PCI IDs, memory size, and an id that is stable within the current process.
You call it first, pick one GPU from the list, and pass it to
RI::createDevice. RI does not choose for you: picking "the best" GPU
automatically is a helper's job, not RI's.
Step by step, listGpus:
- opens a temporary native API handle (a Vulkan instance with no layers or extensions, or a DXGI factory on D3D12);
- asks for every GPU in the order the Vulkan driver or DXGI returns them;
- keeps only the GPUs that meet RI's requirements (listed below) and fills in an
RI::Gpudescription for each one; - closes the temporary instance or factory before returning, so nothing stays alive between calls.
There is no "instance" object to create first: listing needs nothing from you.
Signature¶
- D3D12 header: The GPUs that can run RI (feature level 12_0, shader model 6.6, resource binding tier 3, resource heap tier 2, enhanced barriers), in DXGI's order. Checking them creates a short-lived device per adapter. Picking one automatically is a helper (P60).
- Vulkan header: The GPUs that can run RI (Vulkan 1.3 and a graphics + compute queue), in the driver's order. Picking one automatically is a helper (P60).
Example¶
const std::vector<RI::Gpu> gpus = RI::listGpus();
for (const RI::Gpu &gpu : gpus) {
std::printf("%s (%llu MiB)\n", gpu.name.c_str(), static_cast<unsigned long long>(gpu.memory >> 20));
}
Picking a discrete GPU when there is one:
// Prefer a discrete GPU; otherwise take the first one listed.
const auto discrete =
std::ranges::find_if(gpus, [](const RI::Gpu &gpu) { return gpu.kind == RI::GpuKind::eDiscrete; });
const RI::Gpu &chosen = discrete != gpus.end() ? *discrete : gpus.front();
The same in native code¶
The Vulkan and D3D12 code is a canonical native version: how you would list GPUs in your own renderer, not necessarily what RI does internally.
const VkApplicationInfo appInfo{
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "my toy renderer",
.apiVersion = VK_API_VERSION_1_3, // the highest version this program uses
};
#ifndef NDEBUG
// Debug builds: the validation layer (installed with the Vulkan SDK).
const char *const layers[] = {"VK_LAYER_KHRONOS_validation"};
const uint32_t layerCount = 1;
#else
const char *const *layers = nullptr;
const uint32_t layerCount = 0;
#endif
const VkInstanceCreateInfo instanceInfo{
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &appInfo,
.enabledLayerCount = layerCount,
.ppEnabledLayerNames = layers,
};
VkInstance instance = VK_NULL_HANDLE;
if (vkCreateInstance(&instanceInfo, nullptr, &instance) != VK_SUCCESS) {
return 1;
}
// The usual two-call pattern: ask for the count, then for the handles.
uint32_t count = 0;
vkEnumeratePhysicalDevices(instance, &count, nullptr);
std::vector<VkPhysicalDevice> physicalDevices(count);
vkEnumeratePhysicalDevices(instance, &count, physicalDevices.data());
VkPhysicalDevice chosen = VK_NULL_HANDLE;
uint32_t queueFamily = 0;
for (VkPhysicalDevice physicalDevice : physicalDevices) {
// Name, vendor, type and version, plus a stable ID through the pNext chain.
VkPhysicalDeviceIDProperties id{.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES};
VkPhysicalDeviceProperties2 properties{.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &id};
vkGetPhysicalDeviceProperties2(physicalDevice, &properties);
const VkPhysicalDeviceProperties &p = properties.properties;
if (p.apiVersion < VK_API_VERSION_1_3) {
continue; // too old for this program
}
// p.vendorID is the PCI vendor ID (0x10DE NVIDIA, 0x1002 AMD, 0x8086 Intel).
// p.deviceType tells discrete, integrated, CPU (software) apart.
// id.deviceUUID identifies this GPU across instances and processes.
// Memory: the largest heap that lives on the GPU.
VkPhysicalDeviceMemoryProperties memory{};
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memory);
VkDeviceSize vram = 0;
for (uint32_t i = 0; i < memory.memoryHeapCount; ++i) {
if ((memory.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0) {
vram = std::max(vram, memory.memoryHeaps[i].size);
}
}
std::printf("%s (%llu MiB)\n", p.deviceName, static_cast<unsigned long long>(vram >> 20));
// A queue family that can do both graphics and compute.
uint32_t familyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &familyCount, nullptr);
std::vector<VkQueueFamilyProperties> families(familyCount);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &familyCount, families.data());
for (uint32_t i = 0; i < familyCount; ++i) {
const VkQueueFlags needed = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
if (chosen == VK_NULL_HANDLE && (families[i].queueFlags & needed) == needed) {
chosen = physicalDevice;
queueFamily = i;
}
}
}
What to watch for:
- In Vulkan you need an instance before you can see any GPU.
vkEnumeratePhysicalDevicesreturns every device, including software ones (llvmpipe) and GPUs too old for your program: checkapiVersionand the queue families yourself.- Most Vulkan queries use the two-call pattern: first the count, then the data.
ComPtr<IDXGIFactory6> factory;
if (FAILED(CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)))) {
return 1;
}
ComPtr<IDXGIAdapter1> chosen;
ComPtr<IDXGIAdapter1> adapter;
for (UINT i = 0; factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND; ++i) {
DXGI_ADAPTER_DESC1 desc{};
adapter->GetDesc1(&desc);
// desc.Description: the name (UTF-16). desc.VendorId / DeviceId: PCI IDs.
// desc.DedicatedVideoMemory: VRAM. desc.AdapterLuid: a stable ID until reboot.
// (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) != 0: WARP, a CPU implementation.
// D3D12 has no "list features" call on an adapter: create a short-lived
// device and ask it.
ComPtr<ID3D12Device> probe;
if (FAILED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&probe)))) {
continue;
}
D3D12_FEATURE_DATA_SHADER_MODEL shaderModel{.HighestShaderModel = D3D_SHADER_MODEL_6_6};
D3D12_FEATURE_DATA_D3D12_OPTIONS options{};
D3D12_FEATURE_DATA_D3D12_OPTIONS12 options12{};
const bool usable =
SUCCEEDED(probe->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &shaderModel, sizeof(shaderModel))) &&
shaderModel.HighestShaderModel >= D3D_SHADER_MODEL_6_6 &&
SUCCEEDED(probe->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options))) &&
options.ResourceBindingTier >= D3D12_RESOURCE_BINDING_TIER_3 &&
SUCCEEDED(probe->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS12, &options12, sizeof(options12))) &&
options12.EnhancedBarriersSupported;
std::wprintf(L"%ls (%llu MiB)%ls\n", desc.Description,
static_cast<unsigned long long>(desc.DedicatedVideoMemory >> 20),
usable ? L"" : L" - cannot run this program");
if (usable && chosen == nullptr) {
chosen = adapter;
}
}
What to watch for:
- DXGI lists adapters, not D3D12 devices. To know what an adapter supports, create a
short-lived device on it and call
CheckFeatureSupport. - The list includes WARP (the "Microsoft Basic Render Driver", a CPU implementation).
- Release the probe device before creating your real one (
ComPtrdoes it for you).
Requirements: which GPUs are listed¶
A GPU is listed only if RI can run on it. Each backend checks with its own API:
- Vulkan: the GPU reports Vulkan 1.3 or newer, and has a queue family that does both graphics and compute.
- D3D12: a device can be created at feature level 12_0, and it supports shader model 6.6, resource binding tier 3 (full bindless), resource heap tier 2 (any buffer in any memory block), and enhanced barriers.
Differences you may see between backends:
- D3D12 also lists WARP (
GpuKind::eSoftware). Vulkan lists software drivers such as llvmpipe only if they report Vulkan 1.3. - DXGI may list one physical GPU twice, under two different LUIDs (seen with an RTX 3080 on Windows).
memoryof an integrated GPU is computed differently (seeRI::Gpu).
The hardware floor of the project is NVIDIA Turing and AMD RDNA2; older GPUs may pass the checks but are not targets.
Rules and errors¶
- An empty list is not an error: it means no GPU meets the requirements. Check before
indexing
gpus[0]. - On Vulkan, if there is no Vulkan loader on the system, or if the loader is older than
1.3,
listGpusreports an unsupported error (RI::ErrorCategory::eUnsupported). - If a native call fails,
listGpusreports it through the error channel with the category mapped from its result. - You may call
listGpusat any time, including while devices are already alive. - RI never enables validation or debug layers for listing, even in validation builds.