D3D12: native objects behind RI objects¶
What it does¶
The core RI objects have RI::D3D12::native(...) overloads in <RI/D3D12/Native.hpp> that
return the D3D12 object(s) behind them. Use them for
operations RI does not wrap, such as setting debug names for PIX, querying device features, or
recording native commands.
| RI object | RI::D3D12::native(...) returns |
|---|---|
Device |
NativeDevice: factory, adapter, device, DIRECT queue, and feature level (see device creation) |
Memory |
ID3D12Heap * |
Buffer |
ID3D12Resource * |
DescriptorHeap |
NativeDescriptorHeap: the shader-visible CBV_SRV_UAV and SAMPLER heaps |
Pipeline |
NativePipeline: the ID3D12PipelineState and the root signature RI built |
CommandList |
ID3D12GraphicsCommandList7 * |
Texture |
NativeTexture: the ID3D12Resource * and the layout it rests in |
QueryPool |
ID3D12QueryHeap * (whose heap type matches QueryPoolDesc::type) |
A TextureView has no native object on D3D12 because D3D12 has no view objects, only descriptors.
RI stores each view's render-target and depth-stencil descriptors in its own CPU-only heaps.
All returned pointers are borrowed: RI keeps its references and releases them when you destroy
the RI object. Call AddRef if you need a pointer to outlive the RI object.
Signature¶
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
NativeDevice native(const Device &device);
ID3D12Heap *native(const Memory &memory);
ID3D12Resource *native(const Buffer &buffer);
ID3D12GraphicsCommandList7 *native(const CommandList &cmd);
NativeTexture native(const Texture &texture);
ID3D12QueryHeap *native(const QueryPool &pool);
NativeDescriptorHeap native(const DescriptorHeap &heap);
NativePipeline native(const Pipeline &pipeline);
From the header: Native objects behind RI objects (P84). Borrowed pointers.
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
struct NativeDescriptorHeap {
ID3D12DescriptorHeap *resourceHeap = nullptr; // CBV_SRV_UAV, shader-visible
ID3D12DescriptorHeap *samplerHeap = nullptr; // SAMPLER, shader-visible
};
| Field | Type | Default | Description |
|---|---|---|---|
resourceHeap |
ID3D12DescriptorHeap * |
nullptr |
CBV_SRV_UAV, shader-visible |
samplerHeap |
ID3D12DescriptorHeap * |
nullptr |
SAMPLER, shader-visible |
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
struct NativePipeline {
ID3D12PipelineState *pipelineState = nullptr;
ID3D12RootSignature *rootSignature = nullptr;
};
| Field | Type | Default | Description |
|---|---|---|---|
pipelineState |
ID3D12PipelineState * |
nullptr |
|
rootSignature |
ID3D12RootSignature * |
nullptr |
// <RI/D3D12/Native.hpp>, namespace RI::D3D12
struct NativeTexture {
ID3D12Resource *resource = nullptr;
D3D12_BARRIER_LAYOUT restingLayout = D3D12_BARRIER_LAYOUT_UNDEFINED;
};
From the header: The native resource 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 |
|---|---|---|---|
resource |
ID3D12Resource * |
nullptr |
|
restingLayout |
D3D12_BARRIER_LAYOUT |
D3D12_BARRIER_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::eReadWrite});
RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16, .samplerCount = 16});
ID3D12Resource *resource = RI::D3D12::native(buffer); // borrowed: no Release
ID3D12Heap *nativeMemory = RI::D3D12::native(memory);
const RI::D3D12::NativeDescriptorHeap nativeHeap = RI::D3D12::native(heap);
// Name the buffer for PIX and debug-layer messages.
resource->SetName(L"particles");
std::printf("heap %llu bytes; view heap holds %u descriptors\n",
static_cast<unsigned long long>(nativeMemory->GetDesc().SizeInBytes),
nativeHeap.resourceHeap->GetDesc().NumDescriptors);
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 list: write one 32-bit value.
// It changes no state RI relies on (no heaps, root signature or pipeline).
ID3D12GraphicsCommandList7 *list = RI::D3D12::native(cmd);
const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER write{.Dest = buffer.gpuAddress, .Value = 42};
list->WriteBufferImmediate(1, &write, nullptr);
RI::cmdEnd(cmd);
RI::wait(device.graphicsQueue, RI::submit(device.graphicsQueue, cmd));
Example: a texture and its resting layout¶
NativeTexture::restingLayout is the layout the texture rests in between RI operations
(Image layouts are RI's business): DIRECT_QUEUE_COMMON for
textures that shaders or copies use, or RENDER_TARGET / DEPTH_STENCIL_WRITE for
attachment-only textures. If native code transitions a texture to another layout, it must return
the texture to restingLayout before RI uses it again.
// A texture's resource 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::D3D12::NativeTexture native = RI::D3D12::native(texture); // borrowed: no Release
native.resource->SetName(L"albedo");
const bool common = native.restingLayout == D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COMMON; // sampled or copied
std::printf("rests in DIRECT_QUEUE_COMMON: %s\n", common ? "yes" : "no");
Example: a query heap¶
RI::QueryPool pool = RI::createQueryPool(device, {.type = RI::QueryType::eTimestamp, .count = 2});
ID3D12QueryHeap *queryHeap = RI::D3D12::native(pool); // a TIMESTAMP query heap
queryHeap->SetName(L"frame timestamps");
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::eReadWrite});
RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16, .samplerCount = 16});
ID3D12Resource *resource = RI::D3D12::native(buffer); // borrowed: no Release
ID3D12Heap *nativeMemory = RI::D3D12::native(memory);
const RI::D3D12::NativeDescriptorHeap nativeHeap = RI::D3D12::native(heap);
// Name the buffer for PIX and debug-layer messages.
resource->SetName(L"particles");
std::printf("heap %llu bytes; view heap holds %u descriptors\n",
static_cast<unsigned long long>(nativeMemory->GetDesc().SizeInBytes),
nativeHeap.resourceHeap->GetDesc().NumDescriptors);
The Vulkan equivalents: Vulkan: native handles.
In a plain D3D12 program you already hold these objects; see
createBuffer and the other reference pages for how each is
created.
Rules¶
- Do not call
Releaseon a borrowed pointer unless you calledAddRefon it first. - If you record native commands directly on
RI::D3D12::native(cmd)between RI commands, leave the state RI relies on unchanged: the descriptor heaps, root signature, pipeline state, root constants, primitive topology, and (inside a rendering section) the viewport, scissor, and stencil reference. Commands that bind no state (such as copies,WriteBufferImmediate, queries, and debug markers) 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
restingLayoutwith an enhanced texture barrier—either directly or throughendNative—before the next RI command that may touch it and before the command list ends. - RI's buffers are placed resources that stay in the
UNDEFINEDlayout (enhanced barriers), so they never need layout transitions; synchronize native buffer work with RI'scmdBarrieror your own global barriers. - RI's root signatures enable
CBV_SRV_UAV_HEAP_DIRECTLY_INDEXEDandSAMPLER_HEAP_DIRECTLY_INDEXED, and place root constants at parameter 0 (b0, space999) when the shader declares root data.