Work graphs¶
What they are¶
A work graph is a set of compute shaders (nodes) that launch each other on the GPU: a node writes output records, and the GPU schedules the nodes that consume them without a CPU round trip.
Work graphs are D3D12 only, and require device.limits.workGraphs to be true (NVIDIA
Ampere or AMD RDNA3 and newer, supporting D3D12 work graphs tier 1.0). On Vulkan, every
work-graph function reports an unsupported error (VK_AMDX_shader_enqueue is provisional
and AMD-only).
- Write all nodes in one HLSL library and compile it with the
lib_6_8profile (RI's CMake shader ruleequine_add_shadersproduces DXIL only, without SPIR-V, forlib_profiles). RI::createWorkGraph(device, desc)creates a graph containing every node indesc.library. Because RI does not reflect root data from shader libraries, pass the byte size of theEQ_ROOT_DATAblock declared by the nodes indesc.rootDataSize(or0if none). The returnedWorkGraph'sbackingfield (WorkGraphMemory) reports the required backing memory range in bytes (minSizeandmaxSize).RI::workGraphEntry(graph, node, arrayIndex = 0)returns an entry node's index forGraphInput::entry.RI::bindWorkGraphMemory(device, graph, backing, offset, size)binds a range of aBufferUsage::eReadWritebuffer as the graph's backing memory (offsetmust be a multiple of 8 bytes, andsizemust be at leastgraph.backing.minSize; giving up tomaxSizecan be faster). That range belongs to the graph until you callbindWorkGraphMemoryagain or destroy the graph withRI::destroyWorkGraph(device, graph): do not place other data in it. RI initializes the backing memory immediately with an internal submission that waits on the CPU for completion (similar to a new texture's initial layout transition).RI::cmdSetWorkGraph(cmd, graph)binds the graph on a command list; subsequentRI::cmdSetRootDatacalls supply root data to its nodes.RI::cmdDispatchGraph(cmd, inputs)launches the graph with CPU-side records (which are copied into the command list): pass oneGraphInputfor a single entry node, or several for multiple entry nodes.RI::cmdDispatchGraphIndirect(cmd, inputAddress, multiNode)launches the graph from GPU-written input atinputAddress(a GPU address such asbuffer.gpuAddress + offset) using D3D12'sD3D12_NODE_GPU_INPUTlayout (whenmultiNodeisfalse) orD3D12_MULTI_NODE_GPU_INPUTlayout (whenmultiNodeistrue).
Why a bind, and not a flag per call: D3D12 requires the backing memory to be initialized
before a graph first uses it, and again after anything else has used that memory. Because RI
cannot see what else you place in that memory or the order in which the GPU executes your
command lists, it ties the memory range to the graph explicitly and initializes it during
RI::bindWorkGraphMemory. Call
RI::bindWorkGraphMemory again to re-initialize and reset it.
Signature¶
// <RI/RI.hpp>, namespace RI
void bindWorkGraphMemory(Device &device, WorkGraph &graph, const Buffer &backing, uint64 offset, uint64 size);
From the header: Gives the graph its backing memory (a range of an eReadWrite buffer, at least backing.minSize bytes). The range belongs to the graph until it is bound again or the graph is destroyed: place nothing else over it. RI initializes it at once, with an internal submission that waits (P62).
From the header: Sets the graph (with its bound memory): later cmdSetRootData calls feed its nodes.
// <RI/RI.hpp>, namespace RI
void cmdDispatchGraph(CommandList &cmd, std::span<const GraphInput> inputs);
From the header: Launches the graph with CPU records: one entry node, or several (P62).
Example¶
[Shader("node")]
[NodeLaunch("broadcasting")]
[NodeDispatchGrid(1, 1, 1)]
[NumThreads(64, 1, 1)]
void entry(DispatchNodeInputRecord<EntryRecord> input, uint thread : SV_GroupThreadID,
[MaxRecords(64)] NodeOutput<WorkRecord> work) { /* ... */ }
RI::WorkGraph graph = RI::createWorkGraph(device, {.library = library, .rootDataSize = sizeof(GraphRoot)});
RI::bindWorkGraphMemory(device, graph, backing, 0, graph.backing.minSize); // once
// ... then, while recording:
RI::cmdSetHeap(cmd, heap);
RI::cmdSetWorkGraph(cmd, graph);
RI::Helpers::cmdSetRootData(cmd, GraphRoot{.outputSlot = 0});
const RI::GraphInput input{.entry = RI::workGraphEntry(graph, "entry"), .records = &record, .recordCount = 1,
.recordStride = sizeof(record)};
RI::cmdDispatchGraph(cmd, {&input, 1});
How RI does it¶
- D3D12:
RI::createWorkGraphbuilds anID3D12StateObjectcontaining the DXIL library, a global root signature constructed by RI (with root constants and directly indexed descriptor heaps, matching other RI pipelines), and a work graph including all nodes in the library (D3D12_WORK_GRAPH_FLAG_INCLUDE_ALL_AVAILABLE_NODES).RI::bindWorkGraphMemoryrecordsSetProgramwithD3D12_SET_WORK_GRAPH_FLAG_INITIALIZEon RI's internal command list and waits for completion.RI::cmdSetWorkGraphsets the compute root signature and recordsSetProgramwithoutINITIALIZE;RI::cmdDispatchGraphandRI::cmdDispatchGraphIndirectthen recordDispatchGraph.
Rules and errors¶
- Calling any work-graph function on Vulkan, or calling
RI::createWorkGraphon a D3D12 GPU wheredevice.limits.workGraphsisfalse, reports an unsupported error. - Passing
desc.rootDataSizelarger thandevice.limits.maxRootDataSizetoRI::createWorkGraphreports an unsupported error. - Passing an unknown node name or
arrayIndextoRI::workGraphEntryreports a programmer error. - Set a descriptor heap with
RI::cmdSetHeapbefore callingRI::cmdSetWorkGraph, as before a pipeline (checked in validation builds). - In
RI::bindWorkGraphMemory, the backing buffer must haveBufferUsage::eReadWriteandsizemust be at leastgraph.backing.minSizebytes (programmer error otherwise); in validation builds, RI also checks thatoffsetis a multiple of 8 bytes and that[offset, offset + size)fits in the buffer. - Calling
RI::cmdSetWorkGraphbeforeRI::bindWorkGraphMemoryis a programmer error. - Because
RI::cmdSetWorkGraphreplaces the command list's compute pipeline state, callRI::cmdSetPipelineagain before recording ordinary compute dispatches. - Node-local root arguments and graphs with several programs are not modelled yet; use native access.