librashader/librashader-runtime/src/scaling.rs

59 lines
1.5 KiB
Rust
Raw Normal View History

2022-11-30 15:56:10 +11:00
use librashader_common::Size;
use librashader_presets::{Scale2D, ScaleFactor, ScaleType, Scaling};
use num_traits::AsPrimitive;
2022-11-30 17:38:05 +11:00
use std::ops::Mul;
2022-11-30 15:56:10 +11:00
2022-12-01 14:50:57 +11:00
/// Produce a `Size<T>` scaled with the input scaling options.
2022-11-30 15:56:10 +11:00
pub fn scale<T>(scaling: Scale2D, source: Size<T>, viewport: Size<T>) -> Size<T>
2022-11-30 17:38:05 +11:00
where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
f32: AsPrimitive<T>,
2022-11-30 15:56:10 +11:00
{
2022-12-01 14:50:57 +11:00
let width = match scaling.x {
2022-11-30 15:56:10 +11:00
Scaling {
scale_type: ScaleType::Input,
factor,
2022-12-01 14:50:57 +11:00
} => source.width * factor,
2022-11-30 15:56:10 +11:00
Scaling {
scale_type: ScaleType::Absolute,
factor,
2022-12-01 14:50:57 +11:00
} => factor.into(),
2022-11-30 15:56:10 +11:00
Scaling {
scale_type: ScaleType::Viewport,
factor,
2022-12-01 14:50:57 +11:00
} => viewport.width * factor,
2022-11-30 15:56:10 +11:00
};
2022-12-01 14:50:57 +11:00
let height = match scaling.y {
2022-11-30 15:56:10 +11:00
Scaling {
scale_type: ScaleType::Input,
factor,
2022-12-01 14:50:57 +11:00
} => source.height * factor,
2022-11-30 15:56:10 +11:00
Scaling {
scale_type: ScaleType::Absolute,
factor,
2022-12-01 14:50:57 +11:00
} => factor.into(),
2022-11-30 15:56:10 +11:00
Scaling {
scale_type: ScaleType::Viewport,
factor,
2022-12-01 14:50:57 +11:00
} => viewport.height * factor,
2022-11-30 15:56:10 +11:00
};
Size {
width: width.round().as_(),
height: height.round().as_(),
}
}
2022-12-01 14:50:57 +11:00
/// Calculate the number of mipmap levels for a given size.
2022-11-30 15:56:10 +11:00
pub fn calc_miplevel(size: Size<u32>) -> u32 {
let mut size = std::cmp::max(size.width, size.height);
let mut levels = 0;
while size != 0 {
levels += 1;
size >>= 1;
}
levels
2022-11-30 17:38:05 +11:00
}