rt: cap texture scaling to [1, 16384]

Fixes #79, #78
This commit is contained in:
chyyran 2024-06-14 17:34:02 -04:00 committed by Ronny Chan
parent 0a9fa16855
commit fa48b936be

View file

@ -5,24 +5,27 @@ use librashader_presets::{Scale2D, ScaleFactor, ScaleType, Scaling};
use num_traits::AsPrimitive; use num_traits::AsPrimitive;
use std::ops::Mul; use std::ops::Mul;
pub const MAX_TEXEL_SIZE: f32 = 16384f32;
/// Trait for size scaling relative to the viewport. /// Trait for size scaling relative to the viewport.
pub trait ViewportSize<T> pub trait ViewportSize<T>
where where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static, T: Mul<ScaleFactor, Output = f32> + Copy + Ord + 'static,
f32: AsPrimitive<T>, f32: AsPrimitive<T>,
{ {
/// Produce a `Size<T>` scaled with the input scaling options. /// Produce a `Size<T>` scaled with the input scaling options.
/// The size will at minimum be 1x1, and at a maximum 16384x16384.
fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>, original: Size<T>) -> Size<T>; fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>, original: Size<T>) -> Size<T>;
} }
impl<T> ViewportSize<T> for Size<T> impl<T> ViewportSize<T> for Size<T>
where where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static, T: Mul<ScaleFactor, Output = f32> + Copy + Ord + 'static,
f32: AsPrimitive<T>, f32: AsPrimitive<T>,
{ {
fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>, original: Size<T>) -> Size<T> fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>, original: Size<T>) -> Size<T>
where where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static, T: Mul<ScaleFactor, Output = f32> + Copy + Ord + 'static,
f32: AsPrimitive<T>, f32: AsPrimitive<T>,
{ {
scaling::scale(scaling, self, viewport, original) scaling::scale(scaling, self, viewport, original)
@ -35,6 +38,7 @@ pub trait MipmapSize<T> {
fn calculate_miplevels(self) -> T; fn calculate_miplevels(self) -> T;
/// Scale the size according to the given mipmap level. /// Scale the size according to the given mipmap level.
/// The size will at minimum be 1x1, and at a maximum 16384x16384.
fn scale_mipmap(self, miplevel: T) -> Size<T>; fn scale_mipmap(self, miplevel: T) -> Size<T>;
} }
@ -59,7 +63,7 @@ impl MipmapSize<u32> for Size<u32> {
fn scale<T>(scaling: Scale2D, source: Size<T>, viewport: Size<T>, original: Size<T>) -> Size<T> fn scale<T>(scaling: Scale2D, source: Size<T>, viewport: Size<T>, original: Size<T>) -> Size<T>
where where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static, T: Mul<ScaleFactor, Output = f32> + Copy + Ord + 'static,
f32: AsPrimitive<T>, f32: AsPrimitive<T>,
{ {
let width = match scaling.x { let width = match scaling.x {
@ -101,8 +105,14 @@ where
}; };
Size { Size {
width: width.round().as_(), width: std::cmp::min(
height: height.round().as_(), std::cmp::max(width.round().as_(), 1f32.as_()),
MAX_TEXEL_SIZE.as_(),
),
height: std::cmp::min(
std::cmp::max(height.round().as_(), 1f32.as_()),
MAX_TEXEL_SIZE.as_(),
),
} }
} }