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
use super::{BlendEquation, BlendFactor, CmpFunction, WebGlCommon, WebGlRenderer};
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext};

pub trait PartialWebGlFuncs {
    fn awsm_depth_func(&self, func: CmpFunction);
    fn awsm_blend_color(&self, r: f32, g: f32, b: f32, a: f32);
    fn awsm_blend_func(&self, sfactor: BlendFactor, dfactor: BlendFactor);
    fn awsm_blend_func_separate(
        &self,
        src_rgb: BlendFactor,
        dest_rgb: BlendFactor,
        src_alpha: BlendFactor,
        dest_alpha: BlendFactor,
    );
    fn awsm_blend_equation(&self, mode: BlendEquation);
    fn awsm_blend_equation_separate(&self, rgb_mode: BlendEquation, alpha_mode: BlendEquation);
}

macro_rules! impl_context {
    ($($type:ty { $($defs:tt)* })+) => {
        $(impl PartialWebGlFuncs for $type {
            fn awsm_depth_func(&self, func:CmpFunction) {
                self.depth_func(func as u32);
            }
            fn awsm_blend_color(&self, r:f32, g:f32, b:f32, a:f32) {
                self.blend_color(r, g, b, a);
            }
            fn awsm_blend_func(&self, sfactor: BlendFactor, dfactor: BlendFactor) {
                self.blend_func(sfactor as u32, dfactor as u32);
            }
            fn awsm_blend_func_separate(&self, src_rgb: BlendFactor, dest_rgb: BlendFactor, src_alpha:BlendFactor, dest_alpha: BlendFactor) {
                self.blend_func_separate(src_rgb as u32, dest_rgb as u32, src_alpha as u32, dest_alpha as u32);
            }
            fn awsm_blend_equation(&self, mode:BlendEquation) {
                self.blend_equation(mode as u32);
            }
            fn awsm_blend_equation_separate(&self, rgb_mode:BlendEquation, alpha_mode:BlendEquation) {
                self.blend_equation_separate(rgb_mode as u32, alpha_mode as u32);
            }
            $($defs)*
        })+
    };
}

impl_context! {
    WebGlRenderingContext{}
    WebGl2RenderingContext{}
}

pub struct FuncSettings {
    pub depth_func: CmpFunction,
    pub blend_color: (f32, f32, f32, f32),
    pub blend_func: (BlendFactor, BlendFactor),
    pub blend_func_separate: (BlendFactor, BlendFactor, BlendFactor, BlendFactor),
    pub blend_equation: BlendEquation,
    pub blend_equation_separate: (BlendEquation, BlendEquation),
}

impl Default for FuncSettings {
    fn default() -> Self {
        Self {
            depth_func: CmpFunction::Less,
            blend_color: (0.0, 0.0, 0.0, 0.0),
            blend_func: (BlendFactor::One, BlendFactor::Zero),
            blend_func_separate: (
                BlendFactor::One,
                BlendFactor::Zero,
                BlendFactor::One,
                BlendFactor::Zero,
            ),
            blend_equation: BlendEquation::Add,
            blend_equation_separate: (BlendEquation::Add, BlendEquation::Add),
        }
    }
}

impl<T: WebGlCommon> WebGlRenderer<T> {
    pub fn set_depth_func(&mut self, func: CmpFunction) {
        if self.func_settings.depth_func != func {
            self.gl.awsm_depth_func(func);
            self.func_settings.depth_func = func;
        }
    }
    pub fn set_blend_color(&mut self, r: f32, g: f32, b: f32, a: f32) {
        let curr = self.func_settings.blend_color;

        if curr.0 != r || curr.1 != g || curr.2 != b || curr.3 != a {
            self.gl.awsm_blend_color(r, g, b, a);
            self.func_settings.blend_color = (r, g, b, a);
        }
    }

    pub fn set_blend_func(&mut self, sfactor: BlendFactor, dfactor: BlendFactor) {
        let curr = self.func_settings.blend_func;

        if curr.0 != sfactor || curr.1 != dfactor {
            self.gl.awsm_blend_func(sfactor, dfactor);
            self.func_settings.blend_func = (sfactor, dfactor);
        }
    }
    pub fn set_blend_func_separate(
        &mut self,
        src_rgb: BlendFactor,
        dest_rgb: BlendFactor,
        src_alpha: BlendFactor,
        dest_alpha: BlendFactor,
    ) {
        let curr = self.func_settings.blend_func_separate;

        if curr.0 != src_rgb || curr.1 != dest_rgb || curr.2 != src_alpha || curr.3 != dest_alpha {
            self.gl
                .awsm_blend_func_separate(src_rgb, dest_rgb, src_alpha, dest_alpha);
            self.func_settings.blend_func_separate = (src_rgb, dest_rgb, src_alpha, dest_alpha);
        }
    }
    pub fn set_blend_equation(&mut self, mode: BlendEquation) {
        if self.func_settings.blend_equation != mode {
            self.gl.awsm_blend_equation(mode);
            self.func_settings.blend_equation = mode;
        }
    }
    pub fn set_blend_equation_separate(
        &mut self,
        rgb_mode: BlendEquation,
        alpha_mode: BlendEquation,
    ) {
        let curr = self.func_settings.blend_equation_separate;

        if curr.0 != rgb_mode || curr.1 != alpha_mode {
            self.gl.awsm_blend_equation_separate(rgb_mode, alpha_mode);
            self.func_settings.blend_equation_separate = (rgb_mode, alpha_mode);
        }
    }
}