webgl_rc/
impls.rs

1use super::data_buffer::Writable;
2use super::texture::Texture;
3use super::types::{DataType, TypeMark};
4use crate::uniforms::{IntoUniform, UniformValue};
5
6// f32
7
8impl Writable for f32 {
9    fn write(&self, output: &mut Vec<f32>) {
10        output.push(*self);
11    }
12    fn stride() -> usize {
13        return 1;
14    }
15}
16
17impl TypeMark for f32 {
18    fn data_type() -> DataType {
19        DataType::Float
20    }
21}
22
23impl IntoUniform for f32 {
24    fn into_uniform(&self) -> UniformValue {
25        UniformValue::Float(*self)
26    }
27}
28
29// Texture
30
31impl TypeMark for Texture {
32    fn data_type() -> DataType {
33        DataType::Sampler
34    }
35}
36
37impl IntoUniform for Texture {
38    fn into_uniform(&self) -> UniformValue {
39        UniformValue::Texture(self.clone())
40    }
41}
42
43// Option<Texture>
44
45impl TypeMark for Option<Texture> {
46    fn data_type() -> DataType {
47        DataType::Sampler
48    }
49}
50
51impl IntoUniform for Option<Texture> {
52    fn into_uniform(&self) -> UniformValue {
53        self.as_ref()
54            .map(|texture| UniformValue::Texture(texture.clone()))
55            .unwrap_or(UniformValue::None)
56    }
57}
58
59// Boolean
60
61impl TypeMark for bool {
62    fn data_type() -> DataType {
63        DataType::Boolean
64    }
65}
66
67impl From<bool> for UniformValue {
68    fn from(value: bool) -> Self {
69        UniformValue::Boolean(value)
70    }
71}
72
73impl IntoUniform for bool {
74    fn into_uniform(&self) -> UniformValue {
75        UniformValue::Boolean(*self)
76    }
77}
78
79// [f32;2]
80
81impl TypeMark for [f32; 2] {
82    fn data_type() -> DataType {
83        DataType::Vec2
84    }
85}
86
87impl IntoUniform for [f32; 2] {
88    fn into_uniform(&self) -> UniformValue {
89        UniformValue::Vec2(*self)
90    }
91}
92
93impl Writable for [f32; 2] {
94    fn write(&self, output: &mut Vec<f32>) {
95        for v in self {
96            output.push(*v);
97        }
98    }
99    fn stride() -> usize {
100        2
101    }
102}
103
104// [f32;3]
105
106impl TypeMark for [f32; 3] {
107    fn data_type() -> DataType {
108        DataType::Vec3
109    }
110}
111
112impl IntoUniform for [f32; 3] {
113    fn into_uniform(&self) -> UniformValue {
114        UniformValue::Vec3(*self)
115    }
116}
117
118impl Writable for [f32; 3] {
119    fn write(&self, output: &mut Vec<f32>) {
120        for v in self {
121            output.push(*v);
122        }
123    }
124    fn stride() -> usize {
125        3
126    }
127}
128
129// [f32;4]
130
131impl TypeMark for [f32; 4] {
132    fn data_type() -> DataType {
133        DataType::Vec4
134    }
135}
136
137impl IntoUniform for [f32; 4] {
138    fn into_uniform(&self) -> UniformValue {
139        UniformValue::Vec4(*self)
140    }
141}
142
143impl Writable for [f32; 4] {
144    fn write(&self, output: &mut Vec<f32>) {
145        for v in self {
146            output.push(*v);
147        }
148    }
149    fn stride() -> usize {
150        4
151    }
152}
153
154// (f32, f32)
155
156impl TypeMark for (f32, f32) {
157    fn data_type() -> DataType {
158        DataType::Vec2
159    }
160}
161
162impl IntoUniform for (f32, f32) {
163    fn into_uniform(&self) -> UniformValue {
164        UniformValue::Vec2([self.0, self.1])
165    }
166}
167
168impl Writable for (f32, f32) {
169    fn write(&self, output: &mut Vec<f32>) {
170        output.push(self.0);
171        output.push(self.1);
172    }
173    fn stride() -> usize {
174        2
175    }
176}
177
178// (f32, f32, f32)
179
180impl TypeMark for (f32, f32, f32) {
181    fn data_type() -> DataType {
182        DataType::Vec3
183    }
184}
185
186impl IntoUniform for (f32, f32, f32) {
187    fn into_uniform(&self) -> UniformValue {
188        UniformValue::Vec3([self.0, self.1, self.2])
189    }
190}
191
192impl Writable for (f32, f32, f32) {
193    fn write(&self, output: &mut Vec<f32>) {
194        output.push(self.0);
195        output.push(self.1);
196        output.push(self.2);
197    }
198    fn stride() -> usize {
199        3
200    }
201}
202
203// (f32, f32, f32, f32)
204
205impl TypeMark for (f32, f32, f32, f32) {
206    fn data_type() -> DataType {
207        DataType::Vec4
208    }
209}
210
211impl IntoUniform for (f32, f32, f32, f32) {
212    fn into_uniform(&self) -> UniformValue {
213        UniformValue::Vec4([self.0, self.1, self.2, self.3])
214    }
215}
216
217impl Writable for (f32, f32, f32, f32) {
218    fn write(&self, output: &mut Vec<f32>) {
219        output.push(self.0);
220        output.push(self.1);
221        output.push(self.2);
222        output.push(self.3);
223    }
224    fn stride() -> usize {
225        4
226    }
227}