Skip to content

RI::Gpu

What it is

RI::Gpu describes one GPU as RI::listGpus found it: plain data, with no native handle inside. You read its fields to choose a GPU, then pass the whole struct to RI::createDevice, which finds the same GPU again by matching its id.

Because it holds no native handle, a Gpu stays valid after listGpus returns, can be copied freely, and is the same type on both backends.

Signature

// <RI/RI.hpp>, namespace RI
struct Gpu {
    std::string name;
    GpuVendor vendor = GpuVendor::eOther;
    GpuKind kind = GpuKind::eOther;
    uint32 vendorId = 0; // PCI vendor ID
    uint32 deviceId = 0; // PCI device ID
    // Bytes of GPU memory. Discrete GPUs: the VRAM. Integrated GPUs: the system
    // memory the GPU may use, as each API reports it (Vulkan: the largest
    // device-local heap; D3D12: shared system memory), so the backends differ.
    uint64 memory = 0;
    // Stable within the process: Vulkan deviceUUID, D3D12 adapter LUID (first 8 bytes).
    std::array<uint8, 16> id{};
};

From the header: One GPU as listed by listGpus(): plain facts, no native object. createDevice finds the same GPU again through id. Lists follow the native API, so they can differ between backends: D3D12 also lists WARP (eSoftware), and DXGI may list one physical GPU twice under two LUIDs (seen with an RTX 3080 on Windows).

Field Type Default Description
name std::string
vendor GpuVendor GpuVendor::eOther
kind GpuKind GpuKind::eOther
vendorId uint32 0 PCI vendor ID
deviceId uint32 0 PCI device ID
memory uint64 0 Bytes of GPU memory. Discrete GPUs: the VRAM. Integrated GPUs: the system memory the GPU may use, as each API reports it (Vulkan: the largest device-local heap; D3D12: shared system memory), so the backends differ.
id std::array<uint8, 16> {} Stable within the process: Vulkan deviceUUID, D3D12 adapter LUID (first 8 bytes).

GpuVendor

// <RI/RI.hpp>, namespace RI
enum class GpuVendor : uint8 {
    eOther,
    eNvidia,
    eAMD,
    eIntel,
};
Value Meaning
eOther
eNvidia
eAMD
eIntel

GpuKind

// <RI/RI.hpp>, namespace RI
enum class GpuKind : uint8 {
    eOther,
    eDiscrete,
    eIntegrated, // shares system memory (laptops, APUs)
    eSoftware,   // a CPU implementation (llvmpipe, WARP)
};
Value Meaning
eOther
eDiscrete
eIntegrated shares system memory (laptops, APUs)
eSoftware a CPU implementation (llvmpipe, WARP)

Where each field comes from

Field Vulkan D3D12
name VkPhysicalDeviceProperties::deviceName DXGI_ADAPTER_DESC1::Description (converted to UTF-8)
vendorId, deviceId vendorID, deviceID VendorId, DeviceId
vendor from the PCI vendor ID: 0x10DE (eNvidia), 0x1002 or 0x1022 (eAMD), 0x8086 (eIntel), otherwise eOther same
kind deviceType: discrete (eDiscrete), integrated (eIntegrated), CPU (eSoftware), otherwise eOther DXGI_ADAPTER_FLAG_SOFTWARE gives eSoftware; otherwise eIntegrated if the GPU is UMA (shares system memory), else eDiscrete
memory the largest heap with VK_MEMORY_HEAP_DEVICE_LOCAL_BIT DedicatedVideoMemory; on UMA GPUs, the larger of DedicatedVideoMemory and SharedSystemMemory
id VkPhysicalDeviceIDProperties::deviceUUID (16 bytes) DXGI_ADAPTER_DESC1::AdapterLuid (8 bytes; the remaining 8 bytes are zero)

Because the two backends compute memory and id differently, the same integrated GPU can report a different memory on each backend, and the id of one GPU differs between Vulkan and D3D12. The id is stable within the process: use it to recognise a GPU during the current run, not to store across runs.

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));
}
// 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

Reading the same facts directly from the native API (canonical code, not RI's internals):

// 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();
// 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));

What to watch for: the stable ID (VkPhysicalDeviceIDProperties) is not in the basic properties. Chain it in pNext of VkPhysicalDeviceProperties2.

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");

What to watch for: DXGI_ADAPTER_DESC1 says nothing about D3D12 support. Create a short-lived device and ask it with CheckFeatureSupport.

Rules and errors

  • Pass RI::createDevice a Gpu that RI::listGpus returned in the current process. Passing a hand-made or stale Gpu whose id matches no available GPU reports a programmer error.
  • A default-initialized Gpu has an empty name, GpuVendor::eOther, GpuKind::eOther, 0 for vendorId, deviceId, and memory, and an all-zero id.