Skip to content

Vulkan: native handles of RI objects

What it does

The core RI objects have RI::Vk::native(...) overloads in <RI/Vk/Native.hpp> that return the underlying Vulkan handle(s). Use these handles to call Vulkan functions that RI does not wrap, such as naming objects for debuggers, querying properties, or recording native commands.

RI object RI::Vk::native(...) returns
Device NativeDevice: instance, physical device, device, function table, graphics queue, API version, and layout mode (see device creation)
Memory VkDeviceMemory
Buffer VkBuffer
DescriptorHeap NativeDescriptorHeap: the VkDescriptorPool and the two VkDescriptorSet handles (resourceSet at set 0 for views, samplerSet at set 1 for samplers)
Pipeline NativePipeline: the VkPipeline and its VkPipelineLayout
CommandList VkCommandBuffer
Texture NativeTexture: the VkImage and restingLayout (the VkImageLayout it rests in)
TextureView VkImageView
QueryPool VkQueryPool

The returned handles are borrowed: RI owns them and destroys them when you destroy the RI object (unless you imported the object).

Signature

// <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).

// <RI/Vk/Native.hpp>, namespace RI::Vk
struct NativeDescriptorHeap {
    VkDescriptorPool pool = VK_NULL_HANDLE;
    VkDescriptorSet resourceSet = VK_NULL_HANDLE; // set 0, binding 0: VK_DESCRIPTOR_TYPE_MUTABLE_EXT array
    VkDescriptorSet samplerSet = VK_NULL_HANDLE;  // set 1, binding 0: sampler array
};
Field Type Default Description
pool VkDescriptorPool VK_NULL_HANDLE
resourceSet VkDescriptorSet VK_NULL_HANDLE set 0, binding 0: VK_DESCRIPTOR_TYPE_MUTABLE_EXT array
samplerSet VkDescriptorSet VK_NULL_HANDLE set 1, binding 0: sampler array
// <RI/Vk/Native.hpp>, namespace RI::Vk
struct NativePipeline {
    VkPipeline pipeline = VK_NULL_HANDLE;
    VkPipelineLayout layout = VK_NULL_HANDLE;
};
Field Type Default Description
pipeline VkPipeline VK_NULL_HANDLE
layout VkPipelineLayout VK_NULL_HANDLE
// <RI/Vk/Native.hpp>, namespace RI::Vk
struct NativeTexture {
    VkImage image = VK_NULL_HANDLE;
    VkImageLayout restingLayout = VK_IMAGE_LAYOUT_UNDEFINED;
};

From the header: The native image and the layout it rests in (L1). Native code that leaves a texture in another layout must return it to this one (P27).

Field Type Default Description
image VkImage VK_NULL_HANDLE
restingLayout VkImageLayout VK_IMAGE_LAYOUT_UNDEFINED

Example: name a buffer

RI::Memory memory = RI::allocateMemory(device, RI::MemoryKind::eGPU, 65536);
RI::Buffer buffer = RI::createBuffer(device, memory, 0, {.size = 1024, .usage = RI::BufferUsage::eCopyDst});
RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16, .samplerCount = 16});
const VkBuffer vkBuffer = RI::Vk::native(buffer);
const VkDeviceMemory vkMemory = RI::Vk::native(memory);
const RI::Vk::NativeDescriptorHeap vkHeap = RI::Vk::native(heap); // set 0: views, set 1: samplers
std::printf("memory %p, heap sets %p / %p\n", static_cast<void *>(vkMemory), static_cast<void *>(vkHeap.resourceSet),
            static_cast<void *>(vkHeap.samplerSet));

// Name the buffer for debuggers and validation messages. Debug-utils
// functions are instance extension functions: fetch them per instance.
const auto setName = reinterpret_cast<PFN_vkSetDebugUtilsObjectNameEXT>(
    vkGetInstanceProcAddr(n.instance, "vkSetDebugUtilsObjectNameEXT"));
const VkDebugUtilsObjectNameInfoEXT nameInfo{
    .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
    .objectType = VK_OBJECT_TYPE_BUFFER,
    .objectHandle = reinterpret_cast<uint64_t>(vkBuffer),
    .pObjectName = "particles",
};
setName(n.device, &nameInfo);

Example: a native command in an RI command list

RI::CommandList cmd = RI::createCommandList(device);
RI::cmdBegin(cmd);
// A native command recorded into RI's command buffer, through the device's
// function table. vkCmdFillBuffer changes no state RI relies on.
n.functions->vkCmdFillBuffer(RI::Vk::native(cmd), vkBuffer, 0, VK_WHOLE_SIZE, 0);
RI::cmdEnd(cmd);
RI::wait(device.graphicsQueue, RI::submit(device.graphicsQueue, cmd));

Device-level functions such as vkCmdFillBuffer go through the device's function table, RI::Vk::native(device).functions.

Example: a native command on an RI texture

