Skip to content

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_8 profile (RI's CMake shader rule equine_add_shaders produces DXIL only, without SPIR-V, for lib_ profiles).
  • RI::createWorkGraph(device, desc) creates a graph containing every node in desc.library. Because RI does not reflect root data from shader libraries, pass the byte size of the EQ_ROOT_DATA block declared by the nodes in desc.rootDataSize (or 0 if none). The returned WorkGraph's backing field (WorkGraphMemory) reports the required backing memory range in bytes (minSize and maxSize).
  • RI::workGraphEntry(graph, node, arrayIndex = 0) returns an entry node's index for GraphInput::entry.
  • RI::bindWorkGraphMemory(device, graph, backing, offset, size) binds a range of a BufferUsage::eReadWrite buffer as the graph's backing memory (offset must be a multiple of 8 bytes, and size must be at least graph.backing.minSize; giving up to maxSize can be faster). That range belongs to the graph until you call bindWorkGraphMemory again or destroy the graph with RI::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; subsequent RI::cmdSetRootData calls 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 one GraphInput for a single entry node, or several for multiple entry nodes.
  • RI::cmdDispatchGraphIndirect(cmd, inputAddress, multiNode) launches the graph from GPU-written input at inputAddress (a GPU address such as buffer.gpuAddress + offset) using D3D12's D3D12_NODE_GPU_INPUT layout (when multiNode is false) or D3D12_MULTI_NODE_GPU_INPUT layout (when multiNode is true).

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
WorkGraph createWorkGraph(Device &device, const WorkGraphDesc &desc);
// <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).

// <RI/RI.hpp>, namespace RI
void cmdSetWorkGraph(CommandList &cmd, const WorkGraph &graph);

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::createWorkGraph builds an ID3D12StateObject containing 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::bindWorkGraphMemory records SetProgram with D3D12_SET_WORK_GRAPH_FLAG_INITIALIZE on RI's internal command list and waits for completion. RI::cmdSetWorkGraph sets the compute root signature and records SetProgram without INITIALIZE; RI::cmdDispatchGraph and RI::cmdDispatchGraphIndirect then record DispatchGraph.

Rules and errors

  • Calling any work-graph function on Vulkan, or calling RI::createWorkGraph on a D3D12 GPU where device.limits.workGraphs is false, reports an unsupported error.
  • Passing desc.rootDataSize larger than device.limits.maxRootDataSize to RI::createWorkGraph reports an unsupported error.
  • Passing an unknown node name or arrayIndex to RI::workGraphEntry reports a programmer error.
  • Set a descriptor heap with RI::cmdSetHeap before calling RI::cmdSetWorkGraph, as before a pipeline (checked in validation builds).
  • In RI::bindWorkGraphMemory, the backing buffer must have BufferUsage::eReadWrite and size must be at least graph.backing.minSize bytes (programmer error otherwise); in validation builds, RI also checks that offset is a multiple of 8 bytes and that [offset, offset + size) fits in the buffer.
  • Calling RI::cmdSetWorkGraph before RI::bindWorkGraphMemory is a programmer error.
  • Because RI::cmdSetWorkGraph replaces the command list's compute pipeline state, call RI::cmdSetPipeline again before recording ordinary compute dispatches.
  • Node-local root arguments and graphs with several programs are not modelled yet; use native access.