librashader_common/
viewport.rs

1use crate::{GetSize, Size};
2
3/// The rendering output of a filter chain.
4///
5/// Viewport coordinates are relative to the coordinate system of the
6/// target runtime. For correct results, `x` and `y`  should almost always be
7/// 0, and `size` should be the same as the size of the output texture.
8///
9/// Size uniforms will always be passed the full size of the output texture,
10/// regardless of the user-specified viewport size.
11pub struct Viewport<'a, T> {
12    /// The x offset to start rendering from. For correct results, this should almost
13    /// always be 0 to indicate the origin.
14    pub x: f32,
15    /// The y offset to begin rendering from.
16    pub y: f32,
17    /// An optional pointer to an MVP to use when rendering
18    /// to the viewport.
19    pub mvp: Option<&'a [f32; 16]>,
20    /// The output handle to render the final image to.
21    pub output: T,
22    /// The extent of the viewport size starting from the origin defined
23    /// by x and y.
24    pub size: Size<u32>,
25}
26
27impl<'a, T: GetSize<u32>> Viewport<'a, T> {
28    /// Create a new viewport from an output that can be sized.
29    ///
30    /// This will create a viewport that spans the entire output texture,
31    /// which will give correct results in the general case.
32    #[inline(always)]
33    pub fn new_render_target_sized_origin(
34        output: T,
35        mvp: Option<&'a [f32; 16]>,
36    ) -> Result<Viewport<'a, T>, T::Error> {
37        let size = output.size()?;
38        Ok(Self {
39            x: 0.0,
40            y: 0.0,
41            mvp,
42            output,
43            size,
44        })
45    }
46}