use taim_core::time::{Duration, Instant};
use taim_core::{Point, Rectangle, Size};
use taim_graphics::quality::Tier;
use taim_runtime::quality_controller::{Controller, FramePressure, Pressure};
use taim_wgpu::effects::pool::{Descriptor, Pool};
use taim_wgpu::effects::{Effect, Graph, blur::Blur};
fn descriptor(width: u32, height: u32) -> Descriptor {
Descriptor::new(width, height, taim_wgpu::wgpu::TextureFormat::Rgba8Unorm)
}
fn half_float(width: u32, height: u32) -> Descriptor {
Descriptor::new(width, height, taim_wgpu::wgpu::TextureFormat::Rgba16Float)
}
#[test]
fn a_pool_never_exceeds_its_byte_budget() {
let one = descriptor(64, 64);
let mut pool = Pool::new(one.bytes());
assert!(pool.acquire(one));
assert!(!pool.acquire(one));
assert!(pool.footprint_bytes() <= pool.budget_bytes());
}
#[test]
fn releasing_a_texture_makes_it_reusable_without_growing_the_footprint() {
let one = descriptor(64, 64);
let mut pool = Pool::new(one.bytes());
assert!(pool.acquire(one));
assert_eq!(pool.used_bytes(), one.bytes());
pool.release(one);
assert_eq!(pool.used_bytes(), 0);
assert_eq!(pool.reserved_bytes(), one.bytes());
assert!(pool.acquire(one));
assert_eq!(pool.used_bytes(), one.bytes());
assert_eq!(pool.footprint_bytes(), one.bytes());
}
#[test]
fn a_pool_evicts_idle_textures_to_admit_a_new_size_within_budget() {
let small = descriptor(64, 64);
let other = descriptor(64, 64); let mut pool = Pool::new(small.bytes());
assert!(pool.acquire(small));
pool.release(small);
assert!(pool.acquire(other));
assert!(pool.footprint_bytes() <= pool.budget_bytes());
}
#[test]
fn a_texture_larger_than_the_whole_budget_is_refused() {
let mut pool = Pool::new(1_024);
assert!(!pool.acquire(descriptor(64, 64)));
assert_eq!(pool.used_bytes(), 0);
assert_eq!(pool.footprint_bytes(), 0);
}
#[test]
fn heavy_reuse_stays_bounded_over_many_frames() {
let one = descriptor(128, 128);
let mut pool = Pool::new(one.bytes() * 2);
for _ in 0..10_000 {
assert!(pool.acquire(one));
assert!(pool.footprint_bytes() <= pool.budget_bytes());
pool.release(one);
}
assert!(pool.footprint_bytes() <= pool.budget_bytes());
}
#[test]
fn a_graph_refuses_effects_that_would_exceed_the_tier_area_budget() {
let surface = Rectangle::new(Point::ORIGIN, Size::new(4000.0, 4000.0));
let source = surface; let mut graph = Graph::new();
let admitted = graph.push_within_budget(
Effect::Blur(Blur { radius: 4.0 }),
source,
surface,
Tier::Efficient,
);
assert!(!admitted, "an over-budget-area effect must be refused");
assert!(graph.is_empty());
}
#[test]
fn a_graph_admits_small_effects_within_the_tier_area_budget() {
let surface = Rectangle::new(Point::ORIGIN, Size::new(1920.0, 1080.0));
let source = Rectangle::new(Point::new(50.0, 50.0), Size::new(60.0, 40.0));
let mut graph = Graph::new();
assert!(graph.push_within_budget(
Effect::Blur(Blur { radius: 6.0 }),
source,
surface,
Tier::Balanced
));
assert_eq!(graph.effects().len(), 1);
assert!(
graph.total_sampling_area(source, surface, Tier::Balanced)
<= Tier::Balanced.policy().max_effect_area
);
}
#[test]
fn tier_budgets_are_monotonic_from_neural_down_to_recovery() {
let tiers = [
Tier::Neural,
Tier::Ultra,
Tier::Balanced,
Tier::Efficient,
Tier::Recovery,
];
for pair in tiers.windows(2) {
let higher = pair[0].policy();
let lower = pair[1].policy();
assert!(lower.max_blur_radius <= higher.max_blur_radius);
assert!(lower.max_effect_area <= higher.max_effect_area);
assert!(lower.texture_budget_bytes <= higher.texture_budget_bytes);
assert!(!lower.backdrop_blur || higher.backdrop_blur);
assert!(!lower.noise || higher.noise);
}
}
#[test]
fn neural_tier_activates_upscale_reconstruction() {
let neural = Tier::Neural.policy();
assert!(neural.neural_reconstruction);
assert_eq!(neural.resolution_scale, 0.5);
assert!(neural.texture_budget_bytes >= 128 * 1024 * 1024);
let ultra = Tier::Ultra.policy();
assert!(!ultra.neural_reconstruction);
assert_eq!(ultra.resolution_scale, 1.0);
}
#[test]
fn recovery_disables_every_expensive_effect() {
let recovery = Tier::Recovery.policy();
assert_eq!(recovery.max_blur_radius, 0.0);
assert!(!recovery.backdrop_blur);
assert!(!recovery.noise);
}
fn pressure(gpu: f32, missed: f32, thermal: f32, recovering: bool) -> Pressure {
Pressure {
gpu_ratio: gpu,
missed_frame_ratio: missed,
thermal_ratio: thermal,
energy_ratio: 0.0,
recovering,
}
}
#[test]
fn ordinary_tier_changes_wait_out_the_residency_window() {
let now = Instant::now();
let mut controller = Controller::new(now).with_minimum_residency(Duration::from_secs(5));
let stressed = pressure(0.95, 0.0, 0.0, false);
assert_eq!(controller.update(now, stressed), Tier::Balanced);
assert_eq!(
controller.update(now + Duration::from_secs(2), stressed),
Tier::Balanced
);
assert_eq!(
controller.update(now + Duration::from_secs(5), stressed),
Tier::Efficient
);
}
#[test]
fn an_emergency_drops_to_recovery_immediately_bypassing_hysteresis() {
let now = Instant::now();
let mut controller = Controller::new(now).with_minimum_residency(Duration::from_secs(5));
let emergency = pressure(0.5, 0.0, 0.99, false);
assert_eq!(
controller.update(now, emergency),
Tier::Recovery,
"thermal-critical must drop to Recovery instantly"
);
let mut controller = Controller::new(now);
let recovering = pressure(0.2, 0.0, 0.2, true);
assert_eq!(
controller.update(now, recovering),
Tier::Recovery,
"active GPU recovery must drop to Recovery instantly"
);
}
#[test]
fn leaving_recovery_still_waits_out_the_residency_window() {
let now = Instant::now();
let mut controller = Controller::new(now).with_minimum_residency(Duration::from_secs(5));
assert_eq!(
controller.update(now, pressure(0.5, 0.0, 0.99, false)),
Tier::Recovery
);
let calm = pressure(0.2, 0.0, 0.2, false);
assert_eq!(
controller.update(now + Duration::from_secs(1), calm),
Tier::Recovery
);
assert_ne!(
controller.update(now + Duration::from_secs(6), calm),
Tier::Recovery
);
}
#[test]
fn a_controller_does_not_oscillate_under_a_signal_hovering_at_a_threshold() {
let start = Instant::now();
let mut controller = Controller::new(start).with_minimum_residency(Duration::from_secs(5));
let mut changes = 0;
let mut previous = controller.tier();
for frame in 0..600 {
let now = start + Duration::from_millis(frame * 16);
let gpu = if frame % 2 == 0 { 0.91 } else { 0.89 };
let tier = controller.update(now, pressure(gpu, 0.0, 0.0, false));
if tier != previous {
changes += 1;
previous = tier;
}
}
assert!(
changes <= 2,
"tier oscillated {changes} times under a hovering signal"
);
}
#[test]
fn frame_pressure_reports_a_measured_missed_ratio() {
let mut fp = FramePressure::new(Duration::from_millis(16), 8);
for i in 0..8 {
let duration = if i % 2 == 0 {
Duration::from_millis(20) } else {
Duration::from_millis(10) };
fp.record(duration);
}
assert!((fp.missed_frame_ratio() - 0.5).abs() < f32::EPSILON);
}
#[test]
fn frame_pressure_slides_its_window() {
let mut fp = FramePressure::new(Duration::from_millis(16), 4);
for _ in 0..4 {
fp.record(Duration::from_millis(30));
}
assert_eq!(fp.missed_frame_ratio(), 1.0);
for _ in 0..4 {
fp.record(Duration::from_millis(5));
}
assert_eq!(fp.missed_frame_ratio(), 0.0);
}
#[test]
fn a_half_float_intermediate_costs_twice_an_eight_bit_one() {
let eight_bit = descriptor(256, 256);
let half = half_float(256, 256);
assert_eq!(eight_bit.bytes(), 256 * 256 * 4);
assert_eq!(half.bytes(), eight_bit.bytes() * 2);
}
#[test]
fn accounting_follows_the_format_across_the_range_taim_uses() {
use taim_wgpu::wgpu::TextureFormat;
for (format, bytes_per_texel) in [
(TextureFormat::R8Unorm, 1),
(TextureFormat::Rgba8Unorm, 4),
(TextureFormat::Bgra8Unorm, 4),
(TextureFormat::Rgba8UnormSrgb, 4),
(TextureFormat::Bgra8UnormSrgb, 4),
(TextureFormat::Rgb10a2Unorm, 4),
(TextureFormat::R16Float, 2),
(TextureFormat::Rg16Float, 4),
(TextureFormat::Rgba16Float, 8),
(TextureFormat::Rgba32Float, 16),
] {
let descriptor = Descriptor::new(64, 64, format);
assert_eq!(
descriptor.bytes(),
64 * 64 * bytes_per_texel,
"{format:?} was priced wrongly"
);
}
}
#[test]
fn accounting_includes_samples_layers_and_mips() {
use taim_wgpu::wgpu::TextureFormat;
let base = Descriptor::new(64, 64, TextureFormat::Rgba8Unorm);
let msaa = Descriptor {
sample_count: 4,
..base
};
assert_eq!(msaa.bytes(), base.bytes() * 4);
let layered = Descriptor { layers: 6, ..base };
assert_eq!(layered.bytes(), base.bytes() * 6);
let mipped = Descriptor {
mip_level_count: 7,
..base
};
assert!(mipped.bytes() > base.bytes());
assert!(mipped.bytes() < base.bytes() * 2);
}
#[test]
fn depth_formats_are_priced_rather_than_treated_as_free() {
use taim_wgpu::wgpu::TextureFormat;
for format in [
TextureFormat::Depth32Float,
TextureFormat::Depth24Plus,
TextureFormat::Depth24PlusStencil8,
] {
let descriptor = Descriptor::new(64, 64, format);
assert!(
descriptor.bytes() >= 64 * 64,
"{format:?} was accounted as {} bytes",
descriptor.bytes()
);
}
}
#[test]
fn a_half_float_pool_admits_half_as_many_intermediates() {
let eight_bit = descriptor(256, 256);
let half = half_float(256, 256);
let budget = eight_bit.bytes() * 2;
let mut pool = Pool::new(budget);
assert!(pool.acquire(eight_bit));
assert!(pool.acquire(eight_bit));
assert!(!pool.acquire(eight_bit), "the budget did not bound at two");
let mut pool = Pool::new(budget);
assert!(pool.acquire(half));
assert!(
!pool.acquire(half),
"two half-float intermediates fitted a budget sized for two 8-bit ones"
);
}
#[test]
fn releasing_a_half_float_intermediate_reclaims_its_full_cost() {
let half = half_float(128, 128);
let mut pool = Pool::new(half.bytes() * 2);
assert!(pool.acquire(half));
assert_eq!(pool.used_bytes(), half.bytes());
pool.release(half);
assert_eq!(pool.used_bytes(), 0);
assert_eq!(pool.reserved_bytes(), half.bytes());
assert!(pool.acquire(half));
assert_eq!(pool.footprint_bytes(), half.bytes());
}
#[test]
fn textures_of_different_formats_do_not_substitute_for_one_another() {
let eight_bit = descriptor(128, 128);
let half = half_float(128, 128);
let mut pool = Pool::new(eight_bit.bytes() + half.bytes());
assert!(pool.acquire(eight_bit));
pool.release(eight_bit);
assert!(pool.acquire(half));
assert_eq!(pool.reserved_bytes(), eight_bit.bytes());
assert_eq!(pool.used_bytes(), half.bytes());
}