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)(inRI::VkorRI::D3D12) to open a section. It returns aNativeCommandsstruct containing the native command buffer and Volk function table on Vulkan, or theID3D12GraphicsCommandList7 *command list on D3D12. Outside a rendering section, every texture is in its resting layout, whichnative(texture).restingLayoutreports. - 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)(orendNative(cmd)when no layout changed) to close the section.changedis a span ofNativeLayoutChangeentries listing eachTextureViewyou 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. AfterendNative, subsequent RI commands use the RI state that was active before the section began.
Example¶
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
changedmust be empty because texture layouts cannot change while rendering. Passing a non-emptychangedlist 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
beginNativewhile a section is already open, or callingendNativewhen 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.