Skip to content

RI::createDevice, RI::destroyDevice

What it does

A device is your program's open connection to one GPU. Almost everything in RI (memory, buffers, pipelines, command lists) is created from a device.

RI::createDevice(desc) opens the GPU named in desc.gpu (one returned by RI::listGpus) and returns an RI::Device. RI::destroyDevice(device) closes it again.

Step by step, createDevice:

  1. opens the native API for this device alone: on Vulkan, the device's own instance; on D3D12, its own DXGI factory. Two devices never share these;
  2. finds your GPU again by its id, and checks it still meets RI's requirements;
  3. creates the native device with every feature the GPU supports turned on (except robustness and capture/replay), so Vulkan behaves like D3D12, which has no feature list;
  4. gets one graphics queue (Vulkan: from a queue family with graphics and compute; D3D12: a DIRECT queue);
  5. prepares what the rest of RI needs: the queue's timeline (a Vulkan timeline semaphore or a D3D12 fence, see Timeline points), the mapping for each memory kind, and on Vulkan the descriptor set layouts behind descriptor heaps and the layout mode (described below);
  6. fills in device.limits and device.graphicsQueue;
  7. in validation builds, turns on the native validation layer and routes its errors to the error channel (see Validation).

On Vulkan, the GPU must also support every feature RI's own commands rely on, or createDevice reports an unsupported error naming what is missing: VK_EXT_mutable_descriptor_type (with mutableDescriptorType), shaderDrawParameters (so SV_VertexID excludes the base vertex), timeline semaphores, buffer device addresses, synchronization2, descriptor indexing with runtime arrays, partially bound, variable-count, and update-after-bind descriptors of every buffer and image type, dynamicRendering (rendering without render pass objects), separateDepthStencilLayouts (depth-only views), hostQueryReset (resetting queries from the CPU), occlusionQueryPrecise, pipelineStatisticsQuery, drawIndirectCount, multiDrawIndirect, fillModeNonSolid, depthClamp, depthBiasClamp, and imageCubeArray. Every GPU at RI's hardware floor supports them.

On Vulkan, createDevice also chooses how the backend hides image layouts, called the layout mode: it picks eUnified when the driver supports VK_KHR_unified_image_layouts and its unifiedImageLayouts feature, and eTransitions otherwise. This choice changes nothing in your RI code; see Image layouts are RI's business. You can inspect the active mode via RI::Vk::native(device).layoutMode, or force a specific mode through RI::Vk::NativeDeviceAdditions::layoutMode (Vulkan: device creation).

The returned device.gpu is a fresh description of the GPU, read while creating the device.

destroyDevice(device) destroys the native device and the instance (or factory) created for it, then sets device.internal.state to null.

Signature

// <RI/RI.hpp>, namespace RI
Device createDevice(const DeviceDesc &desc);
// <RI/RI.hpp>, namespace RI
void destroyDevice(Device &device);

From the header: Every destroy and free function below destroys at once: the GPU must no longer use the object (wait for the timeline point of the last submission that used it, P20).

Example

RI::Device device = RI::createDevice({.gpu = chosen});
std::printf("Device created on %s\n", device.gpu.name.c_str());
RI::destroyDevice(device);
// device.internal.state is now null: using `device` again is an error.

The same in native code

The Vulkan and D3D12 code is a canonical native version: how you would create a device in your own renderer, not necessarily what RI does internally.

RI::Device device = RI::createDevice({.gpu = chosen});
std::printf("Device created on %s\n", device.gpu.name.c_str());
RI::destroyDevice(device);
// device.internal.state is now null: using `device` again is an error.
// 1. Ask which features the GPU supports, with a chain of feature structures.
//    Each structure points to the next through pNext.
VkPhysicalDeviceVulkan13Features v13{.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES};
VkPhysicalDeviceVulkan12Features v12{.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES, .pNext = &v13};
VkPhysicalDeviceVulkan11Features v11{.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES, .pNext = &v12};
VkPhysicalDeviceFeatures2 features{.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, .pNext = &v11};
vkGetPhysicalDeviceFeatures2(chosen, &features);

// 2. The same chain now holds VK_TRUE for every supported feature. Turn off
//    what you do not want: robustness costs performance just by being on.
features.features.robustBufferAccess = VK_FALSE;
v13.robustImageAccess = VK_FALSE;
v12.bufferDeviceAddressCaptureReplay = VK_FALSE; // for capture tools only

