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
/* SPDX-License-Identifier: LGPL-2.1-or-later OR MPL-2.0

Copyright (c) 2024 DorotaC
*/

/*! A shader outputting the same image without modifying */


use crate::egl::{ContextRef, CurrentContext, Dimensions, Texture};
use crate::raw;
use glium::{program, uniform};
use std::error::Error;
use super::Shader;

pub struct Passthrough;

impl Passthrough {
    /// Creates a shader using the builtin raw EGL facade.
    pub fn new(facade: &impl glium::backend::Facade, dims: (u32, u32))
        -> Result<Shader<Self>, Box<dyn Error>>
    {
        Shader::<Self>::new_with_facade(facade, dims)
    }
}

impl Shader<Passthrough> {
    /// The only thing that differs between the native R16 and RG88 versions is the fragment shader.
    fn new_with_facade<F: glium::backend::Facade>(
        facade: &F,
        // Output dimensions
        size: (u32, u32),
    ) -> Result<Self, Box<dyn Error>> {
        let (vertices, indices) = super::covering_vertices(facade, size)?;
        Ok(Self {
            program: program!(facade,
                120 => {
                    vertex: include_str!("passthrough/vert.glsl"),
                    fragment: include_str!("passthrough/frag.glsl"),
                }
            )?,
            vertices,
            indices,
            _data: Default::default(),
        })
    }
    
    /// Draws the same image
    pub fn convert<F: glium::backend::Facade>(
        &self,
        facade: &F,
        source_tex: Texture,
        target: &mut impl glium::Surface,
    ) -> Result<(), Box<dyn Error>>{
        let Dimensions { width, height } = source_tex.dimensions();
        if (width, height) != target.get_dimensions() {
            Err(super::Error::BadDimensions("Source and target dimensions must be equal"))?;
        }
        self.draw_any(
            facade,
            source_tex,
            target,
            uniform! {},
        )
    }
}