Skip to main content

sui_render_wgpu/
diagnostics.rs

1#[cfg(test)]
2use crate::draw::PreparedFrameBatches;
3use crate::retained::RetainedCompositorFrameStats;
4#[cfg(test)]
5use crate::submission::{
6    ANALYTIC_QUAD_INSTANCE_SIZE, COMPACT_VERTEX_SIZE, EXTENDED_QUAD_INSTANCE_SIZE,
7    SOLID_VERTEX_SIZE, TEXT_ATLAS_INSTANCE_SIZE,
8};
9use crate::text::TextFrameStats;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
12pub struct RetainedPacketRebuildStats {
13    pub new_count: usize,
14    pub coordinate_space_count: usize,
15    /// Always zero: packets are compared directly rather than by signature.
16    pub signature_count: usize,
17    pub scene_count: usize,
18    pub state_count: usize,
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub(crate) enum PacketRebuildReason {
23    NewPacket,
24    CoordinateSpace,
25    Scene,
26    State,
27}
28
29impl RetainedPacketRebuildStats {
30    pub const fn new(
31        new_count: usize,
32        coordinate_space_count: usize,
33        signature_count: usize,
34        scene_count: usize,
35        state_count: usize,
36    ) -> Self {
37        Self {
38            new_count,
39            coordinate_space_count,
40            signature_count,
41            scene_count,
42            state_count,
43        }
44    }
45
46    pub(crate) fn record_reason(&mut self, reason: PacketRebuildReason) {
47        match reason {
48            PacketRebuildReason::NewPacket => self.new_count += 1,
49            PacketRebuildReason::CoordinateSpace => self.coordinate_space_count += 1,
50            PacketRebuildReason::Scene => self.scene_count += 1,
51            PacketRebuildReason::State => self.state_count += 1,
52        }
53    }
54
55    pub const fn total_count(&self) -> usize {
56        self.new_count
57            + self.coordinate_space_count
58            + self.signature_count
59            + self.scene_count
60            + self.state_count
61    }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Default)]
65pub struct RendererFrameStats {
66    /// Scene/output submissions; atlas resource preparation is reported separately.
67    pub queue_submit_count: usize,
68    pub text_atlas_allocate_time_us: u64,
69    pub text_atlas_clear_time_us: u64,
70    pub text_atlas_copy_time_us: u64,
71    pub text_atlas_create_bind_group_time_us: u64,
72    /// Adapter/device acquisition and shared renderer resources. Background work
73    /// can overlap CPU layout; this is not an additive frame phase.
74    pub device_prepare_time_us: u64,
75    /// Time first use blocks on device preparation (synchronous work or joining
76    /// background preparation). Included in renderer/registration wall time.
77    pub device_prepare_wait_time_us: u64,
78    /// Offscreen color-target preparation (including resize allocations).
79    pub target_prepare_time_us: u64,
80    pub text_engine_init_time_us: u64,
81    /// Shader and pipeline creation; also included in pass encoding time.
82    pub pipeline_create_time_us: u64,
83    pub pipeline_create_count: usize,
84    pub pass_count: usize,
85    pub draw_count: usize,
86    pub uploaded_vertex_bytes: u64,
87    pub text_glyph_instance_count: usize,
88    pub text_vertex_bytes: u64,
89    pub visible_layer_count: usize,
90    pub direct_packet_count: usize,
91    pub prepared_fragment_cache_hits: usize,
92    pub prepared_fragment_build_count: usize,
93    pub retained_state_update_time_us: u64,
94    pub composition_time_us: u64,
95    pub retained_scene_traversal_time_us: u64,
96    pub snapshot_commands_replayed: usize,
97    pub snapshot_commands_reused: usize,
98    pub retained_packet_build_time_us: u64,
99    pub retained_packet_build_count: usize,
100    pub retained_packet_rebuilds: RetainedPacketRebuildStats,
101    pub retained_packet_normalize_time_us: u64,
102    pub retained_packet_signature_time_us: u64,
103    pub retained_packet_raster_state_init_time_us: u64,
104    pub retained_packet_scene_build_time_us: u64,
105    pub retained_packet_command_count: usize,
106    pub retained_packet_text_command_count: usize,
107    pub retained_packet_path_command_count: usize,
108    pub retained_packet_clip_path_command_count: usize,
109    pub retained_packet_image_command_count: usize,
110    pub retained_packet_rect_command_count: usize,
111    pub retained_packet_text_command_time_us: u64,
112    pub retained_packet_path_command_time_us: u64,
113    pub retained_packet_clip_path_command_time_us: u64,
114    pub retained_packet_image_command_time_us: u64,
115    pub retained_packet_rect_command_time_us: u64,
116    pub text_atlas_miss_count: usize,
117    pub text_atlas_miss_time_us: u64,
118    pub surface_acquire_time_us: u64,
119    pub resource_collection_time_us: u64,
120    pub bind_group_prepare_time_us: u64,
121    pub image_bind_group_time_us: u64,
122    pub analytic_path_bind_group_time_us: u64,
123    pub analytic_path_bind_group_miss_count: usize,
124    pub analytic_path_bind_group_upload_bytes: u64,
125    pub text_atlas_bind_group_time_us: u64,
126    pub text_atlas_upload_copy_time_us: u64,
127    pub text_atlas_upload_write_time_us: u64,
128    pub text_atlas_upload_bytes: u64,
129    pub batch_prepare_time_us: u64,
130    pub gpu_upload_time_us: u64,
131    pub pass_encode_time_us: u64,
132    pub queue_submit_time_us: u64,
133    /// Upload/scene command-buffer finishing, a subset of queue_submit_time_us.
134    pub command_finish_time_us: u64,
135    pub surface_present_time_us: u64,
136    pub retained_packet_hotspot: Option<RendererPacketHotspot>,
137}
138
139#[derive(Debug, Clone, PartialEq, Eq)]
140pub struct RendererPacketHotspot {
141    pub container_layer_id: Option<u64>,
142    pub owner_widget_id: Option<u64>,
143    pub segment_index: u32,
144    pub total_time_us: u64,
145    pub scene_build_time_us: u64,
146    pub command_count: usize,
147    pub text_command_count: usize,
148    pub path_command_count: usize,
149    pub rect_command_count: usize,
150    pub text_command_time_us: u64,
151    pub path_command_time_us: u64,
152    pub rect_command_time_us: u64,
153    pub text_sample: Option<String>,
154}
155
156impl RendererFrameStats {
157    #[cfg(test)]
158    pub(crate) fn from_prepared_frame(prepared: &PreparedFrameBatches) -> Self {
159        Self::from_prepared_counts(
160            prepared.passes.len().max(1),
161            prepared
162                .passes
163                .iter()
164                .map(|pass| pass.clip_paths.len() + pass.draws.len())
165                .sum(),
166            (prepared.solid_vertices.len() as u64 + prepared.clip_vertices.len() as u64)
167                * SOLID_VERTEX_SIZE
168                + prepared.scene_vertices.len() as u64 * COMPACT_VERTEX_SIZE
169                + prepared.analytic_vertices.len() as u64 * ANALYTIC_QUAD_INSTANCE_SIZE
170                + prepared.extended_vertices.len() as u64 * EXTENDED_QUAD_INSTANCE_SIZE
171                + prepared.text_instances.len() as u64 * TEXT_ATLAS_INSTANCE_SIZE,
172        )
173    }
174
175    pub(crate) fn from_prepared_counts(
176        pass_count: usize,
177        draw_count: usize,
178        uploaded_vertex_bytes: u64,
179    ) -> Self {
180        Self {
181            queue_submit_count: 0,
182            text_atlas_allocate_time_us: 0,
183            text_atlas_clear_time_us: 0,
184            text_atlas_copy_time_us: 0,
185            text_atlas_create_bind_group_time_us: 0,
186            device_prepare_time_us: 0,
187            device_prepare_wait_time_us: 0,
188            command_finish_time_us: 0,
189            target_prepare_time_us: 0,
190            text_engine_init_time_us: 0,
191            pipeline_create_time_us: 0,
192            pipeline_create_count: 0,
193            pass_count,
194            draw_count,
195            uploaded_vertex_bytes,
196            text_glyph_instance_count: 0,
197            text_vertex_bytes: 0,
198            visible_layer_count: 0,
199            direct_packet_count: 0,
200            prepared_fragment_cache_hits: 0,
201            prepared_fragment_build_count: 0,
202            retained_state_update_time_us: 0,
203            composition_time_us: 0,
204            retained_scene_traversal_time_us: 0,
205            snapshot_commands_replayed: 0,
206            snapshot_commands_reused: 0,
207            retained_packet_build_time_us: 0,
208            retained_packet_build_count: 0,
209            retained_packet_rebuilds: RetainedPacketRebuildStats::default(),
210            retained_packet_normalize_time_us: 0,
211            retained_packet_signature_time_us: 0,
212            retained_packet_raster_state_init_time_us: 0,
213            retained_packet_scene_build_time_us: 0,
214            retained_packet_command_count: 0,
215            retained_packet_text_command_count: 0,
216            retained_packet_path_command_count: 0,
217            retained_packet_clip_path_command_count: 0,
218            retained_packet_image_command_count: 0,
219            retained_packet_rect_command_count: 0,
220            retained_packet_text_command_time_us: 0,
221            retained_packet_path_command_time_us: 0,
222            retained_packet_clip_path_command_time_us: 0,
223            retained_packet_image_command_time_us: 0,
224            retained_packet_rect_command_time_us: 0,
225            text_atlas_miss_count: 0,
226            text_atlas_miss_time_us: 0,
227            surface_acquire_time_us: 0,
228            resource_collection_time_us: 0,
229            bind_group_prepare_time_us: 0,
230            image_bind_group_time_us: 0,
231            analytic_path_bind_group_time_us: 0,
232            analytic_path_bind_group_miss_count: 0,
233            analytic_path_bind_group_upload_bytes: 0,
234            text_atlas_bind_group_time_us: 0,
235            text_atlas_upload_copy_time_us: 0,
236            text_atlas_upload_write_time_us: 0,
237            text_atlas_upload_bytes: 0,
238            batch_prepare_time_us: 0,
239            gpu_upload_time_us: 0,
240            pass_encode_time_us: 0,
241            queue_submit_time_us: 0,
242            surface_present_time_us: 0,
243            retained_packet_hotspot: None,
244        }
245    }
246
247    pub(crate) fn with_compositor_stats(mut self, stats: RetainedCompositorFrameStats) -> Self {
248        self.snapshot_commands_replayed = stats.snapshot_commands_replayed;
249        self.snapshot_commands_reused = stats.snapshot_commands_reused;
250        self.visible_layer_count = stats.visible_layers;
251        self.direct_packet_count = stats.direct_packets;
252        self.retained_state_update_time_us = (stats.state_update_time_ms * 1000.0).round() as u64;
253        self.composition_time_us = (stats.composition_time_ms * 1000.0).round() as u64;
254        self.retained_scene_traversal_time_us =
255            (stats.scene_traversal_time_ms * 1000.0).round() as u64;
256        self.retained_packet_build_time_us = (stats.packet_build_time_ms * 1000.0).round() as u64;
257        self.retained_packet_build_count = stats.packet_build_count;
258        self.retained_packet_rebuilds = stats.packet_rebuilds;
259        self.retained_packet_normalize_time_us =
260            (stats.packet_normalize_time_ms * 1000.0).round() as u64;
261        self.retained_packet_signature_time_us =
262            (stats.packet_signature_time_ms * 1000.0).round() as u64;
263        self.retained_packet_raster_state_init_time_us =
264            (stats.packet_raster_state_init_time_ms * 1000.0).round() as u64;
265        self.retained_packet_scene_build_time_us =
266            (stats.packet_scene_build_time_ms * 1000.0).round() as u64;
267        self.retained_packet_command_count = stats.packet_command_count;
268        self.retained_packet_text_command_count = stats.packet_text_command_count;
269        self.retained_packet_path_command_count = stats.packet_path_command_count;
270        self.retained_packet_clip_path_command_count = stats.packet_clip_path_command_count;
271        self.retained_packet_image_command_count = stats.packet_image_command_count;
272        self.retained_packet_rect_command_count = stats.packet_rect_command_count;
273        self.retained_packet_text_command_time_us =
274            (stats.packet_text_command_time_ms * 1000.0).round() as u64;
275        self.retained_packet_path_command_time_us =
276            (stats.packet_path_command_time_ms * 1000.0).round() as u64;
277        self.retained_packet_clip_path_command_time_us =
278            (stats.packet_clip_path_command_time_ms * 1000.0).round() as u64;
279        self.retained_packet_image_command_time_us =
280            (stats.packet_image_command_time_ms * 1000.0).round() as u64;
281        self.retained_packet_rect_command_time_us =
282            (stats.packet_rect_command_time_ms * 1000.0).round() as u64;
283        self.retained_packet_hotspot =
284            stats
285                .slowest_packet_build
286                .map(|hotspot| RendererPacketHotspot {
287                    container_layer_id: hotspot.container_layer_id,
288                    owner_widget_id: hotspot.owner_widget_id,
289                    segment_index: hotspot.segment_index,
290                    total_time_us: (hotspot.total_time_ms * 1000.0).round() as u64,
291                    scene_build_time_us: (hotspot.scene_build_time_ms * 1000.0).round() as u64,
292                    command_count: hotspot.command_count,
293                    text_command_count: hotspot.text_command_count,
294                    path_command_count: hotspot.path_command_count,
295                    rect_command_count: hotspot.rect_command_count,
296                    text_command_time_us: (hotspot.text_command_time_ms * 1000.0).round() as u64,
297                    path_command_time_us: (hotspot.path_command_time_ms * 1000.0).round() as u64,
298                    rect_command_time_us: (hotspot.rect_command_time_ms * 1000.0).round() as u64,
299                    text_sample: hotspot.text_sample,
300                });
301        self
302    }
303
304    pub(crate) fn with_text_stats(mut self, stats: TextFrameStats) -> Self {
305        self.text_glyph_instance_count = stats.glyph_instances;
306        self.text_vertex_bytes = stats.glyph_upload_bytes;
307        self.text_atlas_miss_count = stats.atlas_miss_count;
308        self.text_atlas_miss_time_us = stats.atlas_miss_time_us;
309        self
310    }
311}