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
|
use crate::program::Version;
use crate::{triangle, Color};
use glow::{Context, HasContext, NativeProgram};
use iced_graphics::Transformation;
#[derive(Debug)]
pub struct Program {
program: <Context as HasContext>::Program,
uniform_data: UniformData,
}
#[derive(Debug)]
struct UniformData {
pub color: Color,
pub color_location: <Context as HasContext>::UniformLocation,
pub transform: Transformation,
pub transform_location: <Context as HasContext>::UniformLocation,
}
impl UniformData {
fn new(gl: &Context, program: NativeProgram) -> Self {
Self {
color: Color::TRANSPARENT,
color_location: unsafe {
gl.get_uniform_location(program, "color")
}
.expect("Solid - Get color."),
transform: Transformation::identity(),
transform_location: unsafe {
gl.get_uniform_location(program, "u_Transform")
}
.expect("Solid - Get u_Transform."),
}
}
}
impl Program {
pub fn new(gl: &Context, shader_version: &Version) -> Self {
let program = triangle::program(
gl,
shader_version,
include_str!("../shader/common/triangle.frag"),
);
Self {
program,
uniform_data: UniformData::new(gl, program),
}
}
pub fn write_uniforms(
&mut self,
gl: &Context,
color: &Color,
transform: &Transformation,
) {
if transform != &self.uniform_data.transform {
triangle::set_transform(
gl,
self.uniform_data.transform_location,
*transform,
)
}
if color != &self.uniform_data.color {
unsafe {
gl.uniform_4_f32(
Some(&self.uniform_data.color_location),
color.r,
color.g,
color.b,
color.a,
);
}
self.uniform_data.color = *color;
}
}
pub fn use_program(
&mut self,
gl: &Context,
color: &Color,
transform: &Transformation,
) {
unsafe { gl.use_program(Some(self.program)) }
self.write_uniforms(gl, color, transform)
}
}
|