use super::clipping::{clip_to_variance, lerp, luminance, RowWindowStats};
use super::config::{TaaConfig, TaaError};
use super::history::TaaHistory;
use super::jitter::jitter_offset;
pub fn sharpen_image(image: &[f32], width: usize, height: usize, strength: f32) -> Vec<f32> {
if strength == 0.0 || width == 0 || height == 0 {
return image.to_vec();
}
let mut blurred = vec![0.0_f32; image.len()];
for py in 0..height {
for px in 0..width {
let y_min = py.saturating_sub(1);
let y_max = (py + 2).min(height);
let x_min = px.saturating_sub(1);
let x_max = (px + 2).min(width);
let mut sum = [0.0_f32; 3];
let mut count = 0_usize;
for ny in y_min..y_max {
for nx in x_min..x_max {
let base = (ny * width + nx) * 3;
for (c, sum_val) in sum.iter_mut().enumerate() {
*sum_val += image.get(base + c).copied().unwrap_or(0.0);
}
count += 1;
}
}
let dst_base = (py * width + px) * 3;
let n = count as f32;
if count > 0 {
for (c, &sum_val) in sum.iter().enumerate() {
if let Some(slot) = blurred.get_mut(dst_base + c) {
*slot = sum_val / n;
}
}
}
}
}
let out = image
.iter()
.zip(blurred.iter())
.map(|(&orig, &blur)| (orig + strength * (orig - blur)).clamp(0.0, 1.0))
.collect();
out
}
pub fn accumulate_taa(
current: &[f32],
history: &mut TaaHistory,
config: &TaaConfig,
) -> Result<Vec<f32>, TaaError> {
let expected = history.width * history.height * 3;
if current.len() != expected {
return Err(TaaError::DimensionMismatch {
expected,
got: current.len(),
});
}
if history.is_empty() {
history.color = current.to_vec();
history.frame_count = 1;
history.last_mean_blend_factor = if expected == 0 { None } else { Some(1.0) };
return Ok(current.to_vec());
}
let width = history.width;
let height = history.height;
let mut blended = vec![0.0_f32; expected];
let mut clip_stats = if config.variance_clipping {
Some(RowWindowStats::new(
width,
height,
config.clip_window_radius,
))
} else {
None
};
let mut alpha_sum = 0.0_f64;
for py in 0..height {
if let Some(stats) = clip_stats.as_mut() {
stats.advance_to_row(current, py);
}
for px in 0..width {
let base = (py * width + px) * 3;
let cur_r = current.get(base).copied().unwrap_or(0.0);
let cur_g = current.get(base + 1).copied().unwrap_or(0.0);
let cur_b = current.get(base + 2).copied().unwrap_or(0.0);
let cur_color = [cur_r, cur_g, cur_b];
let mut hist_color = history.get_pixel(px, py);
if let Some(stats) = clip_stats.as_ref() {
let (mean, std) = stats.pixel(px);
hist_color = clip_to_variance(hist_color, mean, std, 1.0);
}
let alpha = if config.adaptive_blend {
let diff = (luminance(cur_color) - luminance(hist_color)).abs();
lerp(
config.adaptive_blend_min,
config.blend_factor,
diff.clamp(0.0, 1.0),
)
} else {
config.blend_factor
};
alpha_sum += alpha as f64;
let out_r = alpha * cur_color[0] + (1.0 - alpha) * hist_color[0];
let out_g = alpha * cur_color[1] + (1.0 - alpha) * hist_color[1];
let out_b = alpha * cur_color[2] + (1.0 - alpha) * hist_color[2];
if let Some(slot) = blended.get_mut(base) {
*slot = out_r;
}
if let Some(slot) = blended.get_mut(base + 1) {
*slot = out_g;
}
if let Some(slot) = blended.get_mut(base + 2) {
*slot = out_b;
}
}
}
let result = if config.sharpen_strength > 0.0 {
sharpen_image(&blended, width, height, config.sharpen_strength)
} else {
blended
};
history.color = result.clone();
history.frame_count += 1;
let pixel_count = width * height;
history.last_mean_blend_factor = if pixel_count == 0 {
None
} else {
Some((alpha_sum / pixel_count as f64) as f32)
};
Ok(result)
}
pub struct TaaAccumulator {
pub config: TaaConfig,
history: TaaHistory,
frame_idx: usize,
}
impl TaaAccumulator {
pub fn new(width: usize, height: usize, config: TaaConfig) -> Result<Self, TaaError> {
config.validate()?;
Ok(Self {
history: TaaHistory::new(width, height),
config,
frame_idx: 0,
})
}
pub fn process(&mut self, frame: &[f32]) -> Result<Vec<f32>, TaaError> {
let result = accumulate_taa(frame, &mut self.history, &self.config)?;
self.frame_idx += 1;
Ok(result)
}
pub fn current_jitter(&self) -> (f32, f32) {
jitter_offset(self.frame_idx, self.config.jitter_sequence_length)
}
pub fn frame_count(&self) -> usize {
self.history.frame_count
}
pub fn reset(&mut self) {
self.history.reset();
self.frame_idx = 0;
}
pub fn quality(&self) -> f32 {
let effective_samples = 1.0 / self.config.blend_factor;
(self.history.frame_count as f32 / effective_samples).min(1.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::temporal_aa::compute_taa_stats;
#[test]
fn test_accumulate_taa_empty_history_returns_current() {
let cfg = TaaConfig {
sharpen_strength: 0.0,
variance_clipping: false,
..TaaConfig::default()
};
let current = vec![0.8_f32; 4 * 4 * 3];
let mut history = TaaHistory::new(4, 4);
let out =
accumulate_taa(¤t, &mut history, &cfg).expect("accumulate_taa must succeed");
assert_eq!(out.len(), current.len());
for (&a, &b) in out.iter().zip(current.iter()) {
assert!((a - b).abs() < 1e-6);
}
assert_eq!(history.frame_count, 1);
}
#[test]
fn test_accumulate_taa_second_frame_blends() {
let cfg = TaaConfig {
blend_factor: 0.5,
variance_clipping: false,
sharpen_strength: 0.0,
adaptive_blend: false,
..TaaConfig::default()
};
let mut history = TaaHistory::new(2, 2);
let frame0 = vec![0.0_f32; 2 * 2 * 3];
accumulate_taa(&frame0, &mut history, &cfg).expect("accumulate_taa must succeed");
let frame1 = vec![1.0_f32; 2 * 2 * 3];
let out = accumulate_taa(&frame1, &mut history, &cfg).expect("accumulate_taa must succeed");
for &v in &out {
assert!((v - 0.5).abs() < 1e-5, "expected 0.5, got {v}");
}
assert!(
!out.iter().all(|&v| (v - 1.0).abs() < 1e-6),
"must not be pure current"
);
assert!(
!out.iter().all(|&v| v.abs() < 1e-6),
"must not be pure history"
);
}
#[test]
fn test_accumulate_taa_updates_frame_count() {
let cfg = TaaConfig {
sharpen_strength: 0.0,
variance_clipping: false,
..TaaConfig::default()
};
let mut history = TaaHistory::new(2, 2);
let frame = vec![0.5_f32; 2 * 2 * 3];
accumulate_taa(&frame, &mut history, &cfg).expect("accumulate_taa must succeed");
accumulate_taa(&frame, &mut history, &cfg).expect("accumulate_taa must succeed");
assert_eq!(history.frame_count, 2);
}
#[test]
fn test_accumulate_taa_blend_factor_one_equals_current() {
let cfg = TaaConfig {
blend_factor: 1.0,
variance_clipping: false,
sharpen_strength: 0.0,
adaptive_blend: false,
adaptive_blend_min: 0.05_f32.min(1.0),
..TaaConfig::default()
};
let mut history = TaaHistory::new(2, 2);
let frame0 = vec![0.0_f32; 2 * 2 * 3];
accumulate_taa(&frame0, &mut history, &cfg).expect("accumulate_taa must succeed");
let frame1 = vec![0.7_f32; 2 * 2 * 3];
let out = accumulate_taa(&frame1, &mut history, &cfg).expect("accumulate_taa must succeed");
for &v in &out {
assert!(
(v - 0.7).abs() < 1e-5,
"blend_factor=1 must return current: {v}"
);
}
}
#[test]
fn test_accumulate_taa_variance_clipping_difference() {
let no_clip_cfg = TaaConfig {
blend_factor: 0.1,
variance_clipping: false,
sharpen_strength: 0.0,
adaptive_blend: false,
..TaaConfig::default()
};
let clip_cfg = TaaConfig {
variance_clipping: true,
..no_clip_cfg.clone()
};
let mut h_no_clip = TaaHistory::new(4, 4);
let mut h_clip = TaaHistory::new(4, 4);
let black = vec![0.0_f32; 4 * 4 * 3];
accumulate_taa(&black, &mut h_no_clip, &no_clip_cfg).expect("accumulate_taa must succeed");
accumulate_taa(&black, &mut h_clip, &clip_cfg).expect("accumulate_taa must succeed");
let white = vec![1.0_f32; 4 * 4 * 3];
let out_no_clip = accumulate_taa(&white, &mut h_no_clip, &no_clip_cfg)
.expect("accumulate_taa must succeed");
let out_clip =
accumulate_taa(&white, &mut h_clip, &clip_cfg).expect("accumulate_taa must succeed");
let diff_sum: f32 = out_no_clip
.iter()
.zip(out_clip.iter())
.map(|(a, b)| (a - b).abs())
.sum();
assert!(
diff_sum > 1e-5,
"variance_clipping must produce different results"
);
}
#[test]
fn test_accumulate_taa_dimension_mismatch() {
let cfg = TaaConfig::default();
let mut history = TaaHistory::new(4, 4);
let wrong_frame = vec![0.5_f32; 3 * 3 * 3]; let result = accumulate_taa(&wrong_frame, &mut history, &cfg);
assert!(matches!(result, Err(TaaError::DimensionMismatch { .. })));
}
#[test]
fn test_sharpen_strength_zero_returns_copy() {
let image = vec![
0.3_f32, 0.6_f32, 0.9_f32, 0.1_f32, 0.2_f32, 0.3_f32, 0.4_f32, 0.5_f32, 0.6_f32,
0.7_f32, 0.8_f32, 0.9_f32,
];
let out = sharpen_image(&image, 2, 2, 0.0);
assert_eq!(out.len(), image.len());
for (&a, &b) in out.iter().zip(image.iter()) {
assert!(
(a - b).abs() < 1e-6,
"strength=0 must return identical copy"
);
}
}
#[test]
fn test_sharpen_uniform_image_unchanged() {
let image = vec![0.5_f32; 4 * 4 * 3];
let out = sharpen_image(&image, 4, 4, 0.5);
for &v in &out {
assert!(
(v - 0.5).abs() < 1e-4,
"uniform image must be unchanged: {v}"
);
}
}
#[test]
fn test_sharpen_output_clamped_to_unit() {
let image: Vec<f32> = (0..16)
.map(|i| if i % 2 == 0 { 0.0 } else { 1.0 })
.collect();
let out = sharpen_image(&image, 4, 4 / 3, 2.0);
for &v in &out {
assert!((0.0..=1.0).contains(&v), "output must be in [0,1]: {v}");
}
}
#[test]
fn test_accumulator_new_valid() {
let cfg = TaaConfig::default();
let acc = TaaAccumulator::new(8, 8, cfg);
assert!(acc.is_ok());
}
#[test]
fn test_accumulator_new_invalid_config() {
let cfg = TaaConfig {
blend_factor: 0.0,
..Default::default()
}; let acc = TaaAccumulator::new(4, 4, cfg);
assert!(acc.is_err());
}
#[test]
fn test_accumulator_process_first_frame() {
let cfg = TaaConfig {
sharpen_strength: 0.0,
variance_clipping: false,
..TaaConfig::default()
};
let mut acc = TaaAccumulator::new(2, 2, cfg).expect("TaaAccumulator::new must succeed");
let frame = vec![0.5_f32; 2 * 2 * 3];
let out = acc.process(&frame).expect("process must succeed");
assert_eq!(out.len(), frame.len());
assert_eq!(acc.frame_count(), 1);
}
#[test]
fn test_accumulator_process_accumulates() {
let cfg = TaaConfig {
blend_factor: 0.5,
variance_clipping: false,
sharpen_strength: 0.0,
adaptive_blend: false,
..TaaConfig::default()
};
let mut acc = TaaAccumulator::new(2, 2, cfg).expect("TaaAccumulator::new must succeed");
let black = vec![0.0_f32; 2 * 2 * 3];
let white = vec![1.0_f32; 2 * 2 * 3];
acc.process(&black).expect("process must succeed");
let out = acc.process(&white).expect("process must succeed");
for &v in &out {
assert!((v - 0.5).abs() < 1e-5, "expected 0.5, got {v}");
}
}
#[test]
fn test_accumulator_current_jitter_changes() {
let cfg = TaaConfig::default();
let mut acc = TaaAccumulator::new(2, 2, cfg).expect("TaaAccumulator::new must succeed");
let j0 = acc.current_jitter();
let frame = vec![0.5_f32; 2 * 2 * 3];
acc.process(&frame).expect("process must succeed");
let j1 = acc.current_jitter();
assert!(
(j0.0 - j1.0).abs() > 1e-6 || (j0.1 - j1.1).abs() > 1e-6,
"jitter must change between frames"
);
}
#[test]
fn test_accumulator_quality_grows() {
let cfg = TaaConfig {
blend_factor: 0.5,
variance_clipping: false,
sharpen_strength: 0.0,
..TaaConfig::default()
};
let mut acc = TaaAccumulator::new(2, 2, cfg).expect("TaaAccumulator::new must succeed");
let q0 = acc.quality();
let frame = vec![0.5_f32; 2 * 2 * 3];
acc.process(&frame).expect("process must succeed");
let q1 = acc.quality();
acc.process(&frame).expect("process must succeed");
let q2 = acc.quality();
assert!(q1 > q0, "quality must grow after first frame");
assert!(q2 > q1, "quality must grow after second frame");
assert!(q2 <= 1.0, "quality must not exceed 1.0");
}
#[test]
fn test_accumulator_quality_caps_at_one() {
let cfg = TaaConfig {
blend_factor: 0.5, variance_clipping: false,
sharpen_strength: 0.0,
..TaaConfig::default()
};
let mut acc = TaaAccumulator::new(2, 2, cfg).expect("TaaAccumulator::new must succeed");
let frame = vec![0.5_f32; 2 * 2 * 3];
for _ in 0..20 {
acc.process(&frame).expect("process must succeed");
}
assert!(
(acc.quality() - 1.0).abs() < 1e-5,
"quality must cap at 1.0"
);
}
#[test]
fn test_accumulator_reset() {
let cfg = TaaConfig {
sharpen_strength: 0.0,
variance_clipping: false,
..TaaConfig::default()
};
let mut acc = TaaAccumulator::new(2, 2, cfg).expect("TaaAccumulator::new must succeed");
let frame = vec![0.5_f32; 2 * 2 * 3];
acc.process(&frame).expect("process must succeed");
acc.process(&frame).expect("process must succeed");
assert_eq!(acc.frame_count(), 2);
acc.reset();
assert_eq!(acc.frame_count(), 0);
assert!(
(acc.quality() - 0.0).abs() < 1e-6,
"quality after reset must be 0"
);
}
#[test]
fn test_accumulate_taa_records_measured_blend_factor() {
let (w, h) = (6_usize, 6_usize);
let frame0 = vec![0.0_f32; w * h * 3];
let mut frame1 = vec![0.0_f32; w * h * 3];
for px in 0..(w * h) {
if px.is_multiple_of(2) {
for c in 0..3 {
frame1[px * 3 + c] = 1.0;
}
}
}
let config = TaaConfig {
variance_clipping: false,
sharpen_strength: 0.0,
adaptive_blend: true,
blend_factor: 0.8,
adaptive_blend_min: 0.05,
..TaaConfig::default()
};
config.validate().expect("config must be valid");
let mut history = TaaHistory::new(w, h);
assert_eq!(
history.last_mean_blend_factor, None,
"fresh history must carry no measurement"
);
accumulate_taa(&frame0, &mut history, &config).expect("bootstrap");
assert_eq!(
history.last_mean_blend_factor,
Some(1.0),
"the bootstrap frame is returned unblended"
);
let accumulated = accumulate_taa(&frame1, &mut history, &config).expect("blend");
let measured = history
.last_mean_blend_factor
.expect("accumulate_taa must record a measurement");
assert!(
measured > config.adaptive_blend_min + 1e-3 && measured < config.blend_factor - 1e-3,
"measured mean α {measured} must lie strictly between {} and {}",
config.adaptive_blend_min,
config.blend_factor
);
let stats =
compute_taa_stats(&frame1, &accumulated, &history, config.blend_factor).expect("stats");
assert!(
(stats.mean_blend_factor - measured).abs() < 1e-6,
"stats must report the measured α, got {}",
stats.mean_blend_factor
);
}
#[test]
fn test_accumulate_taa_measured_blend_factor_non_adaptive() {
let (w, h) = (4_usize, 3_usize);
let frame0 = vec![0.2_f32; w * h * 3];
let frame1 = vec![0.9_f32; w * h * 3];
let config = TaaConfig {
variance_clipping: false,
sharpen_strength: 0.0,
adaptive_blend: false,
blend_factor: 0.3,
..TaaConfig::default()
};
let mut history = TaaHistory::new(w, h);
accumulate_taa(&frame0, &mut history, &config).expect("bootstrap");
accumulate_taa(&frame1, &mut history, &config).expect("blend");
let measured = history.last_mean_blend_factor.expect("recorded");
assert!(
(measured - 0.3).abs() < 1e-6,
"expected 0.3, got {measured}"
);
}
#[test]
fn test_taa_history_reset_drops_measurement() {
let (w, h) = (3_usize, 3_usize);
let config = TaaConfig {
variance_clipping: false,
sharpen_strength: 0.0,
..TaaConfig::default()
};
let mut history = TaaHistory::new(w, h);
accumulate_taa(&vec![0.1_f32; w * h * 3], &mut history, &config).expect("bootstrap");
accumulate_taa(&vec![0.6_f32; w * h * 3], &mut history, &config).expect("blend");
assert!(history.last_mean_blend_factor.is_some());
history.reset();
assert_eq!(
history.last_mean_blend_factor, None,
"reset must drop the recorded blend factor"
);
let image = vec![0.0_f32; w * h * 3];
let stats = compute_taa_stats(&image, &image, &history, 0.17).expect("stats");
assert!(
(stats.mean_blend_factor - 0.17).abs() < 1e-6,
"expected the nominal fallback, got {}",
stats.mean_blend_factor
);
}
}