Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions native/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ image-extras = [

[build-dependencies]
cmake = "0.1"
# build.rs generates the BRDF table with the renderer's own generator
half = "2"

[dependencies]
bloom-geometry-format = { path = "../../crates/bloom-geometry-format", optional = true }
Expand Down
14 changes: 14 additions & 0 deletions native/shared/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
//! the `bloom_jolt` C++ shim (and JoltPhysics, via its own CMakeLists) via the
//! `cmake` crate and emits link directives so rustc picks up both archives.

// The split-sum BRDF table is a constant: generate it here, once per build,
// instead of in every app's startup (see src/renderer/brdf_lut.rs).
#[path = "src/renderer/brdf_lut.rs"]
mod brdf_lut;

fn write_brdf_lut() {
println!("cargo:rerun-if-changed=src/renderer/brdf_lut.rs");
let texels = brdf_lut::build_brdf_lut(brdf_lut::BRDF_LUT_SIZE);
let bytes: Vec<u8> = texels.iter().flat_map(|t| t.to_le_bytes()).collect();
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("brdf_lut.bin");
std::fs::write(&out, bytes).expect("write brdf_lut.bin");
}

fn main() {
// Always re-run when these change.
println!("cargo:rerun-if-changed=build.rs");
write_brdf_lut();
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_JOLT");

// SH-055 — `fold_scene_inputs` cfg. GPUs that cap `maxBindGroups` at 4 —
Expand Down
127 changes: 108 additions & 19 deletions native/shared/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ pub struct Profiler {
frame_total_gpu_us: [f64; ROLLING_FRAMES],
histogram_idx: usize,
histogram_filled: usize,

/// This frame's CPU phase spans (start, end), for the frame total:
/// phases nest (`queue_submit` runs inside `render_total`), so the
/// total is the time covered by any phase, not the sum of them.
cpu_spans: Vec<(Instant, Instant)>,
/// This frame's GPU busy time: the union of every pass interval.
/// Passes overlap on tile-based GPUs (the next pass's vertex work
/// starts while the previous pass's fragments run), so a sum
/// counts the same GPU time several times.
gpu_union_us: Option<f64>,
}

/// Total length covered by a set of (start, end) intervals, overlaps
/// counted once.
fn union_length(mut spans: Vec<(u64, u64)>) -> u64 {
spans.retain(|(b, e)| e > b);
spans.sort_unstable();
let mut total = 0;
let mut current: Option<(u64, u64)> = None;
for (b, e) in spans {
match current {
Some((cb, ce)) if b <= ce => current = Some((cb, ce.max(e))),
Some((cb, ce)) => {
total += ce - cb;
current = Some((b, e));
}
None => current = Some((b, e)),
}
}
if let Some((cb, ce)) = current {
total += ce - cb;
}
total
}

struct RollingStats {
Expand Down Expand Up @@ -193,6 +226,8 @@ impl Profiler {
frame_total_gpu_us: [0.0; ROLLING_FRAMES],
histogram_idx: 0,
histogram_filled: 0,
cpu_spans: Vec::new(),
gpu_union_us: None,
}
}

Expand Down Expand Up @@ -264,7 +299,9 @@ impl Profiler {
return;
}
if let Some(s) = self.open_cpu.remove(label) {
let us = s.start.elapsed().as_secs_f64() * 1_000_000.0;
let now = Instant::now();
let us = now.duration_since(s.start).as_secs_f64() * 1_000_000.0;
self.cpu_spans.push((s.start, now));
self.frame.push(FrameSample {
label,
cpu_us: us,
Expand Down Expand Up @@ -358,6 +395,8 @@ impl Profiler {
if !self.enabled {
self.frame.clear();
self.open_cpu.clear();
self.cpu_spans.clear();
self.gpu_union_us = None;
self.next_query = 0;
self.pending_gpu.clear();
return;
Expand All @@ -375,7 +414,12 @@ impl Profiler {
let data = slice.get_mapped_range().to_vec();
readback.unmap();
let period = self.timestamp_period_ns as f64;
let mut by_label: HashMap<&'static str, f64> = HashMap::new();
// A label can cover several passes (the bloom mip chain), and
// on tile-based GPUs consecutive passes overlap: each label
// reports the time covered by its intervals, and the frame the
// time covered by all of them, not their sums.
let mut spans: HashMap<&'static str, Vec<(u64, u64)>> = HashMap::new();
let mut all = Vec::with_capacity(self.pending_gpu.len());
for (label, b, e) in &self.pending_gpu {
let bo = (*b as usize) * 8;
let eo = (*e as usize) * 8;
Expand All @@ -384,11 +428,20 @@ impl Profiler {
}
let bt = u64::from_le_bytes(data[bo..bo + 8].try_into().unwrap());
let et = u64::from_le_bytes(data[eo..eo + 8].try_into().unwrap());
// an unwritten end (an empty pass on some drivers) reads 0
if et <= bt {
continue;
}
let us = (et - bt) as f64 * period / 1000.0;
*by_label.entry(*label).or_insert(0.0) += us;
spans.entry(*label).or_default().push((bt, et));
all.push((bt, et));
}
let ticks_to_us = |t: u64| t as f64 * period / 1000.0;
let mut by_label: HashMap<&'static str, f64> = spans
.into_iter()
.map(|(label, v)| (label, ticks_to_us(union_length(v))))
.collect();
if !all.is_empty() {
self.gpu_union_us = Some(ticks_to_us(union_length(all)));
}
for s in self.frame.iter_mut() {
if let Some(us) = by_label.remove(s.label) {
Expand Down Expand Up @@ -417,6 +470,8 @@ impl Profiler {
// Phase 8 — sum the per-pass samples into a per-frame total
// for the histogram before draining. CPU sums every sample;
// GPU sums only those with timestamps (rest have None).
// Totals are the time covered by the frame's phases and passes
// (they nest and overlap); samples without spans (tests) add up.
let mut frame_cpu = 0.0;
let mut frame_gpu = 0.0;
for s in &self.frame {
Expand All @@ -425,6 +480,14 @@ impl Profiler {
frame_gpu += g;
}
}
if let Some(origin) = self.cpu_spans.iter().map(|(b, _)| *b).min() {
let ns = |t: Instant| t.duration_since(origin).as_nanos() as u64;
let spans = self.cpu_spans.drain(..).map(|(b, e)| (ns(b), ns(e))).collect();
frame_cpu = union_length(spans) as f64 / 1000.0;
}
if let Some(g) = self.gpu_union_us.take() {
frame_gpu = g;
}
self.frame_total_cpu_us[self.histogram_idx] = frame_cpu;
self.frame_total_gpu_us[self.histogram_idx] = frame_gpu;
self.histogram_idx = (self.histogram_idx + 1) % ROLLING_FRAMES;
Expand Down Expand Up @@ -509,25 +572,24 @@ impl Profiler {
out
}

/// Average total CPU frame time across the rolling window (sum of
/// all phases). Useful for a single headline number.
/// Average CPU frame time across the rolling window: the time
/// covered by the frame's profiled phases (nested phases once).
pub fn avg_frame_cpu_us(&self) -> f64 {
let fc = self.frame_count;
self.rolling
.values()
.filter(|s| fc.saturating_sub(s.last_frame) <= ROLLING_FRAMES as u64)
.map(|s| s.avg_cpu())
.sum()
self.avg_history(|(cpu, _)| cpu)
}

/// Average total GPU frame time where available.
/// Average GPU frame time where available: the time the frame's
/// timed passes kept the GPU busy (overlapping passes once).
pub fn avg_frame_gpu_us(&self) -> f64 {
let fc = self.frame_count;
self.rolling
.values()
.filter(|s| fc.saturating_sub(s.last_frame) <= ROLLING_FRAMES as u64)
.filter_map(|s| s.avg_gpu())
.sum()
self.avg_history(|(_, gpu)| gpu)
}

fn avg_history(&self, pick: impl Fn((f64, f64)) -> f64) -> f64 {
let h = self.frame_history();
if h.is_empty() {
return 0.0;
}
h.iter().map(|&f| pick(f)).sum::<f64>() / h.len() as f64
}

/// Snapshot the rolling averages in a stable, CPU-time-descending
Expand Down Expand Up @@ -662,6 +724,33 @@ mod tests {
p.frame_end_cpu();
}

#[test]
fn union_length_counts_overlaps_once() {
assert_eq!(union_length(vec![]), 0);
assert_eq!(union_length(vec![(10, 20)]), 10);
// nine overlapping bloom mips spanning 270..1080 (as measured on M1 Max)
let bloom = vec![(270, 941), (369, 972), (386, 988), (471, 1003), (473, 1019), (512, 1027), (532, 1035), (551, 1047), (553, 1077)];
assert_eq!(union_length(bloom), 1077 - 270);
// disjoint, nested and empty/inverted spans
assert_eq!(union_length(vec![(0, 10), (20, 30), (22, 25), (40, 40), (50, 45)]), 20);
}

#[test]
fn nested_cpu_phases_count_once_in_frame_total() {
let mut p = Profiler::new();
p.set_enabled(true);
p.begin("outer");
p.begin("inner");
std::thread::sleep(std::time::Duration::from_millis(5));
p.end("inner");
p.end("outer");
p.frame_end_cpu();
let (cpu, _) = p.frame_history()[0];
let outer = p.snapshot().iter().find(|(l, _, _)| *l == "outer").unwrap().1;
assert!((cpu - outer).abs() < 1.0, "frame total {cpu} us must equal the outer phase {outer} us, not outer + inner");
assert!((p.avg_frame_cpu_us() - cpu).abs() < 1e-9);
}

#[test]
fn frame_history_empty_before_any_frames() {
let mut p = Profiler::new();
Expand Down
30 changes: 26 additions & 4 deletions native/shared/src/renderer/brdf_lut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//!
//! Pre-integrates the GGX BRDF into a 2D `Rg16Float` table indexed by
//! `(NdotV, roughness)` that the PBR shader samples at runtime via
//! the Karis-2013 split-sum approximation. Entirely CPU-side —
//! builds once at startup via `build_brdf_lut(size)` and hands the
//! result to `queue.write_texture` in `Renderer::new`.
//! the Karis-2013 split-sum approximation. The table is a constant:
//! `build.rs` includes this file and runs `build_brdf_lut(BRDF_LUT_SIZE)`
//! once per build, and `Renderer::new` uploads the embedded bytes
//! (computing it at startup took ~0.4 s on an M1 Max, every launch).
//!
//! Private helpers (Hammersley, GGX importance sampling, Smith
//! geometry) stay module-scoped; only `build_brdf_lut` is `pub` so
Expand All @@ -24,6 +25,9 @@
// × Fresnel(VdotH) part. The Fresnel split into scale (F0) and bias
// (1) lets us factor F0 out of the integral.

/// Width and height of the table, in texels.
pub const BRDF_LUT_SIZE: usize = 256;

const BRDF_LUT_SAMPLES: u32 = 1024;

fn radical_inverse_vdc(mut bits: u32) -> f32 {
Expand Down Expand Up @@ -128,7 +132,8 @@ fn build_brdf_lut_row(y: usize, size: usize) -> Vec<u16> {
/// row is constant `roughness` (v axis), each column constant `NdotV`
/// (u axis). Output is row-major suitable for write_texture. Splits
/// across `available_parallelism()` threads since cells are
/// independent — keeps startup latency manageable even at 1024 spp.
/// independent. Runs in `build.rs`; the renderer only calls it in tests.
#[cfg_attr(not(test), allow(dead_code))]
pub fn build_brdf_lut(size: usize) -> Vec<u16> {
#[cfg(not(target_arch = "wasm32"))]
{
Expand Down Expand Up @@ -162,3 +167,20 @@ pub fn build_brdf_lut(size: usize) -> Vec<u16> {
.collect()
}
}

#[cfg(test)]
mod tests {
use super::*;

/// The table the renderer embeds is the generator's output, row-major.
#[test]
fn baked_table_matches_generator() {
let baked = super::super::BRDF_LUT_BAKED;
assert_eq!(baked.len(), BRDF_LUT_SIZE * BRDF_LUT_SIZE * 2 * 2);
for y in [0, BRDF_LUT_SIZE / 2, BRDF_LUT_SIZE - 1] {
let row: Vec<u8> = build_brdf_lut_row(y, BRDF_LUT_SIZE).iter().flat_map(|t| t.to_le_bytes()).collect();
let start = y * BRDF_LUT_SIZE * 4;
assert_eq!(&baked[start..start + row.len()], row.as_slice(), "row {y}");
}
}
}
5 changes: 3 additions & 2 deletions native/shared/src/renderer/froxel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! (256 = worst-case every light in one froxel — exact parity with the
//! reference loop, no truncation).

use super::lazy_pipeline::LazyComputePipeline;
use wgpu::util::DeviceExt;

pub(super) const GRID_X: u32 = 16;
Expand Down Expand Up @@ -243,7 +244,7 @@ pub(super) fn extra_lighting_layout_entries() -> [wgpu::BindGroupLayoutEntry; 3]
}

pub struct FroxelPass {
pub assign_pipeline: wgpu::ComputePipeline,
pub assign_pipeline: LazyComputePipeline,
pub assign_layout: wgpu::BindGroupLayout,
pub params_buffer: wgpu::Buffer,
/// Compact point-light UBO for the compute pass (count + 256 lights).
Expand Down Expand Up @@ -323,7 +324,7 @@ impl FroxelPass {
bind_group_layouts: &[Some(&assign_layout)],
..Default::default()
});
let assign_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
let assign_pipeline = LazyComputePipeline::new(device, &wgpu::ComputePipelineDescriptor {
label: Some("froxel_assign_pipeline"),
layout: Some(&pl),
module: &shader,
Expand Down
Loading