Skip to main content

Module graph

Module graph 

Source
Expand description

The render graph: passes declare what they touch, and the graph works out the barriers.

RenderGraph ──add_render_pass()──▶ declarations
     │                                  │
     │  compile()                       │  reads/writes of virtual resources
     ▼                                  ▼
CompiledGraph ──▶ ordered passes + exact barriers + layout transitions
     │                        │
     │  dump()                │  execute(device, pool, encoder)
     ▼                        ▼
  text a human reads    the only code in the engine that calls
                        `CommandEncoder::pipeline_barrier`

Stage 2 §2.4 states the contract in one sentence: “passes declare reads/writes of virtual resources; graph compiles to ordered passes + exact sync2 barriers + image layout transitions. No manual barriers outside the graph, ever.” The sandbox used to hand-write two image barriers around its frame; it no longer contains the word.

§Deliberately not a scheduler

The same section’s risk list says: “Graph over-engineering. MVP graph = linear pass list with computed barriers. No multi-queue scheduling, no reordering. Resist.” So:

  • Passes execute in declaration order. There is no topological sort, because there is nothing to sort — the order the caller wrote is the dependency order, and a graph that reordered would have to prove it had not changed the picture.
  • No dead-pass culling. Declaring a pass runs it. Culling is the feature that makes a graph unpredictable (“why did my debug pass not run?”) in exchange for saving work a caller could have simply not declared.
  • No multi-queue scheduling. Every pass names a queue and the barrier model carries QueueTransfer, so a dedicated transfer queue is additive later — which stage 2’s corrections require “from the start so a dedicated transfer queue is additive later rather than a barrier-model rewrite”. Nothing decides to use a second queue.

What it does do is the part that is genuinely error-prone by hand: track every resource’s state, emit exactly the transitions that are needed, batch them into one call per pass, alias transients whose lifetimes do not overlap, and return every imported resource to the state its owner requires.

§The physical resource outlives the frame

A graph is built and thrown away every frame. The images are not: a steady-state frame gets the same ImageHandle out of the TransientPool it got last frame, and a graph is aliasing when two of its virtual images land on one of them. So state tracking is per physical resource — and a physical resource’s history does not start at this frame’s first pass.

Treating it as though it did is a real bug and not a subtle one. A barrier whose source state is ResourceState::Undefined carries no source scope at all — on Vulkan it expands to srcStageMask = NONE, srcAccessMask = NONE — so a frame that opens by transitioning its depth buffer from Undefined orders that transition against nothing, while the previous frame’s depth writes to the very same image may still be in flight. Two submissions, no dependency, both writing: a write-after-write hazard that CRCBL_VK_SYNC_VALIDATION=1 reports as write_barriers: 0, and that a driver is free to resolve either way.

So the graph asks the pool what it left each physical resource in, and the first barrier that touches one names that as its source:

frame n-1 ─ forward pass writes depth ─▶ pool remembers DepthStencilWrite
                                               │
frame n   ─ compile(&pool) ────────────────────┘
            first barrier: DepthStencilWrite ──▶ DepthStencilWrite
            (a real source scope, so the two frames are ordered)

Aliasing within a frame is the same story one scale down, and used to have the same hole: handing a slot from one virtual image to the next does not change which VkImage it is, so the incoming resource’s barrier must name the state the outgoing one left, not Undefined. Discarding contents is only free when there are contents worth discarding — with one handle and one description there is nothing to discard, and pretending otherwise costs the source scope that made the two passes ordered.

Imported resources are the owner’s problem, deliberately: the graph cannot know what happened to a swapchain image before it was handed one, so ImportedImage::initial is a declaration and the owner is the one who has to be right. See its docs for what makes Undefined correct there.

It is a declaration the graph audits, though, wherever auditing is possible. An import that says InitialClaim::Tracked is one its owner keeps across frames, so the pool remembers what the last graph left it in — ImportedImage::final_state, which the trailing barrier reaches — and the next RenderGraph::compile rejects an initial that disagrees. That is GraphError::ImportStateMismatch, and it is the compile-time form of two bugs this engine has actually shipped: a readback path and a 1×1 shadow placeholder, each declaring a state the image was not in on every frame after the first, and each found only by a validation layer on CI. An InitialClaim::Acquired import is exempt because a semaphore, not a barrier, is what orders it; the variant’s docs say why that is a real exemption rather than an escape hatch.

A buffer import is audited the same way and has no exemption at all. ImportedBuffer::initial is the identical declaration about a resource with no layout, which is what makes getting it wrong worse rather than milder: a capture of a mislabelled image at least shows the wrong layout, and a buffer whose barrier lost its source scope looks like nothing until the numbers it carries are wrong. So every buffer import is checked against the ledger — GraphError::BufferImportStateMismatch — and ImportedBuffer carries no claim, because nothing hands this engine a buffer from behind a semaphore the way a swapchain hands it an image.

§Compilation is pure, and that is what makes it testable

RenderGraph::compile takes no device and touches no GPU. It reads the pool — a plain table of “what was this last used for” — and produces a CompiledGraph whose barriers name virtual resources; only CompiledGraph::execute resolves those to handles. That split is why docs/notes/process.md can call the graph-compile suite a non-negotiable anchor and have it mean something: the interesting half runs on any machine, in microseconds, with no ICD in the room — including the cross-frame half, which needs two compile calls against one TransientPool::new and no driver at all.

§The graph explains itself

CompiledGraph::dump prints the pass order, every barrier with its from and to states, and which transients ended up sharing a physical resource. Stage 2 §2.4 asks for exactly this (“the graph must be able to explain itself”) and makes “graph dump readable and correct for the sandbox frame” a P1 exit criterion.

Structs§

BufferId
A virtual buffer inside one graph. See ImageId.
CompiledGraph
A compiled graph: ordered passes, exact barriers, and a transient plan.
CompiledPass
One pass, after compilation.
GraphBarriers
One batch of barriers, emitted as a single pipeline_barrier call.
GraphBufferBarrier
A barrier the graph computed, in terms of a virtual buffer.
GraphImageBarrier
A barrier the graph computed, in terms of a virtual image.
ImageId
A virtual image inside one graph.
ImportedBuffer
A buffer the graph did not create, and does not own.
ImportedImage
An image the graph did not create, and does not own.
PassBuilder
Declares one pass. #[must_use] because a builder that is dropped rather than executed adds nothing to the graph, and a silently missing pass is a long afternoon.
PassContext
What a pass body is handed.
RenderGraph
A frame’s worth of passes, before compilation.

Enums§

Attachment
How a pass attaches an image, if it attaches it at all.
GraphError
Why a graph could not be compiled.
ImportField
Which field of a repeated import declaration disagreed with the first one.
InitialClaim
What backs an importer’s claim about ImportedImage::initial.
PassKind
Which scope a pass opens.