Repository: alacritty/alacritty
File: alacritty/src/renderer/text/gles2.rs (Rust)

Code before:

#[derive(Debug)]
pub struct TextShaderProgram {
    /// Shader program.
    program: ShaderProgram,

    /// Projection scale and offset uniform.
    u_projection: GLint,

    /// Rendering pass.
    ///
    /// For dual source blending, there're 2 passes, one for background, another for text,
    /// similar to GLES3 renderer.
    ///
    /// If GL_EXT_blend_func_extended is not available, the rendering is split into 4 passes.
    /// One is used for the background and the rest to perform subpixel text rendering according to
    /// https://github.com/servo/webrender/blob/master/webrender/doc/text-rendering.md.
    ///
    /// Rendering is split into three passes.
    u_rendering_pass: GLint,
}

impl TextShaderProgram {
    pub fn new(shader_version: ShaderVersion, dual_source_blending: bool) -> Result<Self, Error> {
        let fragment_shader =
            if dual_source_blending { &super::glsl3::TEXT_SHADER_F } else { &TEXT_SHADER_F };

        let program = ShaderProgram::new(shader_version, TEXT_SHADER_V, fragment_shader)?;

        Ok(Self {
            u_projection: program.get_uniform_location(cstr!("projection"))?,
            u_rendering_pass: program.get_uniform_location(cstr!("renderingPass"))?,
            program,
        })
    }

    fn set_rendering_pass(&self, rendering_pass: RenderingPass) {
        unsafe { gl::Uniform1i(self.u_rendering_pass, rendering_pass as i32) }
    }
}

impl TextShader for TextShaderProgram {
    fn id(&self) -> GLuint {
        self.program.id()
    }

    fn projection_uniform(&self) -> GLint {
        self.u_projection
    }
}


Review comment (suggestion):
Should use imports and fully qualified paths. `super::` just gets confusing.

Code after:
pub struct TextShaderProgram {
    /// Shader program.
    program: ShaderProgram,

    /// Projection scale and offset uniform.
    u_projection: GLint,

    /// Rendering pass.
    ///
    /// For dual source blending, there are 2 passes; one for background, another for text,
    /// similar to the GLSL3 renderer.
    ///
    /// If GL_EXT_blend_func_extended is not available, the rendering is split into 4 passes.
    /// One is used for the background and the rest to perform subpixel text rendering according to
    /// https://github.com/servo/webrender/blob/master/webrender/doc/text-rendering.md.
    ///
    /// Rendering is split into three passes.
    u_rendering_pass: GLint,
}

impl TextShaderProgram {
    pub fn new(shader_version: ShaderVersion, dual_source_blending: bool) -> Result<Self, Error> {
        let fragment_shader =
            if dual_source_blending { &glsl3::TEXT_SHADER_F } else { &TEXT_SHADER_F };

        let program = ShaderProgram::new(shader_version, TEXT_SHADER_V, fragment_shader)?;

        Ok(Self {
            u_projection: program.get_uniform_location(cstr!("projection"))?,
            u_rendering_pass: program.get_uniform_location(cstr!("renderingPass"))?,
            program,
        })
    }

    fn set_rendering_pass(&self, rendering_pass: RenderingPass) {
        unsafe { gl::Uniform1i(self.u_rendering_pass, rendering_pass as i32) }
    }
}

impl TextShader for TextShaderProgram {
    fn id(&self) -> GLuint {
        self.program.id()
    }

    fn projection_uniform(&self) -> GLint {
        self.u_projection
    }
}