RI hides image layouts (Image layouts are RI's business), but a native command may need a specific layout. NativeTexture::restingLayout is the VkImageLayout the texture is in between RI operations; it depends on the texture's usage and format and on the device's layout mode. Transition the texture out of restingLayout, record the native command, and transition it back:

// A texture's VkImage and the layout it rests in. Native code may move it
// to another layout, but must return it to restingLayout before RI uses it.
RI::Memory textureMemory = RI::allocateMemory(device, RI::MemoryKind::eGPU, 16u << 20);
RI::Texture texture = RI::createTexture(device, textureMemory, 0,
                                        {.width = 256,
                                         .height = 256,
                                         .format = RI::Format::eRGBA8Unorm,
                                         .usage = RI::TextureUsage::eSampled | RI::TextureUsage::eCopyDst});
const RI::Vk::NativeTexture image = RI::Vk::native(texture);
const VkImageView view = RI::Vk::native(texture.view); // views: VkImageView

const VkImageSubresourceRange whole{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
const auto layoutChange = [&](VkImageLayout from, VkImageLayout to) {
    const VkImageMemoryBarrier2 barrier{.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
                                        .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
                                        .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT,
                                        .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
                                        .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT,
                                        .oldLayout = from,
                                        .newLayout = to,
                                        .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
                                        .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
                                        .image = image.image,
                                        .subresourceRange = whole};
    const VkDependencyInfo dependency{
        .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &barrier};
    n.functions->vkCmdPipelineBarrier2(RI::Vk::native(cmd), &dependency);
};
RI::resetCommandList(cmd);
RI::cmdBegin(cmd);
// Out of rest, a native clear, back to rest.
layoutChange(image.restingLayout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
const VkClearColorValue red{{1.0f, 0.0f, 0.0f, 1.0f}};
n.functions->vkCmdClearColorImage(RI::Vk::native(cmd), image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &red, 1,
                                  &whole);
layoutChange(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image.restingLayout);
RI::cmdEnd(cmd);
RI::wait(device.graphicsQueue, RI::submit(device.graphicsQueue, cmd));
std::printf("image view %p\n", static_cast<void *>(view));

Example: a query pool

RI::QueryPool pool = RI::createQueryPool(device, {.type = RI::QueryType::eTimestamp, .count = 2});
const VkQueryPool vkPool = RI::Vk::native(pool);
// E.g. read results on the CPU without a buffer (after the timeline point).
uint64_t results[2] = {};
n.functions->vkGetQueryPoolResults(n.device, vkPool, 0, 2, sizeof(results), results, sizeof(uint64_t),
                                   VK_QUERY_RESULT_64_BIT);

The same in native code

RI::Memory memory = RI::allocateMemory(device, RI::MemoryKind::eGPU, 65536);
RI::Buffer buffer = RI::createBuffer(device, memory, 0, {.size = 1024, .usage = RI::BufferUsage::eCopyDst});
RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16, .samplerCount = 16});
const VkBuffer vkBuffer = RI::Vk::native(buffer);
const VkDeviceMemory vkMemory = RI::Vk::native(memory);
const RI::Vk::NativeDescriptorHeap vkHeap = RI::Vk::native(heap); // set 0: views, set 1: samplers
std::printf("memory %p, heap sets %p / %p\n", static_cast<void *>(vkMemory), static_cast<void *>(vkHeap.resourceSet),
            static_cast<void *>(vkHeap.samplerSet));

// Name the buffer for debuggers and validation messages. Debug-utils
// functions are instance extension functions: fetch them per instance.
const auto setName = reinterpret_cast<PFN_vkSetDebugUtilsObjectNameEXT>(
    vkGetInstanceProcAddr(n.instance, "vkSetDebugUtilsObjectNameEXT"));
const VkDebugUtilsObjectNameInfoEXT nameInfo{
    .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
    .objectType = VK_OBJECT_TYPE_BUFFER,
    .objectHandle = reinterpret_cast<uint64_t>(vkBuffer),
    .pObjectName = "particles",
};
setName(n.device, &nameInfo);

In a plain Vulkan program you already hold these handles; see createBuffer and the other reference pages for how each is created.

The D3D12 equivalents: D3D12: native objects.

Rules

  • Do not destroy or free a handle that RI owns; destroy the RI object instead.
  • If you record native commands directly on RI::Vk::native(cmd) between RI commands, leave the state RI relies on unchanged: the bound pipeline, its pipeline layout, the descriptor heap's sets, push constants, and (inside a rendering section) the viewport, scissor, and stencil reference. Commands that bind no state (such as fills, copies, queries, and debug labels) are safe on their own. If your native commands change state or texture layouts, wrap them in a native section (beginNative / endNative) so RI saves and restores its state.
  • A texture must be in its resting layout whenever RI uses it. Transition every texture you moved back to restingLayout—either with your own barrier or by passing it to endNative—before the next RI command that may touch it and before the command list ends. RI writes shader descriptors for textures using restingLayout (VK_IMAGE_LAYOUT_GENERAL for storage views).
  • Synchronize native work with barriers as usual; RI's cmdBarrier covers native commands as well, by pipeline stage.
  • A descriptor written natively into NativeDescriptorHeap::resourceSet (set 0) must use a type from RI's mutable descriptor list: sampled image, storage image, uniform texel buffer, storage texel buffer, uniform buffer, storage buffer, or (when ray tracing is enabled) acceleration structure (VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR).