2023-02-07 18:12:47 +11:00
|
|
|
use crate::filter_pass::FilterPassMeta;
|
2022-12-22 13:39:31 +11:00
|
|
|
use crate::scaling;
|
2023-02-07 18:12:47 +11:00
|
|
|
use librashader_common::{ImageFormat, Size};
|
2022-11-30 15:56:10 +11:00
|
|
|
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-22 13:13:35 +11:00
|
|
|
pub trait ViewportSize<T>
|
2022-12-22 13:39:31 +11:00
|
|
|
where
|
|
|
|
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
|
|
|
|
f32: AsPrimitive<T>,
|
2022-12-22 13:13:35 +11:00
|
|
|
{
|
|
|
|
/// Produce a `Size<T>` scaled with the input scaling options.
|
|
|
|
fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>) -> Size<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ViewportSize<T> for Size<T>
|
2022-12-22 13:39:31 +11:00
|
|
|
where
|
|
|
|
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
|
|
|
|
f32: AsPrimitive<T>,
|
|
|
|
{
|
|
|
|
fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>) -> Size<T>
|
2022-12-22 13:13:35 +11:00
|
|
|
where
|
|
|
|
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
|
2022-12-22 13:39:31 +11:00
|
|
|
f32: AsPrimitive<T>,
|
|
|
|
{
|
2022-12-22 13:13:35 +11:00
|
|
|
scaling::scale(scaling, self, viewport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MipmapSize<T> {
|
|
|
|
/// Calculate the number of mipmap levels for a given size.
|
|
|
|
fn calculate_miplevels(self) -> T;
|
|
|
|
|
|
|
|
fn scale_mipmap(self, miplevel: T) -> Size<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MipmapSize<u32> for Size<u32> {
|
|
|
|
fn calculate_miplevels(self) -> u32 {
|
|
|
|
let mut size = std::cmp::max(self.width, self.height);
|
|
|
|
let mut levels = 0;
|
|
|
|
while size != 0 {
|
|
|
|
levels += 1;
|
|
|
|
size >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
levels
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scale_mipmap(self, miplevel: u32) -> Size<u32> {
|
|
|
|
let scaled_width = std::cmp::max(self.width >> miplevel, 1);
|
|
|
|
let scaled_height = std::cmp::max(self.height >> miplevel, 1);
|
|
|
|
Size::new(scaled_width, scaled_height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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_(),
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 18:12:47 +11:00
|
|
|
|
|
|
|
/// Trait for owned framebuffer objects that can be scaled.
|
|
|
|
pub trait ScaleableFramebuffer<T> {
|
|
|
|
type Error;
|
|
|
|
type Context: Copy;
|
|
|
|
/// Scale the framebuffer according to the provided parameters, returning the new size.
|
|
|
|
fn scale(
|
|
|
|
&mut self,
|
|
|
|
scaling: Scale2D,
|
|
|
|
format: ImageFormat,
|
|
|
|
viewport_size: &Size<u32>,
|
|
|
|
source_size: &Size<u32>,
|
|
|
|
should_mipmap: bool,
|
|
|
|
context: Self::Context,
|
|
|
|
) -> Result<Size<u32>, Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scale framebuffers according to the pass configs, source and viewport size.
|
2023-02-07 18:19:37 +11:00
|
|
|
#[inline(always)]
|
2023-02-07 18:12:47 +11:00
|
|
|
pub fn scale_framebuffers<T, F, E, P>(
|
|
|
|
source_size: Size<u32>,
|
|
|
|
viewport_size: Size<u32>,
|
|
|
|
output: &mut [F],
|
|
|
|
feedback: &mut [F],
|
|
|
|
passes: &[P],
|
|
|
|
) -> Result<(), E>
|
|
|
|
where
|
|
|
|
F: ScaleableFramebuffer<T, Context = (), Error = E>,
|
|
|
|
P: FilterPassMeta,
|
|
|
|
{
|
|
|
|
scale_framebuffers_with_context_callback(
|
|
|
|
source_size,
|
|
|
|
viewport_size,
|
|
|
|
output,
|
|
|
|
feedback,
|
|
|
|
passes,
|
|
|
|
(),
|
|
|
|
|_, _, _, _| Ok(()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scale framebuffers according to the pass configs, source and viewport size
|
|
|
|
/// passing a context into the scale function and a callback for each framebuffer rescale.
|
2023-02-07 18:19:37 +11:00
|
|
|
#[inline(always)]
|
2023-02-07 18:12:47 +11:00
|
|
|
pub fn scale_framebuffers_with_context_callback<T, F, E, C, P>(
|
|
|
|
source_size: Size<u32>,
|
|
|
|
viewport_size: Size<u32>,
|
|
|
|
output: &mut [F],
|
|
|
|
feedback: &mut [F],
|
|
|
|
passes: &[P],
|
|
|
|
context: C,
|
|
|
|
mut callback: impl FnMut(usize, &P, &F, &F) -> Result<(), E>,
|
|
|
|
) -> Result<(), E>
|
|
|
|
where
|
|
|
|
F: ScaleableFramebuffer<T, Context = C, Error = E>,
|
|
|
|
C: Copy,
|
|
|
|
P: FilterPassMeta,
|
|
|
|
{
|
|
|
|
assert_eq!(output.len(), feedback.len());
|
|
|
|
let mut iterator = passes.iter().enumerate().peekable();
|
|
|
|
let mut target_size = source_size;
|
|
|
|
while let Some((index, pass)) = iterator.next() {
|
|
|
|
let should_mipmap = iterator
|
|
|
|
.peek()
|
|
|
|
.map_or(false, |(_, p)| p.config().mipmap_input);
|
|
|
|
|
|
|
|
let next_size = output[index].scale(
|
|
|
|
pass.config().scaling.clone(),
|
|
|
|
pass.get_format(),
|
|
|
|
&viewport_size,
|
|
|
|
&target_size,
|
|
|
|
should_mipmap,
|
|
|
|
context,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
feedback[index].scale(
|
|
|
|
pass.config().scaling.clone(),
|
|
|
|
pass.get_format(),
|
|
|
|
&viewport_size,
|
|
|
|
&target_size,
|
|
|
|
should_mipmap,
|
|
|
|
context,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
target_size = next_size;
|
|
|
|
|
2023-02-07 18:19:37 +11:00
|
|
|
callback(index, pass, &output[index], &feedback[index])?;
|
2023-02-07 18:12:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|