use super::{
FrameComposerError, FrameStatistics, ReadyGeometry, ReadyItem, ReadySource, ResolvedViewport,
add_tessellation_stats, prepare_streaming_scene_resolved,
};
use crate::renderer::{
CameraUniform, PreparedDrawBatch, RendererFrameError, RendererPreparationMetrics, Scene,
TessellationStats, Vertex,
};
#[derive(Clone, Copy)]
struct SceneProof<'frame> {
scene: &'frame Scene,
uniform: CameraUniform,
ready_index: usize,
statistics: TessellationStats,
}
#[cfg(test)]
mod tests;
#[derive(Default)]
pub(super) struct StreamingSceneProofs<'frame> {
entries: [Option<SceneProof<'frame>>; 8],
next: usize,
pub(super) preparation: RendererPreparationMetrics,
}
impl<'frame> StreamingSceneProofs<'frame> {
#[allow(clippy::too_many_arguments)]
pub(super) fn prepare(
&mut self,
scene: &'frame Scene,
uniform: CameraUniform,
viewport: ResolvedViewport,
vertices: &mut Vec<Vertex>,
ready: &mut Vec<ReadyItem<'frame>>,
statistics: &mut FrameStatistics,
aggregate: &mut TessellationStats,
batches: &mut Vec<PreparedDrawBatch>,
) -> Result<(), FrameComposerError> {
let cached = self.entries.iter().flatten().find(|proof| {
std::ptr::eq(proof.scene, scene)
&& bytemuck::bytes_of(&proof.uniform) == bytemuck::bytes_of(&uniform)
});
if let Some(proof) = cached {
let Some(ReadyItem::Geometry(source)) = ready.get(proof.ready_index) else {
return Err(RendererFrameError::InvalidGeometryTransform.into());
};
let additional = proof.statistics.vertex_count;
batches.try_reserve(source.batches.len()).map_err(|_| {
FrameComposerError::AllocationFailed {
requested_bytes: source
.batches
.len()
.saturating_mul(std::mem::size_of::<PreparedDrawBatch>()),
}
})?;
batches.extend_from_slice(&source.batches);
*statistics = statistics.adding(FrameStatistics {
pass_count: 1,
command_count: scene.command_count(),
vertex_count: additional,
streaming_vertex_count: additional,
upload_bytes: std::mem::size_of::<CameraUniform>(),
draw_calls: batches.len(),
..FrameStatistics::default()
});
add_tessellation_stats(aggregate, proof.statistics);
ready.push(ReadyItem::Geometry(ReadyGeometry {
source: ReadySource::Streaming,
vertex_count: additional,
batches: std::mem::take(batches),
camera_uniform: uniform,
viewport,
}));
return Ok(());
}
let ready_index = ready.len();
let mut source_statistics = TessellationStats::default();
prepare_streaming_scene_resolved(
scene,
uniform,
viewport,
vertices,
ready,
statistics,
&mut source_statistics,
batches,
&mut self.preparation,
)?;
add_tessellation_stats(aggregate, source_statistics);
self.entries[self.next] = Some(SceneProof {
scene,
uniform,
ready_index,
statistics: source_statistics,
});
self.next = (self.next + 1) % self.entries.len();
Ok(())
}
}