use super::*;
use indicatrix::{
geometry::cuts::StandardGemCuts,
optics::{materials::GemMaterial, raytracer::LightingPreset},
};
fn tiny_scene() -> SceneState {
SceneState {
width: 8,
height: 8,
yaw: 0.4,
pitch: 0.3,
distance: 3.0,
light_yaw: 0.85,
light_pitch: 0.95,
exposure: 1.0,
max_bounces: 4,
lighting_preset: LightingPreset::Daylight,
material: GemMaterial::diamond(),
planes: StandardGemCuts::standard_round_brilliant(),
girdle_frosted: false,
backdrop: 0.0,
environment: indicatrix_net::scene::SceneEnvironment::Studio,
surface_glare: 1.0,
tools: Vec::new(),
fluorescence: indicatrix::optics::fluorescence::Fluorescence::default(),
head_shadow_deg: 16.0,
}
}
fn never_cancel() -> AtomicBool {
AtomicBool::new(false)
}
#[test]
fn calibrate_declines_when_the_gpu_declines() {
let scene = tiny_scene();
let gpu = GpuBackend::disabled();
let mut out = vec![Vec3::ZERO; 64];
assert_eq!(
calibrate(&gpu, &scene, 0, 8, 1, &mut out, &never_cancel()),
CalibrationOutcome::GpuDeclined
);
assert!(out.iter().all(|v| *v == Vec3::ZERO));
}
#[test]
fn calibrate_declines_when_there_are_too_few_samples_to_probe_with() {
let scene = tiny_scene();
let gpu = GpuBackend::disabled();
let mut out = vec![Vec3::ZERO; 64];
assert_eq!(
calibrate(&gpu, &scene, 0, 2, 1, &mut out, &never_cancel()),
CalibrationOutcome::GpuDeclined
);
}
#[test]
fn calibrate_honors_a_cancel_already_set_before_the_first_probe_sample() {
let scene = tiny_scene();
let gpu = GpuBackend::disabled();
let mut out = vec![Vec3::ZERO; 64];
let cancel = AtomicBool::new(true);
assert_eq!(
calibrate(&gpu, &scene, 0, 8, 1, &mut out, &cancel),
CalibrationOutcome::Cancelled { consumed: 0 }
);
assert!(out.iter().all(|v| *v == Vec3::ZERO));
}
#[test]
fn hybrid_trace_falls_back_to_cpu_only_when_gpu_frac_is_none() {
let scene = tiny_scene();
let gpu = GpuBackend::disabled();
let mut gpu_frac = None;
let hybrid = hybrid_trace(&gpu, &scene, 0, 4, 1, &mut gpu_frac, &never_cancel())
.expect("gpu_share == 0 here, so this can never observe a cancellation");
let cpu_only = render_core_trace_samples(&scene, 0, 4, 1);
assert_eq!(hybrid, cpu_only);
assert_eq!(gpu_frac, None);
}
#[test]
fn hybrid_trace_equals_the_whole_range_on_cpu_with_the_gpu_share_disabled() {
let scene = tiny_scene();
let gpu = GpuBackend::disabled();
let mut gpu_frac = Some(0.8);
let hybrid = hybrid_trace(&gpu, &scene, 10, 6, 2, &mut gpu_frac, &never_cancel())
.expect("gpu_share == 0 here, so this can never observe a cancellation");
let direct = render_core_trace_samples(&scene, 10, 6, 2);
assert_eq!(hybrid, direct);
}
fn render_core_trace_samples(
scene: &SceneState,
first: u32,
samples: u32,
threads: usize,
) -> Vec<Vec3> {
super::super::trace_samples(scene, first, samples, threads)
}
fn adaptive_next_batch_size(prev: u32, elapsed: std::time::Duration) -> u32 {
const TARGET: std::time::Duration = std::time::Duration::from_millis(100);
const MAX_SUBBATCH: u32 = 2048;
if elapsed.is_zero() {
return prev.saturating_mul(4).clamp(1, MAX_SUBBATCH);
}
let ratio = TARGET.as_secs_f64() / elapsed.as_secs_f64();
let scaled = f64::from(prev) * ratio;
let max_growth = f64::from(prev.saturating_mul(4).clamp(1, MAX_SUBBATCH));
scaled.clamp(1.0, max_growth).round() as u32
}
fn time_adaptive_loop(total: u32, mut trace: impl FnMut(u32, u32)) -> std::time::Duration {
let overall = Instant::now();
let mut produced = 0u32;
let mut batch_size = 1u32;
while produced < total {
let this_batch = batch_size.min(total - produced);
let start = Instant::now();
trace(produced, this_batch);
let elapsed = start.elapsed();
produced += this_batch;
batch_size = adaptive_next_batch_size(batch_size, elapsed);
}
overall.elapsed()
}
fn warm_up_gpu(gpu: &GpuBackend, scene: &SceneState) {
let mut warm = vec![Vec3::ZERO; scene.width as usize * scene.height as usize];
let _ = gpu.try_accumulate(
&GpuSceneRef {
camera: &Camera::new(scene.yaw, scene.pitch, scene.distance, DEFAULT_FOV_DEG),
width: scene.width,
height: scene.height,
planes: &scene.planes,
facet_finishes: &[],
material: &scene.material,
max_bounces: scene.max_bounces,
environment: scene
.lighting_preset
.studio(scene.exposure, scene.light_yaw, scene.light_pitch)
.with_backdrop(scene.backdrop),
},
0,
1,
&mut warm,
);
}
#[test]
#[ignore = "manual measurement: prints GPU-only vs. hybrid throughput on this machine's real adapter"]
fn measure_hybrid_speedup_against_gpu_only() {
let gpu = GpuBackend::acquire();
assert!(
gpu.adapter_label().is_some(),
"this measurement requires a real adapter -- run probe_gpu_adapter first"
);
let scene = SceneState {
width: 800,
height: 600,
max_bounces: 6,
..tiny_scene()
};
let total_samples: u32 = 2000;
let threads = 0;
warm_up_gpu(&gpu, &scene);
let gpu_only_elapsed = time_adaptive_loop(total_samples, |first, n| {
let _ = super::super::trace_samples_with_gpu(&gpu, &scene, first, n, threads);
});
let hybrid_start = Instant::now();
{
let mut calib_buf = vec![Vec3::ZERO; scene.width as usize * scene.height as usize];
let (frac, consumed) = match calibrate(
&gpu,
&scene,
0,
total_samples,
threads,
&mut calib_buf,
&never_cancel(),
) {
CalibrationOutcome::Split(frac, consumed) => (frac, consumed),
other => panic!("gpu available, expected a Split outcome, got {other:?}"),
};
let mut gpu_frac = Some(frac);
let mut produced = consumed;
let mut batch_size = 1u32;
while produced < total_samples {
let this_batch = batch_size.min(total_samples - produced);
let start = Instant::now();
let _ = hybrid_trace(
&gpu,
&scene,
produced,
this_batch,
threads,
&mut gpu_frac,
&never_cancel(),
);
let elapsed = start.elapsed();
produced += this_batch;
batch_size = adaptive_next_batch_size(batch_size, elapsed);
}
eprintln!("final gpu_frac={gpu_frac:?}");
}
let hybrid_elapsed = hybrid_start.elapsed();
let cpu_only_samples: u32 = 200; let cpu_only_elapsed = time_adaptive_loop(cpu_only_samples, |first, n| {
let _ = super::super::trace_samples(&scene, first, n, threads);
});
let cpu_only_rate = f64::from(cpu_only_samples) / cpu_only_elapsed.as_secs_f64();
let gpu_only_rate = f64::from(total_samples) / gpu_only_elapsed.as_secs_f64();
let hybrid_rate = f64::from(total_samples) / hybrid_elapsed.as_secs_f64();
eprintln!(
"GPU-only: {total_samples} samples in {gpu_only_elapsed:?} ({gpu_only_rate:.1} samples/s)"
);
eprintln!(
"Hybrid: {total_samples} samples in {hybrid_elapsed:?} ({hybrid_rate:.1} samples/s)"
);
eprintln!(
"CPU-only: {cpu_only_samples} samples in {cpu_only_elapsed:?} ({cpu_only_rate:.1} samples/s)"
);
eprintln!(
"Speedup (hybrid vs GPU-only): {:.2}x",
hybrid_rate / gpu_only_rate
);
eprintln!(
"GPU is {:.1}x faster than CPU per-sample on this scene",
gpu_only_rate / cpu_only_rate
);
}
#[test]
fn cpu_threads_beside_gpu_never_returns_zero() {
assert_eq!(cpu_threads_beside_gpu(1), 1, "one core still traces");
assert_eq!(cpu_threads_beside_gpu(2), 1, "two cores: one reserved");
assert!(
cpu_threads_beside_gpu(0) >= 1,
"0 means `all available`, which must still resolve to at least one thread"
);
}
#[test]
fn cpu_threads_beside_gpu_reserves_exactly_one_core() {
for n in 3..=16usize {
assert_eq!(
cpu_threads_beside_gpu(n),
n - 1,
"with {n} cores the tracer should get {} of them",
n - 1
);
}
}
#[test]
fn calibrate_cached_never_caches_a_gpu_decline() {
let gpu = GpuBackend::disabled();
let scene = tiny_scene();
let key = JobKey {
compute_mode: compute_mode_tag(ComputeMode::Hybrid),
gpu_id: 0xDEAD_0001,
threads: 1,
resolution_bucket: 6,
};
let mut out = vec![Vec3::ZERO; 64];
for _ in 0..2 {
let outcome = calibrate_cached(
key,
&gpu,
&scene,
SampleRange::new(0, 8),
1,
&mut out,
&never_cancel(),
);
assert_eq!(outcome, CachedCalibration::GpuDeclined);
}
assert!(
!CALIBRATION_CACHE.lock().unwrap().contains_key(&key),
"a decline must never be cached"
);
}
#[test]
fn calibrate_cached_hits_without_reprobing() {
let gpu = GpuBackend::disabled();
let scene = tiny_scene();
let key = JobKey {
compute_mode: compute_mode_tag(ComputeMode::Hybrid),
gpu_id: 0xDEAD_0002,
threads: 2,
resolution_bucket: 8,
};
CALIBRATION_CACHE
.lock()
.unwrap()
.insert(key, CachedDecision::Split { gpu_frac: 0.42 });
let mut out = vec![Vec3::ZERO; 64];
let outcome = calibrate_cached(
key,
&gpu,
&scene,
SampleRange::new(0, 8),
2,
&mut out,
&never_cancel(),
);
assert_eq!(
outcome,
CachedCalibration::Split {
gpu_frac: 0.42,
consumed: 0
}
);
assert!(
out.iter().all(|v| *v == Vec3::ZERO),
"a cache hit dispatches nothing, so `out` must stay untouched"
);
CALIBRATION_CACHE.lock().unwrap().remove(&key);
}
#[test]
fn calibrate_cached_hits_gpu_only_the_same_way() {
let gpu = GpuBackend::disabled();
let scene = tiny_scene();
let key = JobKey {
compute_mode: compute_mode_tag(ComputeMode::Hybrid),
gpu_id: 0xDEAD_0003,
threads: 4,
resolution_bucket: 10,
};
CALIBRATION_CACHE
.lock()
.unwrap()
.insert(key, CachedDecision::GpuOnly);
let mut out = vec![Vec3::ZERO; 64];
let outcome = calibrate_cached(
key,
&gpu,
&scene,
SampleRange::new(0, 8),
4,
&mut out,
&never_cancel(),
);
assert_eq!(outcome, CachedCalibration::GpuOnly { consumed: 0 });
CALIBRATION_CACHE.lock().unwrap().remove(&key);
}
#[test]
fn finalize_split_cache_overwrites_on_some_and_evicts_on_none() {
let key = JobKey {
compute_mode: compute_mode_tag(ComputeMode::Hybrid),
gpu_id: 0xDEAD_0004,
threads: 1,
resolution_bucket: 12,
};
CALIBRATION_CACHE
.lock()
.unwrap()
.insert(key, CachedDecision::Split { gpu_frac: 0.1 });
finalize_split_cache(key, Some(0.77));
assert_eq!(
CALIBRATION_CACHE.lock().unwrap().get(&key).copied(),
Some(CachedDecision::Split { gpu_frac: 0.77 }),
"a live split at job end must overwrite the cache with the job's final blend"
);
finalize_split_cache(key, None);
assert_eq!(
CALIBRATION_CACHE.lock().unwrap().get(&key).copied(),
None,
"a mid-job GPU decline must evict the cached split, not leave a stale one"
);
}
#[test]
fn job_key_collapses_a_zero_threads_request_onto_its_realized_count() {
let gpu = GpuBackend::disabled();
let scene = tiny_scene();
let realized = super::super::effective_thread_count(0);
assert_eq!(
job_key(&gpu, &scene, 0, ComputeMode::Hybrid),
job_key(&gpu, &scene, realized, ComputeMode::Hybrid),
"`--threads 0` and its own realized count must share one cache entry"
);
}
#[test]
fn job_key_distinguishes_compute_modes_and_resolutions() {
let gpu = GpuBackend::disabled();
let scene = tiny_scene();
let hybrid_key = job_key(&gpu, &scene, 4, ComputeMode::Hybrid);
let only_gpu_key = job_key(&gpu, &scene, 4, ComputeMode::OnlyGpu);
assert_ne!(hybrid_key, only_gpu_key);
let bigger_scene = SceneState {
width: 4000,
height: 3000,
..tiny_scene()
};
let bigger_key = job_key(&gpu, &bigger_scene, 4, ComputeMode::Hybrid);
assert_ne!(
hybrid_key, bigger_key,
"a far larger resolution must land in a different bucket"
);
}
#[test]
fn calibrate_now_is_callable_and_never_caches_a_decline() {
let gpu = GpuBackend::disabled();
let scene = tiny_scene();
calibrate_now(&gpu, &scene, 1, ComputeMode::Hybrid);
let key = job_key(&gpu, &scene, 1, ComputeMode::Hybrid);
assert!(!CALIBRATION_CACHE.lock().unwrap().contains_key(&key));
}