Skip to content

Native sections

What they are

A native section lets you record Vulkan or D3D12 commands directly in the middle of an RI command list. Use one when you need an extension RI does not wrap, a vendor library that records its own commands, or existing native rendering code.

  • Call beginNative(cmd) (in RI::Vk or RI::D3D12) to open a section. It returns a NativeCommands struct containing the native command buffer and Volk function table on Vulkan, or the ID3D12GraphicsCommandList7 * command list on D3D12. Outside a rendering section, every texture is in its resting layout, which native(texture).restingLayout reports.
  • Record your native commands on that command buffer or command list. Outside a rendering section, you may transition texture layouts, bind your own pipelines and descriptor sets or heaps, and set your own push or root constants.
  • Call endNative(cmd, changed) (or endNative(cmd) when no layout changed) to close the section. changed is a span of NativeLayoutChange entries listing each TextureView you left in a non-resting layout together with the layout it is currently in. RI transitions each listed texture back to its resting layout using a full pipeline barrier on both sides.
  • Before returning from endNative, RI restores everything it set: the descriptor heaps (Vulkan descriptor sets), root signatures or pipeline layouts, the last bound pipelines, the last root data, primitive topology (D3D12), and—inside a rendering section—the viewport, scissor, and stencil reference. After endNative, subsequent RI commands use the RI state that was active before the section began.

Example

const RI::Vk::NativeCommands n = RI::Vk::beginNative(cmd);
// ... a native barrier from native(texture).restingLayout to TRANSFER_DST_OPTIMAL,
//     then a native clear ...
const RI::Vk::NativeLayoutChange left[] = {{texture.view, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL}};
RI::Vk::endNative(cmd, left);
const RI::D3D12::NativeCommands n = RI::D3D12::beginNative(cmd);
// ... a native texture barrier from native(texture).restingLayout to COPY_DEST,
//     then CopyTextureRegion ...
const RI::D3D12::NativeLayoutChange left[] = {{texture.view, D3D12_BARRIER_LAYOUT_COPY_DEST}};
RI::D3D12::endNative(cmd, left);

Rules and errors

  • Inside a rendering section, native code must not end rendering or change attachments, and changed must be empty because texture layouts cannot change while rendering. Passing a non-empty changed list inside a rendering section is reported as a programmer error.
  • List every texture you left in a non-resting layout in changed. RI cannot see layout transitions recorded natively, so a missing entry leaves the texture in the wrong layout for RI's next use, which the native validation layers will report.
  • Synchronization between RI commands and native commands is your responsibility: any native write read by later RI work (or RI write read by native work) needs a barrier, either natively or via cmdBarrier.
  • Native sections do not nest: calling beginNative while a section is already open, or calling endNative when none is open, is a programmer error.
  • RI only restores the state listed above that RI itself set. State that RI does not track across native sections (such as an index buffer binding) is not restored; rebind it with RI if subsequent RI draws depend on it.