Vulkan: device creation¶
What it does¶
RI::createDevice enables the Vulkan extensions RI uses
and every feature the GPU supports, except robustness and capture/replay features. When you
need something else—such as robust buffer access to debug an out-of-bounds read, or an
extension RI does not enable—the Vulkan backend provides RI::Vk::createDevice, which takes
a NativeDeviceAdditions struct. All of these declarations live in
<RI/Vk/Native.hpp>.
You can customize the created device in three ways, and you can combine them:
- Change RI's features.
RI::Vk::defaultDeviceFeatures(gpu)returns the features RI would enable by default, packed into a plain struct of Vulkan feature structures. Modify any field and pass a pointer to the struct in.features. - Add extensions. List extra extension names in
.extensions; RI enables them alongside its own extensions. - Add feature structures RI does not know. Pass a chain in
.pNext. RI appends your chain after its own feature chain without modifying it.
A fourth field, .layoutMode, is for testing and measuring: it forces how the Vulkan
backend hides image layouts (see below). A fifth field, .hostBarrierAtEnd, is also for
measuring: turning it off skips cmdEnd's barrier to the host, so CPU reads after a
timeline point are no longer guaranteed to see GPU writes.
Signature¶
// <RI/Vk/Native.hpp>, namespace RI::Vk
struct DeviceFeatures {
VkPhysicalDeviceFeatures core{};
VkPhysicalDeviceVulkan11Features v11{};
VkPhysicalDeviceVulkan12Features v12{};
VkPhysicalDeviceVulkan13Features v13{};
VkPhysicalDeviceVulkan14Features v14{};
VkPhysicalDeviceMeshShaderFeaturesEXT mesh{};
VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationStructure{};
VkPhysicalDeviceRayQueryFeaturesKHR rayQuery{};
#ifdef VK_KHR_unified_image_layouts
VkPhysicalDeviceUnifiedImageLayoutsFeaturesKHR unifiedLayouts{};
#endif
VkPhysicalDeviceFaultFeaturesEXT fault{};
VkPhysicalDeviceMutableDescriptorTypeFeaturesEXT mutableDescriptor{}; // descriptor heaps (P94)
VkPhysicalDeviceDeviceGeneratedCommandsFeaturesEXT generatedCommands{}; // indirect Level 2 (P48)
};
From the header: The features RI enables (P60 (2), P92): every feature the GPU reports, minus robustness and capture/replay. Get them with defaultDeviceFeatures, change any field (e.g. turn robustness on), and pass them to createDevice. RI links the structures itself, so sType and pNext are ignored. Members of extensions RI does not enable on this GPU are ignored; v14 is used only on Vulkan 1.4 devices. After your changes RI still turns off features whose prerequisites are off (e.g. update-after-bind buffers when robust buffer access cannot support them), so the device stays valid.
| Field | Type | Default | Description |
|---|---|---|---|
core |
VkPhysicalDeviceFeatures |
{} |
|
v11 |
VkPhysicalDeviceVulkan11Features |
{} |
|
v12 |
VkPhysicalDeviceVulkan12Features |
{} |
|
v13 |
VkPhysicalDeviceVulkan13Features |
{} |
|
v14 |
VkPhysicalDeviceVulkan14Features |
{} |
|
mesh |
VkPhysicalDeviceMeshShaderFeaturesEXT |
{} |
|
accelerationStructure |
VkPhysicalDeviceAccelerationStructureFeaturesKHR |
{} |
|
rayQuery |
VkPhysicalDeviceRayQueryFeaturesKHR |
{} |
|
unifiedLayouts |
VkPhysicalDeviceUnifiedImageLayoutsFeaturesKHR |
{} |
(only when VK_KHR_unified_image_layouts is defined) |
fault |
VkPhysicalDeviceFaultFeaturesEXT |
{} |
|
mutableDescriptor |
VkPhysicalDeviceMutableDescriptorTypeFeaturesEXT |
{} |
descriptor heaps (P94) |
generatedCommands |
VkPhysicalDeviceDeviceGeneratedCommandsFeaturesEXT |
{} |
indirect Level 2 (P48) |
// <RI/Vk/Native.hpp>, namespace RI::Vk
struct NativeDeviceAdditions {
// Extra device extensions, enabled alongside RI's own.
std::span<const char *const> extensions;
// RI's features as changed by you (start from defaultDeviceFeatures).
// nullptr: RI's default.
const DeviceFeatures *features = nullptr;
// Extra structures for VkDeviceCreateInfo, appended after RI's chain and
// left untouched. Not for the structures in DeviceFeatures or their
// single-feature equivalents (e.g. VkPhysicalDeviceImageRobustnessFeatures):
// Vulkan forbids chaining both; change DeviceFeatures instead.
const void *pNext = nullptr;
// Forces a layout mode, for testing and measuring (P78). eUnified needs the extension.
LayoutMode layoutMode = LayoutMode::eAuto;
// cmdEnd's barrier to the host (P94 (6)). Off only to measure its cost: CPU
// reads after a timeline point are then not guaranteed to see GPU writes.
bool hostBarrierAtEnd = true;
};
From the header: Native additions at device creation (P60 (3)).
| Field | Type | Default | Description |
|---|---|---|---|
extensions |
std::span<const char *const> |
Extra device extensions, enabled alongside RI's own. | |
features |
const DeviceFeatures * |
nullptr |
RI's features as changed by you (start from defaultDeviceFeatures). nullptr: RI's default. |
pNext |
const void * |
nullptr |
Extra structures for VkDeviceCreateInfo, appended after RI's chain and left untouched. Not for the structures in DeviceFeatures or their single-feature equivalents (e.g. VkPhysicalDeviceImageRobustnessFeatures): Vulkan forbids chaining both; change DeviceFeatures instead. |
layoutMode |
LayoutMode |
LayoutMode::eAuto |
Forces a layout mode, for testing and measuring (P78). eUnified needs the extension. |
hostBarrierAtEnd |
bool |
true |
cmdEnd's barrier to the host (P94 (6)). Off only to measure its cost: CPU reads after a timeline point are then not guaranteed to see GPU writes. |
// <RI/Vk/Native.hpp>, namespace RI::Vk
Device createDevice(const DeviceDesc &desc, const NativeDeviceAdditions &additions);
Example: turn robustness on¶
// Start from what RI would enable, change fields, pass the result back.
RI::Vk::DeviceFeatures features = RI::Vk::defaultDeviceFeatures(gpus[0]);
features.core.robustBufferAccess = VK_TRUE; // off by default
features.v13.robustImageAccess = VK_TRUE;
RI::Device robust = RI::Vk::createDevice({.gpu = gpus[0]}, {.features = &features});
Example: an extension with its feature structure¶
// An extension RI does not enable, with its feature structure. RI appends
// the structure after its own chain and leaves it untouched.
const char *const extensions[] = {VK_KHR_SHADER_CLOCK_EXTENSION_NAME};
VkPhysicalDeviceShaderClockFeaturesKHR clock{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CLOCK_FEATURES_KHR,
.shaderSubgroupClock = VK_TRUE,
};
RI::Device device = RI::Vk::createDevice({.gpu = gpus[0]}, {.extensions = extensions, .pNext = &clock});
Example: force a layout mode¶
// Force a layout mode, to test or measure (P78). User code is the same in every mode.
RI::Device modeT = RI::Vk::createDevice({.gpu = gpus[0]}, {.layoutMode = RI::Vk::LayoutMode::eTransitions});
const RI::Vk::LayoutMode mode = RI::Vk::native(modeT).layoutMode; // the mode in use, never eAuto
std::printf("layout mode %u\n", static_cast<unsigned>(mode));
The Vulkan backend hides image layouts in one of three layout modes, described in Image layouts are RI's business:
// <RI/Vk/Native.hpp>, namespace RI::Vk
enum class LayoutMode : uint8 {
eAuto, // eUnified when VK_KHR_unified_image_layouts is on, else the measured choice (P78)
eUnified, // everything rests in GENERAL; VK_KHR_unified_image_layouts makes it optimal
eGeneral, // mode G: everything rests in GENERAL without the extension (no switches)
eTransitions, // mode T: D3D12-style resting layouts, switched around RI's operations (P68)
};
From the header: How the Vulkan backend hides image layouts (L1, A', P78). Backend-internal: user code is identical in every mode (P16).
| Value | Meaning |
|---|---|
eAuto |
eUnified when VK_KHR_unified_image_layouts is on, else the measured choice (P78) |
eUnified |
everything rests in GENERAL; VK_KHR_unified_image_layouts makes it optimal |
eGeneral |
mode G: everything rests in GENERAL without the extension (no switches) |
eTransitions |
mode T: D3D12-style resting layouts, switched around RI's operations (P68) |
layoutMode |
Effect |
|---|---|
eAuto (default) |
Selects eUnified when VK_KHR_unified_image_layouts is enabled with its unifiedImageLayouts feature, and eTransitions otherwise |
eUnified |
Every texture rests in VK_IMAGE_LAYOUT_GENERAL with no layout switches; requires VK_KHR_unified_image_layouts and unifiedImageLayouts (reports an unsupported error otherwise) |
eGeneral |
Mode G: every texture rests in VK_IMAGE_LAYOUT_GENERAL without the extension, with no layout switches |
eTransitions |
Mode T: each texture rests in a layout chosen from its usage flags, and RI switches layouts around its own operations |
Your RI code is identical in every mode; only
native code that touches RI's textures sees the difference, through each texture's resting
layout (Vulkan: native handles). NativeDevice::layoutMode reports the mode in
use, never eAuto.
The same in native code¶
In plain Vulkan, you build the feature chain yourself. RI's DeviceFeatures is that chain,
already filled in, without the sType / pNext bookkeeping.
// Start from what RI would enable, change fields, pass the result back.
RI::Vk::DeviceFeatures features = RI::Vk::defaultDeviceFeatures(gpus[0]);
features.core.robustBufferAccess = VK_TRUE; // off by default
features.v13.robustImageAccess = VK_TRUE;
RI::Device robust = RI::Vk::createDevice({.gpu = gpus[0]}, {.features = &features});
// 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);
D3D12 has no feature enable step: the equivalent is a hook that runs before the device is created. See D3D12: device creation.
Rules¶
sTypeandpNextinDeviceFeaturesare ignored. RI setssTypeand links the structures itself.- Unused extension structs are ignored. Fields for extensions RI does not enable on this
GPU are not linked into
VkDeviceCreateInfo(for example,meshon a GPU withoutVK_EXT_mesh_shader).v14is linked only on Vulkan 1.4 devices. - RI turns off features whose prerequisites are off. After applying your
DeviceFeatures, RI disables unsupported combinations sovkCreateDevicestays valid: update-after-bind for uniform, storage, and texel buffers whenrobustBufferAccessis enabled on a GPU whoserobustBufferAccessUpdateAfterBindproperty is false;multiviewMeshShaderwhenmultiviewis off;accelerationStructurewhenbufferDeviceAddressis off;rayQuerywhenaccelerationStructureis off; andprimitiveFragmentShadingRateMeshShaderandunifiedImageLayoutsVideo, which require extensions RI does not enable. - Do not put RI's structures in
pNext, nor their single-feature equivalents (for example,VkPhysicalDeviceImageRobustnessFeatures, which duplicatesv13.robustImageAccess): Vulkan forbids chaining the same feature twice. ChangeDeviceFeaturesinstead. defaultDeviceFeaturesopens a short-lived Vulkan instance to query the GPU's supported features. Call it with aGpufromlistGpus.- RI's own commands require several features (such as mutable descriptors, timeline
semaphores, buffer device addresses,
synchronization2, descriptor indexing with update-after-bind, dynamic rendering, separate depth-stencil layouts, and host query reset). Turning any required feature off inDeviceFeaturesmakescreateDevicereport an unsupported error that names the missing feature.mutableDescriptorcorresponds toVK_EXT_mutable_descriptor_type, which RI's descriptor heaps are built on. - Requesting an extension or feature that the GPU does not support makes
vkCreateDevicefail, which RI reports as unsupported through the error channel.
Native handles¶
// <RI/Vk/Native.hpp>, namespace RI::Vk
struct NativeDevice {
VkInstance instance = VK_NULL_HANDLE;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
VkDevice device = VK_NULL_HANDLE;
// Device-level functions for this device (P89). Core instance-level functions are volk's
// globals; fetch instance extension functions with vkGetInstanceProcAddr(instance, ...).
const VolkDeviceTable *functions = nullptr;
uint32 graphicsQueueFamily = 0;
VkQueue graphicsQueue = VK_NULL_HANDLE;
uint32 apiVersion = 0; // the device's Vulkan version, at least 1.3
LayoutMode layoutMode = LayoutMode::eAuto; // the mode in use (never eAuto)
};
| Field | Type | Default | Description |
|---|---|---|---|
instance |
VkInstance |
VK_NULL_HANDLE |
|
physicalDevice |
VkPhysicalDevice |
VK_NULL_HANDLE |
|
device |
VkDevice |
VK_NULL_HANDLE |
|
functions |
const VolkDeviceTable * |
nullptr |
Device-level functions for this device (P89). Core instance-level functions are volk's globals; fetch instance extension functions with vkGetInstanceProcAddr(instance, ...). |
graphicsQueueFamily |
uint32 |
0 |
|
graphicsQueue |
VkQueue |
VK_NULL_HANDLE |
|
apiVersion |
uint32 |
0 |
the device's Vulkan version, at least 1.3 |
layoutMode |
LayoutMode |
LayoutMode::eAuto |
the mode in use (never eAuto) |
// <RI/Vk/Native.hpp>, namespace RI::Vk
NativeDevice native(const Device &device);
VkDeviceMemory native(const Memory &memory);
VkBuffer native(const Buffer &buffer);
VkCommandBuffer native(const CommandList &cmd);
NativeDescriptorHeap native(const DescriptorHeap &heap);
NativeTexture native(const Texture &texture);
VkImageView native(const TextureView &view);
VkQueryPool native(const QueryPool &pool);
NativePipeline native(const Pipeline &pipeline);
From the header: Native handles of RI objects (P84).
const RI::Vk::NativeDevice n = RI::Vk::native(device);
// Device-level functions: through the device's own table.
n.functions->vkDeviceWaitIdle(n.device);
// Core instance-level functions: volk's globals.
VkPhysicalDeviceProperties properties{};
vkGetPhysicalDeviceProperties(n.physicalDevice, &properties);
std::printf("Vulkan %u.%u on queue family %u\n", VK_API_VERSION_MAJOR(n.apiVersion),
VK_API_VERSION_MINOR(n.apiVersion), n.graphicsQueueFamily);
RI loads Vulkan with volk and never links the system loader:
- Device-level functions (
vkCmd*,vkCreateBuffer,vkDeviceWaitIdle, ...): call them throughNativeDevice::functions, which is the device's ownVolkDeviceTableand skips the loader's dispatch. - Core instance-level functions (
vkGetPhysicalDeviceProperties, ...): call them directly; volk provides them as globals. - Instance extension functions: fetch them with
vkGetInstanceProcAddr(instance, "..."). Volk's globals follow the most recently created instance, which may lack the extension.
The returned handles are borrowed: RI destroys them in destroyDevice.