List of evicted pages

This commit is contained in:
2026-10-13 21:17:24 +02:00
parent 2a85708b3a
commit a9785252c7
5 changed files with 535 additions and 125 deletions
+269 -17
View File
@@ -1,17 +1,14 @@
use std::fmt::Debug;
use std::iter::Peekable;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::DerefMut;
use egui::Sense;
use egui::Ui;
use egui::vec2;
use wgpu::Buffer;
use wgpu::BufferUsages;
use wgpu::CommandEncoder;
use wgpu::ComputePass;
use wgpu::Device;
use wgpu::QUERY_RESOLVE_BUFFER_ALIGNMENT;
use wgpu::QuerySet;
use wgpu::Queue;
@@ -21,7 +18,7 @@ macro_rules! begin_frame {
};
($encoder:ident, $name:literal) => {
let mut $encoder = $encoder.begin_frame(Some(literal));
let mut $encoder = $encoder.begin_frame(Some($name));
};
}
@@ -57,7 +54,8 @@ pub struct ProfilingEncoder<'enc>
scopes: Vec<Scope>,
index_stack: Vec<usize>,
compute_pass: Option<ComputePass<'static>>,
// Stores a compute pass when currently in one
compute_pass: Option<(usize, ComputePass<'static>)>,
}
pub struct GpuProfile
@@ -112,8 +110,18 @@ impl<'enc> ProfilingEncoder<'enc>
fn begin_scope(&mut self, scope_index: Option<usize>, name: &'static str) -> usize
{
let begin_query = self.query_set_allocator.allocate();
self.query_set_allocator
.write_timestamp(self.encoder, &begin_query);
if let Some((_, pass)) = &mut self.compute_pass
{
self.query_set_allocator
.write_compute_timestamp(pass, &begin_query);
}
else
{
self.query_set_allocator
.write_timestamp(self.encoder, &begin_query);
}
let scope = Scope {
name,
start_index: begin_query,
@@ -137,8 +145,16 @@ impl<'enc> ProfilingEncoder<'enc>
fn end_scope(&mut self, scope_index: usize)
{
let end_query = self.query_set_allocator.allocate();
self.query_set_allocator
.write_timestamp(self.encoder, &end_query);
if let Some((_, pass)) = &mut self.compute_pass
{
self.query_set_allocator
.write_compute_timestamp(pass, &end_query);
}
else
{
self.query_set_allocator
.write_timestamp(self.encoder, &end_query);
}
let scope = &mut self.scopes[scope_index];
let pop = self.index_stack.pop().unwrap();
@@ -170,9 +186,35 @@ impl<'enc> ProfilingEncoder<'enc>
}
}
impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc>
impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
{
pub fn begin_scope<'scope>(&'scope mut self, name: &'static str) -> ScopeEncoder<'enc, 'scope>
pub fn begin_profiled_compute_pass<'scope>(
&'scope mut self,
label: &'static str,
) -> ScopeEncoder<'enc, 'scope, ComputePass<'static>>
{
let new_scope = self.encoder.begin_scope(Some(self.scope_index), label);
let compute_pass = self
.encoder
.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some(label),
timestamp_writes: None,
})
.forget_lifetime();
self.encoder.compute_pass = Some((new_scope, compute_pass));
ScopeEncoder {
encoder: self.encoder,
scope_index: new_scope,
_phantom: Default::default(),
}
}
pub fn begin_scope<'scope>(
&'scope mut self,
name: &'static str,
) -> ScopeEncoder<'enc, 'scope, CommandEncoder>
where
'prof_enc: 'scope,
{
@@ -180,6 +222,7 @@ impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc>
ScopeEncoder {
encoder: self.encoder,
scope_index,
_phantom: Default::default(),
}
}
@@ -189,15 +232,44 @@ impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc>
}
}
impl<'enc, 'prof_enc> Drop for ScopeEncoder<'enc, 'prof_enc>
impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>>
{
pub fn begin_scope<'scope>(
&'scope mut self,
name: &'static str,
) -> ScopeEncoder<'enc, 'scope, ComputePass<'static>>
where
'prof_enc: 'scope,
{
let scope_index = self.encoder.begin_scope(Some(self.scope_index), name);
ScopeEncoder {
encoder: self.encoder,
scope_index,
_phantom: Default::default(),
}
}
pub fn end_scope(self)
{
drop(self);
}
}
impl<'enc, 'prof_enc, T> Drop for ScopeEncoder<'enc, 'prof_enc, T>
{
fn drop(&mut self)
{
if let Some((parent_scope, _)) = self.encoder.compute_pass
&& parent_scope == self.scope_index
{
// Finish compute pass by droping it
self.encoder.compute_pass = None;
}
self.encoder.end_scope(self.scope_index);
}
}
impl<'enc, 'prof_enc> DerefMut for ScopeEncoder<'enc, 'prof_enc>
impl<'enc, 'prof_enc> DerefMut for ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
{
fn deref_mut(&mut self) -> &mut Self::Target
{
@@ -205,7 +277,7 @@ impl<'enc, 'prof_enc> DerefMut for ScopeEncoder<'enc, 'prof_enc>
}
}
impl<'enc, 'prof_enc> Deref for ScopeEncoder<'enc, 'prof_enc>
impl<'enc, 'prof_enc> Deref for ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
{
type Target = CommandEncoder;
fn deref(&self) -> &Self::Target
@@ -214,6 +286,23 @@ impl<'enc, 'prof_enc> Deref for ScopeEncoder<'enc, 'prof_enc>
}
}
impl<'enc, 'prof_enc> DerefMut for ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>>
{
fn deref_mut(&mut self) -> &mut Self::Target
{
&mut self.encoder.compute_pass.as_mut().unwrap().1
}
}
impl<'enc, 'prof_enc> Deref for ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>>
{
type Target = ComputePass<'static>;
fn deref(&self) -> &Self::Target
{
&self.encoder.compute_pass.as_ref().unwrap().1
}
}
impl<'enc> DerefMut for ProfilingEncoder<'enc>
{
fn deref_mut(&mut self) -> &mut Self::Target
@@ -354,6 +443,12 @@ impl QuerySetAllocator
let query_set = &self.query_sets[handle.0].2;
encoder.write_timestamp(query_set, handle.1);
}
fn write_compute_timestamp(&mut self, pass: &mut ComputePass, handle: &QuerySetHandle)
{
let query_set = &self.query_sets[handle.0].2;
pass.write_timestamp(query_set, handle.1);
}
}
impl GpuProfile
@@ -373,14 +468,15 @@ impl GpuProfile
move |buffer| {
let buffer = buffer.unwrap();
let timestamps: Vec<u64> = bytemuck::pod_collect_to_vec(&buffer);
let start = timestamps.first().copied().unwrap_or(0);
let frames = self
.scopes
.into_iter()
.map(|scope| Frame {
name: scope.name,
children: scope.children,
start_timestamp: timestamps[scope.start_index.index()],
end_timestamp: timestamps[scope.end_index.unwrap().index()],
start_timestamp: timestamps[scope.start_index.index()] - start,
end_timestamp: timestamps[scope.end_index.unwrap().index()] - start,
})
.collect::<Vec<_>>();
@@ -415,6 +511,16 @@ impl Frame
(self.end_timestamp - self.start_timestamp) as f32 * q
}
fn start(&self, q: f32) -> f32
{
self.start_timestamp as f32 * q
}
fn end(&self, q: f32) -> f32
{
self.end_timestamp as f32 * q
}
fn build_string(&self, frames: &Vec<Frame>, depth: usize, q: f32, str: &mut String)
{
let mut depth_string = String::new();
@@ -459,3 +565,149 @@ impl Frame
});
}
}
impl Profile
{
// Clauderies
pub fn show_flamegraph(&self, id: egui::Id, q: f32, ui: &mut Ui) -> egui::Response
{
if self.frames.is_empty()
{
return ui.label("empty");
}
let row_height = 20.;
// Full time range and max depth of the tree.
let mut full = (f64::INFINITY, f64::NEG_INFINITY);
let mut max_depth = 0usize;
let mut stack: Vec<(usize, usize)> = [0].iter().map(|&r| (r, 0)).collect();
while let Some((i, d)) = stack.pop()
{
let s = &self.frames[i];
full = (full.0.min(s.start(q) as f64), full.1.max(s.end(q) as f64));
max_depth = max_depth.max(d);
stack.extend(s.children.iter().map(|&c| (c, d + 1)));
}
if full.0 >= full.1
{
full = (0.0, 1.0); // empty or degenerate input
}
// The view is stored as fractions of the full range (not absolute times), so it
// stays valid when the data changes from frame to frame.
let fspan = full.1 - full.0;
let norm: (f64, f64) = ui.data_mut(|d| d.get_temp(id)).unwrap_or((0.0, 1.0));
let mut view = (full.0 + norm.0 * fspan, full.0 + norm.1 * fspan);
if !(view.1 > view.0) || !view.0.is_finite() || !view.1.is_finite()
{
view = full;
}
let size = vec2(ui.available_width(), (max_depth + 1) as f32 * row_height);
let (resp, painter) = ui.allocate_painter(size, egui::Sense::click_and_drag());
let rect = resp.rect;
let w = rect.width().max(1.0) as f64;
// --- interaction ---
let mut span = view.1 - view.0;
if resp.dragged()
{
let dt = -(resp.drag_delta().x as f64) / w * span;
view = (view.0 + dt, view.1 + dt);
}
if let Some(p) = resp.hover_pos()
{
let (zoom, scroll) = ui.input(|i| (i.zoom_delta(), i.smooth_scroll_delta.y));
let z = zoom as f64 * (scroll as f64 * 0.005).exp();
if (z - 1.0).abs() > 1e-6
{
let frac = ((p.x - rect.left()) as f64 / w).clamp(0.0, 1.0);
let t_cursor = view.0 + frac * span;
span = (span / z).clamp((full.1 - full.0) * 1e-6, (full.1 - full.0) * 10.0);
view = (t_cursor - frac * span, t_cursor + (1.0 - frac) * span);
}
}
if resp.double_clicked()
{
view = full;
}
//ui.data_mut(|d| d.insert_temp(id, view));
ui.data_mut(|d| d.insert_temp(id, ((view.0 - full.0) / fspan, (view.1 - full.0) / fspan)));
let span = view.1 - view.0;
let to_x = |t: f64| rect.left() + ((t - view.0) / span) as f32 * rect.width();
// --- drawing ---
painter.rect_filled(rect, 0.0, ui.visuals().extreme_bg_color);
let font = egui::FontId::proportional(12.0);
let hover = resp.hover_pos();
let mut hovered: Option<usize> = None;
let mut stack: Vec<(usize, usize)> = [0].iter().map(|&r| (r, 0)).collect();
while let Some((i, depth)) = stack.pop()
{
let s = &self.frames[i];
let start = s.start(q) as f64;
let end = s.end(q) as f64;
if end < view.0 || start > view.1
{
continue; // off-screen: children are inside the parent, skip them too
}
let (x0, x1) = (to_x(start).max(rect.left()), to_x(end).min(rect.right()));
if x1 - x0 < 0.5
{
continue; // too thin to see
}
let y = rect.top() + depth as f32 * row_height;
let r = egui::Rect::from_min_max(egui::pos2(x0, y), egui::pos2(x1, y + row_height))
.shrink2(vec2(0.5, 0.5));
let is_hovered = hover.is_some_and(|p| r.contains(p));
if is_hovered
{
hovered = Some(i);
}
let mut color = color_for(&s.name);
if is_hovered
{
color = color.gamma_multiply(1.15);
}
painter.rect_filled(r, 2.0, color);
if r.width() > 24.0
{
painter.with_clip_rect(r).text(
r.left_center() + vec2(3.0, 0.0),
egui::Align2::LEFT_CENTER,
&s.name,
font.clone(),
egui::Color32::BLACK,
);
}
stack.extend(s.children.iter().map(|&c| (c, depth + 1)));
}
if let Some(i) = hovered
{
let s = &self.frames[i];
let start = s.start(q) as f64;
let end = s.end(q) as f64;
resp.clone().on_hover_ui_at_pointer(|ui| {
ui.strong(s.name);
ui.label(format!("duration: {:.3}", end - start));
ui.label(format!("start: {:.3} end: {:.3}", start, end));
});
}
resp
}
}
/// Stable pastel color derived from the scope name.
fn color_for(name: &str) -> egui::Color32
{
let mut h: u32 = 2166136261; // FNV-1a
for b in name.bytes()
{
h = (h ^ b as u32).wrapping_mul(16777619);
}
egui::ecolor::Hsva::new((h % 360) as f32 / 360.0, 0.45, 0.9, 1.0).into()
}