Merge pull request #742 from ludovic111/perf/metal-gpu-timer

🤖 Agent: merged by Ludovic's AI agent: verify passed, parity proven (CI or perf-frame-2's local run), before/after numbers and screenshots in the PR body.
This commit is contained in:
Ludovic Marie
2026-10-17 21:42:40 +02:00
committed by GitHub
3 changed files with 82 additions and 23 deletions
@@ -684,6 +684,8 @@ impl Game<'_> {
"size": [r.config.width, r.config.height],
"present_mode": format!("{:?}", r.config.present_mode),
"drawn_sections": r.drawn_sections,
"section_draws": r.section_draws,
"section_draw_calls": r.section_draw_calls,
"uploaded_bytes": r.uploaded_bytes,
"resident_sections": r.resident_sections(),
"arena_bytes": arena_bytes,
+33 -9
View File
@@ -355,6 +355,10 @@ pub struct Renderer {
pub info: wgpu::AdapterInfo,
pub suspended: bool,
pub drawn_sections: usize,
/// Last frame's section draws (face ranges and translucent sections),
/// and the draw calls the backend issued for them.
pub section_draws: usize,
pub section_draw_calls: usize,
pub uploaded_bytes: u64,
started: Instant,
/// Write the next presented frame to this PNG path (control socket).
@@ -1767,6 +1771,8 @@ impl Renderer {
info,
suspended: size.width == 0 || size.height == 0,
drawn_sections: 0,
section_draws: 0,
section_draw_calls: 0,
uploaded_bytes: 0,
started: Instant::now(),
screenshot: None,
@@ -2383,18 +2389,22 @@ impl Renderer {
let culling = !frame.title && frame.cave_culling;
if culling {
let radius = (frame.distance / 16.0) as i32 + 2;
self.occlusion.update(camera_section, radius);
crate::spans::time("occlusion", || {
self.occlusion.update(camera_section, radius)
});
}
let reachable = &self.occlusion;
let mut visible = std::mem::take(&mut self.visible);
visible.clear();
self.sections.visible(
camera,
(frame.distance / 16.0) as i32,
&view,
|k| !culling || reachable.contains(k),
&mut visible,
);
crate::spans::time("visibility", || {
self.sections.visible(
camera,
(frame.distance / 16.0) as i32,
&view,
|k| !culling || reachable.contains(k),
&mut visible,
)
});
self.drawn_sections = visible.len();
if visible.len() as u64 > self.instance_capacity {
self.instance_capacity = (visible.len() as u64).next_power_of_two();
@@ -2482,6 +2492,19 @@ impl Renderer {
}
self.translucency_cursor += budget;
let translucent_draws = self.draws.len() as u32 - solid_draws;
self.section_draws = self.draws.len();
// Metal replays a multi-draw as one draw call per entry.
self.section_draw_calls = if self.multi_draw && self.info.backend != wgpu::Backend::Metal {
(0..self.draws.len())
.filter(|&i| {
i == 0
|| i == solid_draws as usize
|| self.draw_pages[i] != self.draw_pages[i - 1]
})
.count()
} else {
self.draws.len()
};
if self.multi_draw && !self.draws.is_empty() {
if self.draws.len() as u64 > self.indirect_capacity {
self.indirect_capacity = (self.draws.len() as u64).next_power_of_two();
@@ -3001,7 +3024,8 @@ impl Renderer {
.take()
.map(|path| (path, self.copy_frame(&mut encoder, &target_texture)));
self.uploads.finish(&encoder);
let submission = self.queue.submit([encoder.finish()]);
let commands = crate::spans::time("encode", || encoder.finish());
let submission = crate::spans::time("submit", || self.queue.submit([commands]));
if let Some(timer) = &mut self.gpu_timer {
timer.submitted();
timer.collect(&self.device, &mut self.gpu_times);
+47 -14
View File
@@ -1,5 +1,9 @@
//! GPU frame time from timestamp queries, read back a few frames late so the
//! CPU never waits for it. Frames whose readback slot is still busy go untimed.
//!
//! Where the GPU can only time pass boundaries (Apple GPUs on Metal), the
//! frame is bracketed by two one-thread compute passes that write the
//! timestamps (Metal leaves an empty pass's timestamps at zero).
use std::sync::{
Arc,
atomic::{AtomicU8, Ordering},
@@ -23,24 +27,18 @@ pub struct GpuTimer {
current: Option<usize>,
next: usize,
period_ns: f32,
/// Timestamps between commands, or else this pass's boundaries.
marker: Option<wgpu::ComputePipeline>,
}
impl GpuTimer {
/// The features timing needs; `None` when the adapter lacks them.
/// The features timing uses that the adapter has (none: no timing).
pub fn features(adapter: &wgpu::Adapter) -> wgpu::Features {
let wanted =
wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS;
if adapter.features().contains(wanted) {
wanted
} else {
wgpu::Features::empty()
}
adapter.features()
& (wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS)
}
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option<Self> {
if !device
.features()
.contains(wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS)
{
if !device.features().contains(wgpu::Features::TIMESTAMP_QUERY) {
return None;
}
let queries = device.create_query_set(&wgpu::QuerySetDescriptor {
@@ -72,21 +70,56 @@ impl GpuTimer {
current: None,
next: 0,
period_ns: queue.get_timestamp_period(),
marker: (!device
.features()
.contains(wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS))
.then(|| {
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Frame timestamp"),
source: wgpu::ShaderSource::Wgsl(
"@compute @workgroup_size(1) fn mark() {}".into(),
),
});
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("Frame timestamp"),
layout: None,
module: &module,
entry_point: Some("mark"),
compilation_options: Default::default(),
cache: None,
})
}),
})
}
pub fn begin(&mut self, encoder: &mut wgpu::CommandEncoder) {
let slot = self.next;
self.current = (self.slots[slot].state.load(Ordering::Acquire) == FREE).then_some(slot);
if self.current.is_some() {
encoder.write_timestamp(&self.queries, 0);
self.stamp(encoder, 0);
}
}
pub fn end(&mut self, encoder: &mut wgpu::CommandEncoder) {
let Some(slot) = self.current else { return };
encoder.write_timestamp(&self.queries, 1);
self.stamp(encoder, 1);
encoder.resolve_query_set(&self.queries, 0..2, &self.resolve, 0);
encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.slots[slot].buffer, 0, 16);
}
fn stamp(&self, encoder: &mut wgpu::CommandEncoder, index: u32) {
let Some(marker) = &self.marker else {
encoder.write_timestamp(&self.queries, index);
return;
};
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Frame timestamp"),
timestamp_writes: Some(wgpu::ComputePassTimestampWrites {
query_set: &self.queries,
beginning_of_pass_write_index: (index == 0).then_some(0),
end_of_pass_write_index: (index == 1).then_some(1),
}),
});
pass.set_pipeline(marker);
pass.dispatch_workgroups(1, 1, 1);
}
/// After the frame's submission: start reading its timestamps back.
pub fn submitted(&mut self) {
let Some(slot) = self.current.take() else {