// 3. Create the device with one queue, passing the chain in pNext.
//    pEnabledFeatures stays null: the features travel in the chain instead.
const float priority = 1.0f;
const VkDeviceQueueCreateInfo queueInfo{
    .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
    .queueFamilyIndex = queueFamily,
    .queueCount = 1,
    .pQueuePriorities = &priority,
};
const VkDeviceCreateInfo deviceInfo{
    .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
    .pNext = &features,
    .queueCreateInfoCount = 1,
    .pQueueCreateInfos = &queueInfo,
    // Device extensions (e.g. VK_KHR_swapchain) go in ppEnabledExtensionNames,
    // after checking vkEnumerateDeviceExtensionProperties.
};
VkDevice device = VK_NULL_HANDLE;
if (vkCreateDevice(chosen, &deviceInfo, nullptr, &device) != VK_SUCCESS) {
    vkDestroyInstance(instance, nullptr);
    return 1;
}
VkQueue queue = VK_NULL_HANDLE;
vkGetDeviceQueue(device, queueFamily, 0, &queue);
vkDeviceWaitIdle(device); // nothing may still run on the GPU
vkDestroyDevice(device, nullptr);
vkDestroyInstance(instance, nullptr);

What to watch for:

  • Vulkan enables nothing by default. Every feature you use must be turned on at device creation, through a chain of feature structures linked by pNext.
  • Asking with vkGetPhysicalDeviceFeatures2 and passing the same chain back turns on everything supported. Turn off robustness yourself: it costs performance just by being on.
  • When the chain is in pNext, leave pEnabledFeatures null.
  • Device extensions (swapchain, mesh shaders, ray queries, ...) are separate from features: check them with vkEnumerateDeviceExtensionProperties and list them in ppEnabledExtensionNames. RI enables its own when the GPU has them.
  • Destroy in reverse order: the device, then the instance.
// No feature list: everything the GPU supports is available once the device exists.
ComPtr<ID3D12Device> device;
if (FAILED(D3D12CreateDevice(chosen.Get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&device)))) {
    return 1;
}
// A DIRECT queue accepts graphics, compute and copy work.
const D3D12_COMMAND_QUEUE_DESC queueDesc{.Type = D3D12_COMMAND_LIST_TYPE_DIRECT};
ComPtr<ID3D12CommandQueue> queue;
if (FAILED(device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&queue)))) {
    return 1;
}
// COM objects are reference counted: Reset() (or leaving the scope) releases
// them. With work in flight, wait on a fence first.
queue.Reset();
device.Reset();
factory.Reset();

What to watch for:

  • D3D12 has no feature list: once the device exists, everything the GPU supports is available. Ask with CheckFeatureSupport before using something.
  • The executable must export the Agility SDK version and path, or Windows uses its own, older D3D12 runtime:

    // The Agility SDK: D3D12 loads the newer runtime shipped next to your program
    // (in .\D3D12\) only if the *executable* exports these two symbols.
    extern "C" {
    __declspec(dllexport) extern const UINT D3D12SDKVersion = D3D12_SDK_VERSION;
    __declspec(dllexport) extern const char *D3D12SDKPath = ".\\D3D12\\";
    }
    
  • D3D12CreateDevice returns the same ID3D12Device for every call on one adapter: it is reference counted, not a new device.

What RI enables on Vulkan

"Everything supported" means every feature the GPU reports in the core Vulkan 1.0 to 1.4 feature structures and in the structures of RI's extensions, with these turned off:

  • robustness: robustBufferAccess, robustImageAccess, pipelineRobustness;
  • capture/replay features (for capture tools; they can change how drivers place memory);
  • features that need an extension RI does not enable.

It does not mean every extension, because some extensions change behaviour just by being on. RI always enables VK_EXT_mutable_descriptor_type (required for descriptor heaps), and enables these extensions when the GPU supports them: VK_KHR_swapchain, VK_EXT_mesh_shader, VK_KHR_deferred_host_operations together with VK_KHR_acceleration_structure (and VK_KHR_ray_query when present), VK_KHR_unified_image_layouts, VK_EXT_device_generated_commands (on Vulkan 1.4 devices, except on NVIDIA in validation builds with GPU-assisted validation enabled), and VK_EXT_device_fault (in validation builds only). The device's Vulkan instance always enables VK_EXT_debug_utils for object names and markers, along with available surface extensions.

To change any of this (turn robustness on, add an extension), use the native overload described in Vulkan device creation. On D3D12, a hook runs just before the device is created: D3D12 device creation.

Rules and errors

  • Pass a GPU returned by listGpus. A GPU that is not available (for example a hand-made RI::Gpu) reports a programmer error: the GPU '<name>' is not available; pass a GPU returned by listGpus().
  • Vulkan validation builds need VK_LAYER_KHRONOS_validation installed; D3D12 validation builds need the Agility SDK debug layer next to the executable. Otherwise, createDevice reports an unsupported error.
  • RI::Device is a thin value: copies refer to the same device. Destroy it once.
  • destroyDevice on a device that was never created, or on the copy you already destroyed, reports a programmer error. Other copies of a destroyed device are not detected, so do not use them after destroying the device.
  • Wait for the GPU to finish all work submitted to the device before calling destroyDevice (for example, by waiting on the timeline point of the last submission).
  • You can create several devices at once, even on the same GPU. On D3D12 they share one ID3D12Device, with each RI device holding a reference.
  • The thread that calls createDevice becomes the device's device thread; destroyDevice and most other calls must run on that thread (checked in validation builds; see Threads).