Skip to content

Commit bbc0d2d

Browse files
bitshifterLegNeato
authored andcommitted
Use glam methods when available.
Some functionality has been added to glam since these shaders were ported to Rust-GPU.
1 parent 0739db6 commit bbc0d2d

File tree

1 file changed

+12
-79
lines changed

1 file changed

+12
-79
lines changed

shared/src/lib.rs

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ pub fn saturate(x: f32) -> f32 {
3535
}
3636

3737
pub fn pow(v: Vec3, power: f32) -> Vec3 {
38-
vec3(v.x.powf(power), v.y.powf(power), v.z.powf(power))
38+
v.powf(power)
3939
}
4040

4141
pub fn exp(v: Vec3) -> Vec3 {
42-
vec3(v.x.exp(), v.y.exp(), v.z.exp())
42+
v.exp()
4343
}
4444

4545
/// Based on: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
@@ -80,24 +80,6 @@ impl Clamp for f32 {
8080
}
8181
}
8282

83-
impl Clamp for Vec2 {
84-
fn clamp(self, min: Self, max: Self) -> Self {
85-
self.max(min).min(max)
86-
}
87-
}
88-
89-
impl Clamp for Vec3 {
90-
fn clamp(self, min: Self, max: Self) -> Self {
91-
self.max(min).min(max)
92-
}
93-
}
94-
95-
impl Clamp for Vec4 {
96-
fn clamp(self, min: Self, max: Self) -> Self {
97-
self.max(min).min(max)
98-
}
99-
}
100-
10183
pub trait FloatExt {
10284
fn gl_fract(self) -> Self;
10385
fn rem_euclid(self, rhs: Self) -> Self;
@@ -131,7 +113,7 @@ impl FloatExt for f32 {
131113
}
132114

133115
fn deg_to_radians(self) -> f32 {
134-
PI * self / 180.0
116+
self.to_radians()
135117
}
136118

137119
fn step(self, x: f32) -> f32 {
@@ -153,14 +135,12 @@ pub trait VecExt {
153135
fn rem_euclid(self, m: f32) -> Self;
154136
fn rem_euclid_vec(self, m: Self) -> Self;
155137
fn step(self, other: Self) -> Self;
156-
fn reflect(self, normal: Self) -> Self;
157-
fn distance(self, other: Self) -> f32;
158138
fn gl_sign(self) -> Self;
159139
}
160140

161141
impl VecExt for Vec2 {
162142
fn gl_fract(self) -> Vec2 {
163-
vec2(self.x.gl_fract(), self.y.gl_fract())
143+
self.fract_gl()
164144
}
165145

166146
fn sin(self) -> Vec2 {
@@ -184,33 +164,25 @@ impl VecExt for Vec2 {
184164
}
185165

186166
fn rem_euclid(self, m: f32) -> Vec2 {
187-
vec2(self.x.rem_euclid(m), self.y.rem_euclid(m))
167+
Vec2::rem_euclid(self, Vec2::splat(m))
188168
}
189169

190170
fn rem_euclid_vec(self, m: Vec2) -> Vec2 {
191-
vec2(self.x.rem_euclid(m.x), self.y.rem_euclid(m.y))
171+
Vec2::rem_euclid(self, m)
192172
}
193173

194174
fn step(self, other: Vec2) -> Vec2 {
195175
vec2(self.x.step(other.x), self.y.step(other.y))
196176
}
197177

198-
fn reflect(self, normal: Vec2) -> Vec2 {
199-
self - 2.0 * normal.dot(self) * normal
200-
}
201-
202-
fn distance(self, other: Vec2) -> f32 {
203-
(self - other).length()
204-
}
205-
206178
fn gl_sign(self) -> Vec2 {
207179
vec2(self.x.gl_sign(), self.y.gl_sign())
208180
}
209181
}
210182

211183
impl VecExt for Vec3 {
212184
fn gl_fract(self) -> Vec3 {
213-
vec3(self.x.gl_fract(), self.y.gl_fract(), self.z.gl_fract())
185+
self.fract_gl()
214186
}
215187

216188
fn sin(self) -> Vec3 {
@@ -234,19 +206,11 @@ impl VecExt for Vec3 {
234206
}
235207

236208
fn rem_euclid(self, m: f32) -> Vec3 {
237-
vec3(
238-
self.x.rem_euclid(m),
239-
self.y.rem_euclid(m),
240-
self.z.rem_euclid(m),
241-
)
209+
Vec3::rem_euclid(self, Vec3::splat(m))
242210
}
243211

244212
fn rem_euclid_vec(self, m: Vec3) -> Vec3 {
245-
vec3(
246-
self.x.rem_euclid(m.x),
247-
self.y.rem_euclid(m.y),
248-
self.z.rem_euclid(m.z),
249-
)
213+
Vec3::rem_euclid(self, m)
250214
}
251215

252216
fn step(self, other: Vec3) -> Vec3 {
@@ -257,27 +221,14 @@ impl VecExt for Vec3 {
257221
)
258222
}
259223

260-
fn reflect(self, normal: Vec3) -> Vec3 {
261-
self - 2.0 * normal.dot(self) * normal
262-
}
263-
264-
fn distance(self, other: Vec3) -> f32 {
265-
(self - other).length()
266-
}
267-
268224
fn gl_sign(self) -> Vec3 {
269225
vec3(self.x.gl_sign(), self.y.gl_sign(), self.z.gl_sign())
270226
}
271227
}
272228

273229
impl VecExt for Vec4 {
274230
fn gl_fract(self) -> Vec4 {
275-
vec4(
276-
self.x.gl_fract(),
277-
self.y.gl_fract(),
278-
self.z.gl_fract(),
279-
self.w.gl_fract(),
280-
)
231+
self.fract_gl()
281232
}
282233

283234
fn sin(self) -> Vec4 {
@@ -306,21 +257,11 @@ impl VecExt for Vec4 {
306257
}
307258

308259
fn rem_euclid(self, m: f32) -> Vec4 {
309-
vec4(
310-
self.x.rem_euclid(m),
311-
self.y.rem_euclid(m),
312-
self.z.rem_euclid(m),
313-
self.w.rem_euclid(m),
314-
)
260+
Vec4::rem_euclid(self, Vec4::splat(m))
315261
}
316262

317263
fn rem_euclid_vec(self, m: Vec4) -> Vec4 {
318-
vec4(
319-
self.x.rem_euclid(m.x),
320-
self.y.rem_euclid(m.y),
321-
self.z.rem_euclid(m.z),
322-
self.w.rem_euclid(m.w),
323-
)
264+
Vec4::rem_euclid(self, m)
324265
}
325266

326267
fn step(self, other: Vec4) -> Vec4 {
@@ -332,14 +273,6 @@ impl VecExt for Vec4 {
332273
)
333274
}
334275

335-
fn reflect(self, normal: Vec4) -> Vec4 {
336-
self - 2.0 * normal.dot(self) * normal
337-
}
338-
339-
fn distance(self, other: Vec4) -> f32 {
340-
(self - other).length()
341-
}
342-
343276
fn gl_sign(self) -> Vec4 {
344277
vec4(
345278
self.x.gl_sign(),

0 commit comments

Comments
 (0)