1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::filter_pass::FilterPassMeta;
use crate::scaling;
use librashader_common::{ImageFormat, Size};
use librashader_presets::{Scale2D, ScaleFactor, ScaleType, Scaling};
use num_traits::AsPrimitive;
use std::ops::Mul;
pub trait ViewportSize<T>
where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
f32: AsPrimitive<T>,
{
fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>) -> Size<T>;
}
impl<T> ViewportSize<T> for Size<T>
where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
f32: AsPrimitive<T>,
{
fn scale_viewport(self, scaling: Scale2D, viewport: Size<T>) -> Size<T>
where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
f32: AsPrimitive<T>,
{
scaling::scale(scaling, self, viewport)
}
}
pub trait MipmapSize<T> {
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>
where
T: Mul<ScaleFactor, Output = f32> + Copy + 'static,
f32: AsPrimitive<T>,
{
let width = match scaling.x {
Scaling {
scale_type: ScaleType::Input,
factor,
} => source.width * factor,
Scaling {
scale_type: ScaleType::Absolute,
factor,
} => factor.into(),
Scaling {
scale_type: ScaleType::Viewport,
factor,
} => viewport.width * factor,
};
let height = match scaling.y {
Scaling {
scale_type: ScaleType::Input,
factor,
} => source.height * factor,
Scaling {
scale_type: ScaleType::Absolute,
factor,
} => factor.into(),
Scaling {
scale_type: ScaleType::Viewport,
factor,
} => viewport.height * factor,
};
Size {
width: width.round().as_(),
height: height.round().as_(),
}
}
pub trait ScaleFramebuffer<T = ()> {
type Error;
type Context;
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>;
#[inline(always)]
fn scale_framebuffers<P>(
source_size: Size<u32>,
viewport_size: Size<u32>,
output: &mut [Self],
feedback: &mut [Self],
passes: &[P],
callback: Option<&mut dyn FnMut(usize, &P, &Self, &Self) -> Result<(), Self::Error>>,
) -> Result<(), Self::Error>
where
Self: Sized,
Self::Context: Default,
P: FilterPassMeta,
{
scale_framebuffers_with_context_callback::<T, Self, Self::Error, Self::Context, _>(
source_size,
viewport_size,
output,
feedback,
passes,
&Self::Context::default(),
callback,
)
}
#[inline(always)]
fn scale_framebuffers_with_context<P>(
source_size: Size<u32>,
viewport_size: Size<u32>,
output: &mut [Self],
feedback: &mut [Self],
passes: &[P],
context: &Self::Context,
callback: Option<&mut dyn FnMut(usize, &P, &Self, &Self) -> Result<(), Self::Error>>,
) -> Result<(), Self::Error>
where
Self: Sized,
P: FilterPassMeta,
{
scale_framebuffers_with_context_callback::<T, Self, Self::Error, Self::Context, _>(
source_size,
viewport_size,
output,
feedback,
passes,
context,
callback,
)
}
}
#[inline(always)]
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: Option<&mut dyn FnMut(usize, &P, &F, &F) -> Result<(), E>>,
) -> Result<(), E>
where
F: ScaleFramebuffer<T, Context = C, Error = E>,
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;
if let Some(callback) = callback.as_mut() {
callback(index, pass, &output[index], &feedback[index])?;
}
}
Ok(())
}