RI::DeviceDesc, RI::Device¶
What they are¶
RI::DeviceDesc is the description you pass to RI::createDevice.
Today it holds one field, gpu, which selects the GPU to open. Descriptions in RI are plain
structs filled with C++20 designated initializers; any field you leave out keeps its default
value, so new fields can be added later without breaking existing code.
RI::Device is the struct createDevice returns. It has three public fields and an
internal field that holds the backend's private state (never read or modify internal):
gpu: a freshRI::Gpudescription of the GPU the device runs on, read during device creation.limits: what this device allows (seeRI::DeviceLimits).graphicsQueue: the queue you submit command lists to (seeRI::submit). It is the only queue in this version.
A Device is a thin value: copying it is cheap,
and every copy refers to the same underlying device. It has no C++ destructor and owns no
resources in the RAII sense, so you must destroy it explicitly with
RI::destroyDevice exactly once when you are done with it.
Signature¶
| Field | Type | Default | Description |
|---|---|---|---|
gpu |
Gpu |
// <RI/RI.hpp>, namespace RI
struct Device {
Gpu gpu;
DeviceLimits limits;
Queue graphicsQueue;
// internal: backend-private state (not part of the API)
};
- D3D12 header: A thin value (P81): copies refer to the same device. The device owns its DXGI factory (P92).
- Vulkan header: A thin value (P81): copies refer to the same device. The device owns its Vulkan instance (P92).
| Field | Type | Default | Description |
|---|---|---|---|
gpu |
Gpu |
||
limits |
DeviceLimits |
||
graphicsQueue |
Queue |
The device struct is defined by each backend (RI::Vk::Device, RI::D3D12::Device) with the
same public fields; RI::Device names the one you
build with.
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¶
Neither Vulkan nor D3D12 has a device description struct like RI::DeviceDesc; the native
equivalent is the arguments passed to the native device-creation call.
VkDeviceCreateInfo plays the role of DeviceDesc, and VkDevice and VkQueue play
the role of RI::Device.
// 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);
The adapter and minimum feature level passed to D3D12CreateDevice play the role of
DeviceDesc, and ID3D12Device and ID3D12CommandQueue play the role of RI::Device.
// 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;
}
Reaching the native objects¶
The native handles behind a Device (such as VkInstance, VkPhysicalDevice, VkDevice,
and VkQueue on Vulkan, or IDXGIFactory6, IDXGIAdapter1, ID3D12Device, and
ID3D12CommandQueue on D3D12) are not public fields. You reach them by including the
backend's native header (<RI/Vk/Native.hpp> or <RI/D3D12/Native.hpp>) and calling
RI::Vk::native(device) or RI::D3D12::native(device). See
Native access.
Rules and errors¶
DeviceDesc::gpumust come fromRI::listGpus; otherwisecreateDevicereports a programmer error.- Do not modify
internalor copy it from oneDeviceto another.