|
| 1 | +use core::{any::type_name, marker::PhantomData}; |
| 2 | + |
| 3 | +use bevy_app::{Plugin, PreUpdate}; |
| 4 | +use bevy_diagnostic::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic}; |
| 5 | +use bevy_ecs::{resource::Resource, system::Res}; |
| 6 | +use bevy_platform::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; |
| 7 | +use bevy_render::{Extract, ExtractSchedule, RenderApp}; |
| 8 | + |
| 9 | +use crate::{Material, MaterialBindGroupAllocator}; |
| 10 | + |
| 11 | +#[derive(Default)] |
| 12 | +pub struct MaterialAllocatorDiagnosticPlugin<M: Material> { |
| 13 | + _phantom: PhantomData<M>, |
| 14 | +} |
| 15 | + |
| 16 | +impl<M: Material> MaterialAllocatorDiagnosticPlugin<M> { |
| 17 | + /// Get the [`DiagnosticPath`] for slab count |
| 18 | + pub fn slabs_diagnostic_path() -> DiagnosticPath { |
| 19 | + DiagnosticPath::from_components(["material_allocator_slabs", type_name::<M>()]) |
| 20 | + } |
| 21 | + /// Get the [`DiagnosticPath`] for total slabs size |
| 22 | + pub fn slabs_size_diagnostic_path() -> DiagnosticPath { |
| 23 | + DiagnosticPath::from_components(["material_allocator_slabs_size", type_name::<M>()]) |
| 24 | + } |
| 25 | + /// Get the [`DiagnosticPath`] for material allocations |
| 26 | + pub fn allocations_diagnostic_path() -> DiagnosticPath { |
| 27 | + DiagnosticPath::from_components(["material_allocator_allocations", type_name::<M>()]) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl<M: Material> Plugin for MaterialAllocatorDiagnosticPlugin<M> { |
| 32 | + fn build(&self, app: &mut bevy_app::App) { |
| 33 | + app.register_diagnostic( |
| 34 | + Diagnostic::new(Self::slabs_diagnostic_path()).with_suffix(" slabs"), |
| 35 | + ) |
| 36 | + .register_diagnostic( |
| 37 | + Diagnostic::new(Self::slabs_size_diagnostic_path()).with_suffix(" bytes"), |
| 38 | + ) |
| 39 | + .register_diagnostic( |
| 40 | + Diagnostic::new(Self::allocations_diagnostic_path()).with_suffix(" meshes"), |
| 41 | + ) |
| 42 | + .init_resource::<MaterialAllocatorMeasurements<M>>() |
| 43 | + .add_systems(PreUpdate, add_material_allocator_measurement::<M>); |
| 44 | + |
| 45 | + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { |
| 46 | + render_app.add_systems(ExtractSchedule, measure_allocator::<M>); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Debug, Resource)] |
| 52 | +struct MaterialAllocatorMeasurements<M: Material> { |
| 53 | + slabs: AtomicUsize, |
| 54 | + slabs_size: AtomicUsize, |
| 55 | + allocations: AtomicU64, |
| 56 | + _phantom: PhantomData<M>, |
| 57 | +} |
| 58 | + |
| 59 | +impl<M: Material> Default for MaterialAllocatorMeasurements<M> { |
| 60 | + fn default() -> Self { |
| 61 | + Self { |
| 62 | + slabs: AtomicUsize::default(), |
| 63 | + slabs_size: AtomicUsize::default(), |
| 64 | + allocations: AtomicU64::default(), |
| 65 | + _phantom: PhantomData, |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn add_material_allocator_measurement<M: Material>( |
| 71 | + mut diagnostics: Diagnostics, |
| 72 | + measurements: Res<MaterialAllocatorMeasurements<M>>, |
| 73 | +) { |
| 74 | + diagnostics.add_measurement( |
| 75 | + &MaterialAllocatorDiagnosticPlugin::<M>::slabs_diagnostic_path(), |
| 76 | + || measurements.slabs.load(Ordering::Relaxed) as f64, |
| 77 | + ); |
| 78 | + diagnostics.add_measurement( |
| 79 | + &MaterialAllocatorDiagnosticPlugin::<M>::slabs_size_diagnostic_path(), |
| 80 | + || measurements.slabs_size.load(Ordering::Relaxed) as f64, |
| 81 | + ); |
| 82 | + diagnostics.add_measurement( |
| 83 | + &MaterialAllocatorDiagnosticPlugin::<M>::allocations_diagnostic_path(), |
| 84 | + || measurements.allocations.load(Ordering::Relaxed) as f64, |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +fn measure_allocator<M: Material>( |
| 89 | + measurements: Extract<Res<MaterialAllocatorMeasurements<M>>>, |
| 90 | + allocator: Res<MaterialBindGroupAllocator<M>>, |
| 91 | +) { |
| 92 | + measurements |
| 93 | + .slabs |
| 94 | + .store(allocator.slab_count(), Ordering::Relaxed); |
| 95 | + measurements |
| 96 | + .slabs_size |
| 97 | + .store(allocator.slabs_size(), Ordering::Relaxed); |
| 98 | + measurements |
| 99 | + .allocations |
| 100 | + .store(allocator.allocations(), Ordering::Relaxed); |
| 101 | +} |
0 commit comments