pub struct CullStatsRing { /* private fields */ }Expand description
A ring of host-readable buffers, one per frame in flight plus one.
Implementations§
Source§impl CullStatsRing
impl CullStatsRing
Sourcepub fn new(
device: &dyn Device,
frames_in_flight: usize,
counts_clusters: bool,
) -> Option<Self>
pub fn new( device: &dyn Device, frames_in_flight: usize, counts_clusters: bool, ) -> Option<Self>
Builds the ring, or None if the device will not give it its buffers.
counts_clusters is whether the caller built an amplification stage
— ForwardRenderer::culls_clusters, not a device capability — because
that is what decides whether the cluster word is a count or the zero the
clearing pass left.
None is not an error: it is PassTimers::new’s
answer to a device with no timestamps, for the same reason. The caller
adds no copy pass and the panel says indirect.
Sourcepub const fn latest(&self) -> Option<CullStats>
pub const fn latest(&self) -> Option<CullStats>
The most recent frame whose statistics have actually landed, or None
until the first readback has answered.
Sourcepub fn begin_frame(&mut self, device: &dyn Device)
pub fn begin_frame(&mut self, device: &dyn Device)
Starts a frame: requests the readback the last frame’s copy earned, then polls everything outstanding.
Called once per frame, before add_copy_pass, and
after the previous frame was submitted — which is the frame loop’s
own order, since a frame is recorded, submitted and presented before the
next one starts. See the module docs for why the request cannot happen at
the copy.
Sourcepub fn add_copy_pass(&mut self, graph: &mut RenderGraph<'_>, stats: BufferId)
pub fn add_copy_pass(&mut self, graph: &mut RenderGraph<'_>, stats: BufferId)
Adds the copy that takes this frame’s statistics off the GPU.
stats is the id
GeneratedDraws::visible_count_id
handed back, so the graph moves the buffer into
ResourceState::TransferSrc and back out again; there is not a
hand-written barrier here, which is the whole difference between this and
crcbl::screenshot’s copy.
Declare it last, after every pass that writes that buffer: the cull dispatch, the amplification stage and the light grid all add to it, and a copy scheduled between them reads a total half the frame has not contributed to yet.
Nothing is added on a frame that finds every slot still waiting on an answer. The alternative is a copy into a buffer a readback is still mapping, and a frame’s statistics are worth far less than that: the panel keeps the number it has, still stamped with the frame it came from, and the next frame after an answer lands records a copy again.
§The destination is in the graph too, and it has to be
A slot is written by a copy this frame and was written by a copy some frames ago — two writes, in two submissions, with nothing between them. That is a write-after-write hazard exactly like the one the graph already computes for the depth transient across a frame boundary, and it is not made safe by the readback’s completion point: a timeline gates the host’s read, which is a different edge from the GPU’s second write.
So the slot is imported with what the last frame on it left it in —
Undefined the first time and TransferDst thereafter — and the copy
declares ResourceState::TransferDst on it, which makes the graph emit
TransferDst → TransferDst before the copy: a barrier whose source scope
covers that earlier submission’s transfer write. An earlier version of
this function left the destination out on the grounds that “nothing else
in the frame touches it”, which is true and beside the point — the
conflicting access is in another frame. CI’s validation layer reported
it as SYNC-HAZARD-WRITE-AFTER-WRITE … write_barriers: 0; ours cannot
see across submissions at all.
The state never changes after that, which is also what D3D12 requires: a
resource on the READBACK heap is created in COPY_DEST and pinned
there for its lifetime, so TransferDst is the only state this buffer
may ever be declared in.