use super::*;
#[must_use]
pub fn clamp_viewport_scale(raw_scale: f32) -> f32 {
if raw_scale.is_finite() && raw_scale > 0.0 {
raw_scale.clamp(VIEWPORT_SCALE_MIN, VIEWPORT_SCALE_MAX)
} else {
1.0
}
}
#[must_use]
pub fn compute_lossless_physical_dim(logical_extent: f64, scale_factor: f32) -> u32 {
let safe_logical: f64 = if logical_extent.is_finite() && logical_extent > 0.0 {
logical_extent.max(1.0)
} else {
1.0
};
let safe_scale: f64 = f64::from(clamp_viewport_scale(scale_factor));
let raw_phys: f64 = (safe_logical * safe_scale).round();
if !raw_phys.is_finite() || raw_phys < 1.0 {
1
} else if raw_phys >= f64::from(u32::MAX) {
u32::MAX
} else {
raw_phys as u32
}
}
#[must_use]
pub fn compute_effective_tile_dims(
physical_width: u32,
physical_height: u32,
limits: SurfaceTileLimit,
) -> (u32, u32) {
let phys_w: u32 = physical_width.max(1);
let phys_h: u32 = physical_height.max(1);
let max_dim: u32 = limits.get_max_texture_dim().max(MIN_SURFACE_TILE_DIM);
let min_area: u64 =
u64::from(MIN_SURFACE_TILE_DIM).saturating_mul(u64::from(MIN_SURFACE_TILE_DIM));
let max_area: u64 = limits.get_max_surface_area().max(min_area);
let mut tile_w: u32 = phys_w.min(max_dim);
let mut tile_h: u32 = phys_h.min(max_dim);
let area: u64 = u64::from(tile_w).saturating_mul(u64::from(tile_h));
if area <= max_area {
return (tile_w, tile_h);
}
let aspect: f64 = f64::from(tile_w) / f64::from(tile_h.max(1));
let target_w: u32 = ((max_area as f64) * aspect)
.sqrt()
.floor()
.clamp(1.0, f64::from(tile_w)) as u32;
tile_w = target_w.max(1).min(max_dim);
let max_h_for_w: u64 = max_area / u64::from(tile_w);
tile_h = (max_h_for_w.min(u64::from(tile_h)) as u32).max(1);
while u64::from(tile_w).saturating_mul(u64::from(tile_h)) > max_area {
if tile_w >= tile_h && tile_w > 1 {
tile_w -= 1;
} else if tile_h > 1 {
tile_h -= 1;
} else {
break;
}
}
(tile_w, tile_h)
}