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