formatting: Using canonical formatting on whole project

This commit is contained in:
2026-11-22 17:16:32 +01:00
parent 2910314df3
commit dd0e102348
44 changed files with 1526 additions and 3376 deletions
+84 -170
View File
@@ -32,23 +32,20 @@ macro_rules! begin_scope {
};
}
pub struct ScopeEncoder<'enc, 'prof_enc, T>
{
pub struct ScopeEncoder<'enc, 'prof_enc, T> {
encoder: &'prof_enc mut ProfilingEncoder<'enc>,
scope_index: usize,
_phantom: PhantomData<T>,
}
pub struct Scope
{
pub struct Scope {
name: &'static str,
start_index: QuerySetHandle,
end_index: Option<QuerySetHandle>,
children: Vec<usize>,
}
pub struct ProfilingEncoder<'enc>
{
pub struct ProfilingEncoder<'enc> {
device: Device,
encoder: &'enc mut CommandEncoder,
query_set_allocator: QuerySetAllocator,
@@ -59,14 +56,12 @@ pub struct ProfilingEncoder<'enc>
compute_pass: Option<(usize, ComputePass<'static>)>,
}
pub struct GpuProfile
{
pub struct GpuProfile {
scopes: Vec<Scope>,
timestamps: Buffer,
}
pub struct Frame
{
pub struct Frame {
pub name: &'static str,
pub children: Vec<usize>,
@@ -75,15 +70,12 @@ pub struct Frame
}
#[derive(Default)]
pub struct Profile
{
pub struct Profile {
pub frames: Vec<Frame>,
}
impl<'enc> ProfilingEncoder<'enc>
{
pub fn new(device: &Device, encoder: &'enc mut CommandEncoder) -> Self
{
impl<'enc> ProfilingEncoder<'enc> {
pub fn new(device: &Device, encoder: &'enc mut CommandEncoder) -> Self {
Self {
encoder,
query_set_allocator: QuerySetAllocator::new(device),
@@ -97,8 +89,7 @@ impl<'enc> ProfilingEncoder<'enc>
pub fn begin_frame<'prof_enc>(
&'prof_enc mut self,
frame_name: Option<&'static str>,
) -> ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
{
) -> ScopeEncoder<'enc, 'prof_enc, CommandEncoder> {
let scope_index = self.begin_scope(None, frame_name.unwrap_or("Frame"));
self.index_stack.push(scope_index);
ScopeEncoder {
@@ -108,17 +99,13 @@ impl<'enc> ProfilingEncoder<'enc>
}
}
fn begin_scope(&mut self, scope_index: Option<usize>, name: &'static str) -> usize
{
fn begin_scope(&mut self, scope_index: Option<usize>, name: &'static str) -> usize {
let begin_query = self.query_set_allocator.allocate();
if let Some((_, pass)) = &mut self.compute_pass
{
if let Some((_, pass)) = &mut self.compute_pass {
self.query_set_allocator
.write_compute_timestamp(pass, &begin_query);
}
else
{
} else {
self.query_set_allocator
.write_timestamp(self.encoder, &begin_query);
}
@@ -135,24 +122,19 @@ impl<'enc> ProfilingEncoder<'enc>
self.index_stack.push(new_scope_index);
// Add this new stack to parent
if let Some(scope_index) = scope_index
{
if let Some(scope_index) = scope_index {
self.scopes[scope_index].children.push(new_scope_index);
}
new_scope_index
}
fn end_scope(&mut self, scope_index: usize)
{
fn end_scope(&mut self, scope_index: usize) {
let end_query = self.query_set_allocator.allocate();
if let Some((_, pass)) = &mut self.compute_pass
{
if let Some((_, pass)) = &mut self.compute_pass {
self.query_set_allocator
.write_compute_timestamp(pass, &end_query);
}
else
{
} else {
self.query_set_allocator
.write_timestamp(self.encoder, &end_query);
}
@@ -167,8 +149,7 @@ impl<'enc> ProfilingEncoder<'enc>
scope.end_index = Some(end_query);
}
pub fn finish_profiler(self) -> GpuProfile
{
pub fn finish_profiler(self) -> GpuProfile {
let timestamp_count = self.query_set_allocator.allocated_count();
let resolve_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ProfilingEncoder::resolve_buffer"),
@@ -187,13 +168,11 @@ impl<'enc> ProfilingEncoder<'enc>
}
}
impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
{
impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, CommandEncoder> {
pub fn begin_profiled_compute_pass<'scope>(
&'scope mut self,
label: &'static str,
) -> ScopeEncoder<'enc, 'scope, ComputePass<'static>>
{
) -> ScopeEncoder<'enc, 'scope, ComputePass<'static>> {
let new_scope = self.encoder.begin_scope(Some(self.scope_index), label);
let compute_pass = self
.encoder
@@ -227,14 +206,12 @@ impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
}
}
pub fn end_scope(self)
{
pub fn end_scope(self) {
drop(self);
}
}
impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>>
{
impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>> {
pub fn begin_scope<'scope>(
&'scope mut self,
name: &'static str,
@@ -250,16 +227,13 @@ impl<'enc, 'prof_enc> ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>>
}
}
pub fn end_scope(self)
{
pub fn end_scope(self) {
drop(self);
}
}
impl<'enc, 'prof_enc, T> Drop for ScopeEncoder<'enc, 'prof_enc, T>
{
fn drop(&mut 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
{
@@ -270,59 +244,46 @@ impl<'enc, 'prof_enc, T> Drop for ScopeEncoder<'enc, 'prof_enc, T>
}
}
impl<'enc, 'prof_enc> DerefMut for ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
{
fn deref_mut(&mut self) -> &mut Self::Target
{
impl<'enc, 'prof_enc> DerefMut for ScopeEncoder<'enc, 'prof_enc, CommandEncoder> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self.encoder
}
}
impl<'enc, 'prof_enc> Deref for ScopeEncoder<'enc, 'prof_enc, CommandEncoder>
{
impl<'enc, 'prof_enc> Deref for ScopeEncoder<'enc, 'prof_enc, CommandEncoder> {
type Target = CommandEncoder;
fn deref(&self) -> &Self::Target
{
fn deref(&self) -> &Self::Target {
&*self.encoder
}
}
impl<'enc, 'prof_enc> DerefMut for ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>>
{
fn deref_mut(&mut self) -> &mut Self::Target
{
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>>
{
impl<'enc, 'prof_enc> Deref for ScopeEncoder<'enc, 'prof_enc, ComputePass<'static>> {
type Target = ComputePass<'static>;
fn deref(&self) -> &Self::Target
{
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
{
impl<'enc> DerefMut for ProfilingEncoder<'enc> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.encoder
}
}
impl<'enc> Deref for ProfilingEncoder<'enc>
{
impl<'enc> Deref for ProfilingEncoder<'enc> {
type Target = CommandEncoder;
fn deref(&self) -> &Self::Target
{
fn deref(&self) -> &Self::Target {
self.encoder
}
}
struct QuerySetAllocator
{
struct QuerySetAllocator {
device: Device,
current_size: u32,
current_id: u32,
@@ -332,40 +293,30 @@ struct QuerySetAllocator
#[derive(PartialEq, Eq)]
struct QuerySetHandle(usize, u32, u32);
impl QuerySetHandle
{
pub fn index(&self) -> usize
{
impl QuerySetHandle {
pub fn index(&self) -> usize {
self.2 as usize
}
}
impl PartialOrd for QuerySetHandle
{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering>
{
impl PartialOrd for QuerySetHandle {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for QuerySetHandle
{
fn cmp(&self, other: &Self) -> std::cmp::Ordering
{
match self.0.cmp(&other.0)
{
core::cmp::Ordering::Equal =>
{}
impl Ord for QuerySetHandle {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.0.cmp(&other.0) {
core::cmp::Ordering::Equal => {}
ord => return ord,
}
self.1.cmp(&other.1)
}
}
impl QuerySetAllocator
{
pub fn new(device: &Device) -> Self
{
impl QuerySetAllocator {
pub fn new(device: &Device) -> Self {
let current_size = 32;
let query_sets = vec![(
0,
@@ -380,27 +331,23 @@ impl QuerySetAllocator
}
}
pub fn allocated_count(&self) -> u32
{
pub fn allocated_count(&self) -> u32 {
self.query_sets
.iter()
.map(|(allocated, _, _)| *allocated)
.sum()
}
pub fn resolve_all(&self, encoder: &mut CommandEncoder, target: &Buffer)
{
pub fn resolve_all(&self, encoder: &mut CommandEncoder, target: &Buffer) {
let mut offset = 0;
for (allocated, _, query_set) in self.query_sets.iter()
{
for (allocated, _, query_set) in self.query_sets.iter() {
encoder.resolve_query_set(query_set, 0..*allocated, target, offset);
offset += *allocated as u64 * size_of::<u64>() as u64;
}
}
pub fn allocate(&mut self) -> QuerySetHandle
{
pub fn allocate(&mut self) -> QuerySetHandle {
// Check if allocation is possible
if self
.query_sets
@@ -430,8 +377,7 @@ impl QuerySetAllocator
QuerySetHandle(query_set_index, query_index, id)
}
fn new_query_set(device: &Device, size: u32, index: usize) -> QuerySet
{
fn new_query_set(device: &Device, size: u32, index: usize) -> QuerySet {
device.create_query_set(&wgpu::QuerySetDescriptor {
label: Some(format!("QuerySetAllocator::query_set_{index}").as_str()),
ty: wgpu::QueryType::Timestamp,
@@ -439,28 +385,24 @@ impl QuerySetAllocator
})
}
fn write_timestamp(&mut self, encoder: &mut CommandEncoder, handle: &QuerySetHandle)
{
fn write_timestamp(&mut self, encoder: &mut CommandEncoder, handle: &QuerySetHandle) {
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)
{
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
{
impl GpuProfile {
pub fn read_back(
self,
device: &Device,
queue: &Queue,
mut callback: impl FnMut(Profile) + Send + Sync + 'static,
)
{
) {
// Readback timestamps
wgpu::util::DownloadBuffer::read_buffer(
device,
@@ -487,43 +429,34 @@ impl GpuProfile
}
}
impl Profile
{
pub fn to_display_string(&self, q: f32) -> String
{
impl Profile {
pub fn to_display_string(&self, q: f32) -> String {
let mut str = String::new();
self.frames[0].build_string(&self.frames, 0, q, &mut str);
str
}
pub fn show(&self, ui: &mut Ui, q: f32)
{
if !self.frames.is_empty()
{
pub fn show(&self, ui: &mut Ui, q: f32) {
if !self.frames.is_empty() {
self.frames[0].show(ui, q, &self.frames);
}
}
}
impl Frame
{
fn time(&self, q: f32) -> f32
{
impl Frame {
fn time(&self, q: f32) -> f32 {
(self.end_timestamp - self.start_timestamp) as f32 * q
}
fn start(&self, q: f32) -> f32
{
fn start(&self, q: f32) -> f32 {
self.start_timestamp as f32 * q
}
fn end(&self, q: f32) -> f32
{
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)
{
fn build_string(&self, frames: &Vec<Frame>, depth: usize, q: f32, str: &mut String) {
let mut depth_string = String::new();
(0..depth).for_each(|_| depth_string.push_str(" "));
@@ -535,8 +468,7 @@ impl Frame
.for_each(|c| frames[*c].build_string(frames, depth + 1, q, str));
}
pub fn show(&self, ui: &mut Ui, q: f32, frames: &Vec<Frame>)
{
pub fn show(&self, ui: &mut Ui, q: f32, frames: &Vec<Frame>) {
let id = ui.make_persistent_id(self.name);
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
.show_header(ui, |ui| {
@@ -567,13 +499,10 @@ impl Frame
}
}
impl Profile
{
impl Profile {
// Clauderies
pub fn show_flamegraph(&self, id: egui::Id, q: f32, ui: &mut Ui) -> egui::Response
{
if self.frames.is_empty()
{
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.;
@@ -581,15 +510,13 @@ impl Profile
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()
{
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
{
if full.0 >= full.1 {
full = (0.0, 1.0); // empty or degenerate input
}
@@ -612,25 +539,21 @@ impl Profile
// --- interaction ---
let mut span = view.1 - view.0;
if resp.dragged()
{
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()
{
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
{
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()
{
if resp.double_clicked() {
view = full;
}
//ui.data_mut(|d| d.insert_temp(id, view));
@@ -647,18 +570,15 @@ impl Profile
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()
{
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
{
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
{
if x1 - x0 < 0.5 {
continue; // too thin to see
}
let y = rect.top() + depth as f32 * row_height;
@@ -666,18 +586,15 @@ impl Profile
.shrink2(vec2(0.5, 0.5));
let is_hovered = hover.is_some_and(|p| r.contains(p));
if is_hovered
{
if is_hovered {
hovered = Some(i);
}
let mut color = color_for(s.name);
if is_hovered
{
if is_hovered {
color = color.gamma_multiply(1.15);
}
painter.rect_filled(r, 2.0, color);
if r.width() > 24.0
{
if r.width() > 24.0 {
painter.with_clip_rect(r).text(
r.left_center() + vec2(3.0, 0.0),
egui::Align2::LEFT_CENTER,
@@ -689,8 +606,7 @@ impl Profile
stack.extend(s.children.iter().map(|&c| (c, depth + 1)));
}
if let Some(i) = hovered
{
if let Some(i) = hovered {
let s = &self.frames[i];
let start = s.start(q) as f64;
let end = s.end(q) as f64;
@@ -705,11 +621,9 @@ impl Profile
}
/// Stable pastel color derived from the scope name.
fn color_for(name: &str) -> egui::Color32
{
fn color_for(name: &str) -> egui::Color32 {
let mut h: u32 = 2166136261; // FNV-1a
for b in name.bytes()
{
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()
+30 -61
View File
@@ -1,24 +1,20 @@
use std::ops::Deref;
use wgpu::util::DeviceExt;
pub struct Buffer
{
pub struct Buffer {
pub label: String,
pub size: usize,
}
pub struct Group
{
pub struct Group {
pub label: &'static str,
size: Option<usize>,
pub buffers: Vec<Buffer>,
pub sub_groups: Vec<Group>,
}
impl Group
{
pub fn new(label: &'static str) -> Self
{
impl Group {
pub fn new(label: &'static str) -> Self {
Self {
label,
size: None,
@@ -27,16 +23,14 @@ impl Group
}
}
pub fn size(&self) -> usize
{
pub fn size(&self) -> usize {
self.size.unwrap_or_else(|| {
self.buffers.iter().map(|b| b.size).sum::<usize>()
+ self.sub_groups.iter().map(Self::size).sum::<usize>()
})
}
pub fn size_mut(&mut self) -> usize
{
pub fn size_mut(&mut self) -> usize {
*self.size.get_or_insert_with(|| {
self.buffers.iter().map(|b| b.size).sum::<usize>()
+ self
@@ -48,21 +42,17 @@ impl Group
}
}
pub struct MemoryProfile
{
pub struct MemoryProfile {
stack: Vec<Group>,
}
pub struct Device<'a>
{
pub struct Device<'a> {
device: wgpu::Device,
profile: &'a mut MemoryProfile,
}
impl<'a> Device<'a>
{
pub fn create_buffer(&mut self, desc: &wgpu::BufferDescriptor) -> wgpu::Buffer
{
impl<'a> Device<'a> {
pub fn create_buffer(&mut self, desc: &wgpu::BufferDescriptor) -> wgpu::Buffer {
let buffer = self.device.create_buffer(desc);
self.profile.add_buffer(Buffer {
label: desc.label.unwrap_or("Unnamed").into(),
@@ -71,8 +61,7 @@ impl<'a> Device<'a>
buffer
}
pub fn create_buffer_init(&mut self, desc: &wgpu::util::BufferInitDescriptor) -> wgpu::Buffer
{
pub fn create_buffer_init(&mut self, desc: &wgpu::util::BufferInitDescriptor) -> wgpu::Buffer {
let buffer = self.device.create_buffer_init(desc);
self.profile.add_buffer(Buffer {
label: desc.label.unwrap_or("Unnamed").into(),
@@ -93,87 +82,69 @@ impl<'a> Device<'a>
}
}
impl<'a> Drop for Device<'a>
{
fn drop(&mut self)
{
impl<'a> Drop for Device<'a> {
fn drop(&mut self) {
self.profile.close_group();
}
}
impl Deref for Device<'_>
{
impl Deref for Device<'_> {
type Target = wgpu::Device;
fn deref(&self) -> &Self::Target
{
fn deref(&self) -> &Self::Target {
&self.device
}
}
impl MemoryProfile
{
pub fn new(label: &'static str) -> Self
{
impl MemoryProfile {
pub fn new(label: &'static str) -> Self {
Self {
stack: vec![Group::new(label)],
}
}
pub fn begin(&mut self, device: &wgpu::Device) -> Device<'_>
{
pub fn begin(&mut self, device: &wgpu::Device) -> Device<'_> {
Device {
device: device.clone(),
profile: self,
}
}
pub fn terminate(mut self) -> Group
{
pub fn terminate(mut self) -> Group {
self.stack.pop().unwrap()
}
fn open_group(&mut self, label: &'static str)
{
fn open_group(&mut self, label: &'static str) {
self.stack.push(Group::new(label));
}
fn close_group(&mut self)
{
fn close_group(&mut self) {
let mut group = self.stack.pop().unwrap();
let _ = group.size_mut();
// Add it to parent if it exists
if let Some(parent) = self.stack.last_mut()
{
if let Some(parent) = self.stack.last_mut() {
parent.sub_groups.push(group);
}
else
{
} else {
self.stack.push(group);
}
}
fn add_buffer(&mut self, buffer: Buffer)
{
fn add_buffer(&mut self, buffer: Buffer) {
self.stack.last_mut().unwrap().buffers.push(buffer);
}
}
use std::fmt;
impl fmt::Display for Group
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
impl fmt::Display for Group {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt_with_indent(f, 0)
}
}
impl Group
{
fn fmt_with_indent(&self, f: &mut fmt::Formatter<'_>, indent: usize) -> fmt::Result
{
impl Group {
fn fmt_with_indent(&self, f: &mut fmt::Formatter<'_>, indent: usize) -> fmt::Result {
let prefix = " ".repeat(indent);
writeln!(
@@ -185,8 +156,7 @@ impl Group
)?;
// Print buffers
for buffer in &self.buffers
{
for buffer in &self.buffers {
writeln!(
f,
"{} ├── {} ({} bytes)",
@@ -195,8 +165,7 @@ impl Group
}
// Print subgroups
for group in self.sub_groups.iter()
{
for group in self.sub_groups.iter() {
group.fmt_with_indent(f, indent + 1)?;
}