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:
- 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;
- finds your GPU again by its
id, and checks it still meets RI's requirements; - 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;
- gets one graphics queue (Vulkan: from a queue family with graphics and compute; D3D12: a DIRECT queue);
- 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);
- fills in
device.limitsanddevice.graphicsQueue; - 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¶
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());
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.
// 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
vkGetPhysicalDeviceFeatures2and 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, leavepEnabledFeaturesnull. - Device extensions (swapchain, mesh shaders, ray queries, ...) are separate from
features: check them with
vkEnumerateDeviceExtensionPropertiesand list them inppEnabledExtensionNames. 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
CheckFeatureSupportbefore 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\\"; } -
D3D12CreateDevicereturns the sameID3D12Devicefor 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-madeRI::Gpu) reports a programmer error:the GPU '<name>' is not available; pass a GPU returned by listGpus(). - Vulkan validation builds need
VK_LAYER_KHRONOS_validationinstalled; D3D12 validation builds need the Agility SDK debug layer next to the executable. Otherwise,createDevicereports an unsupported error. RI::Deviceis a thin value: copies refer to the same device. Destroy it once.destroyDeviceon 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
createDevicebecomes the device's device thread;destroyDeviceand most other calls must run on that thread (checked in validation builds; see Threads).