Skip to content

Commit 4045d84

Browse files
committed
Cleanup sweep sweep.
1 parent 2749c6b commit 4045d84

File tree

11 files changed

+675
-550
lines changed

11 files changed

+675
-550
lines changed

Cargo.lock

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use-installed-tools = ["spirv-builder/use-installed-tools"]
1111
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
1212

1313
[dependencies]
14-
shared = { path = "shared" }
1514
shadertoys-shaders = { path = "shaders" }
1615
futures = { version = "0.3", default-features = false, features = [
1716
"std",
@@ -27,7 +26,7 @@ ouroboros = "0.18.5"
2726
spirv-builder.workspace = true
2827

2928
[workspace]
30-
members = ["shaders", "shared"]
29+
members = ["shaders"]
3130

3231
[workspace.dependencies]
3332
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "0b37696e9f5edde8fa0c1363a88e6c8cb8e6ff68", default-features = false }

shaders/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ crate-type = ["dylib"]
99

1010
[dependencies]
1111
spirv-std.workspace = true
12-
shared = { path = "../shared" }
12+
bytemuck = { version = "1.20.0", features = ["derive"] }
1313

1414
[lints]
1515
workspace = true

shaders/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub mod shader_prelude;
44
use shader_prelude::*;
55
pub mod shaders;
6+
pub mod shared_data;
67

78
#[inline(always)]
89
pub fn fs(constants: &ShaderConstants, mut frag_coord: Vec2) -> Vec4 {

shaders/src/shader_prelude.rs

Lines changed: 246 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
pub use core::f32::consts::{FRAC_1_PI, FRAC_PI_2, PI};
2+
use core::ops::{Add, Mul, Sub};
23
pub const TWO_PI: f32 = 2.0 * PI;
34
pub const SQRT3: f32 = 1.7320508075688772;
45

5-
pub use shared::*;
6+
pub use crate::shared_data::ShaderConstants;
67
pub use spirv_std::{
78
arch::Derivative,
89
glam::{
@@ -59,3 +60,247 @@ pub struct ShaderResult {
5960
pub struct ShaderDefinition {
6061
pub name: &'static str,
6162
}
63+
64+
#[inline(always)]
65+
pub fn saturate_vec3(a: Vec3) -> Vec3 {
66+
a.clamp(Vec3::ZERO, Vec3::ONE)
67+
}
68+
#[inline(always)]
69+
pub fn saturate_vec2(a: Vec2) -> Vec2 {
70+
a.clamp(Vec2::ZERO, Vec2::ONE)
71+
}
72+
#[inline(always)]
73+
pub fn saturate(a: f32) -> f32 {
74+
a.clamp(0.0, 1.0)
75+
}
76+
77+
/// Based on: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
78+
#[inline]
79+
pub fn acos_approx(v: f32) -> f32 {
80+
let x = v.abs();
81+
let mut res = -0.155972 * x + 1.56467; // p(x)
82+
res *= (1.0f32 - x).sqrt();
83+
84+
if v >= 0.0 {
85+
res
86+
} else {
87+
PI - res
88+
}
89+
}
90+
91+
#[inline(always)]
92+
pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
93+
// Scale, bias and saturate x to 0..1 range
94+
let x = saturate((x - edge0) / (edge1 - edge0));
95+
// Evaluate polynomial
96+
x * x * (3.0 - 2.0 * x)
97+
}
98+
99+
#[inline(always)]
100+
pub fn mix<X: Copy + Mul<A, Output = X> + Add<Output = X> + Sub<Output = X>, A: Copy>(
101+
x: X,
102+
y: X,
103+
a: A,
104+
) -> X {
105+
x - x * a + y * a
106+
}
107+
108+
pub trait Clamp {
109+
fn clamp(self, min: Self, max: Self) -> Self;
110+
}
111+
112+
impl Clamp for f32 {
113+
#[inline(always)]
114+
fn clamp(self, min: Self, max: Self) -> Self {
115+
self.max(min).min(max)
116+
}
117+
}
118+
119+
pub trait FloatExt {
120+
fn fract_gl(self) -> Self;
121+
fn rem_euclid(self, rhs: Self) -> Self;
122+
fn sign_gl(self) -> Self;
123+
fn step(self, x: Self) -> Self;
124+
}
125+
126+
impl FloatExt for f32 {
127+
#[inline]
128+
fn fract_gl(self) -> f32 {
129+
self - self.floor()
130+
}
131+
132+
#[inline]
133+
fn rem_euclid(self, rhs: f32) -> f32 {
134+
let r = self % rhs;
135+
if r < 0.0 {
136+
r + rhs.abs()
137+
} else {
138+
r
139+
}
140+
}
141+
142+
#[inline]
143+
fn sign_gl(self) -> f32 {
144+
if self < 0.0 {
145+
-1.0
146+
} else if self == 0.0 {
147+
0.0
148+
} else {
149+
1.0
150+
}
151+
}
152+
153+
#[inline]
154+
fn step(self, x: f32) -> f32 {
155+
if x < self {
156+
0.0
157+
} else {
158+
1.0
159+
}
160+
}
161+
}
162+
163+
pub trait VecExt {
164+
fn sin(self) -> Self;
165+
fn cos(self) -> Self;
166+
fn powf_vec(self, p: Self) -> Self;
167+
fn sqrt(self) -> Self;
168+
fn ln(self) -> Self;
169+
fn step(self, other: Self) -> Self;
170+
fn sign_gl(self) -> Self;
171+
}
172+
173+
impl VecExt for Vec2 {
174+
#[inline]
175+
fn sin(self) -> Vec2 {
176+
vec2(self.x.sin(), self.y.sin())
177+
}
178+
179+
#[inline]
180+
fn cos(self) -> Vec2 {
181+
vec2(self.x.cos(), self.y.cos())
182+
}
183+
184+
#[inline]
185+
fn powf_vec(self, p: Vec2) -> Vec2 {
186+
vec2(self.x.powf(p.x), self.y.powf(p.y))
187+
}
188+
189+
#[inline]
190+
fn sqrt(self) -> Vec2 {
191+
vec2(self.x.sqrt(), self.y.sqrt())
192+
}
193+
194+
#[inline]
195+
fn ln(self) -> Vec2 {
196+
vec2(self.x.ln(), self.y.ln())
197+
}
198+
199+
#[inline]
200+
fn step(self, other: Vec2) -> Vec2 {
201+
vec2(self.x.step(other.x), self.y.step(other.y))
202+
}
203+
204+
#[inline]
205+
fn sign_gl(self) -> Vec2 {
206+
vec2(self.x.sign_gl(), self.y.sign_gl())
207+
}
208+
}
209+
210+
impl VecExt for Vec3 {
211+
#[inline]
212+
fn sin(self) -> Vec3 {
213+
vec3(self.x.sin(), self.y.sin(), self.z.sin())
214+
}
215+
216+
#[inline]
217+
fn cos(self) -> Vec3 {
218+
vec3(self.x.cos(), self.y.cos(), self.z.cos())
219+
}
220+
221+
#[inline]
222+
fn powf_vec(self, p: Vec3) -> Vec3 {
223+
vec3(self.x.powf(p.x), self.y.powf(p.y), self.z.powf(p.z))
224+
}
225+
226+
#[inline]
227+
fn sqrt(self) -> Vec3 {
228+
vec3(self.x.sqrt(), self.y.sqrt(), self.z.sqrt())
229+
}
230+
231+
#[inline]
232+
fn ln(self) -> Vec3 {
233+
vec3(self.x.ln(), self.y.ln(), self.z.ln())
234+
}
235+
236+
#[inline]
237+
fn step(self, other: Vec3) -> Vec3 {
238+
vec3(
239+
self.x.step(other.x),
240+
self.y.step(other.y),
241+
self.z.step(other.z),
242+
)
243+
}
244+
245+
#[inline]
246+
fn sign_gl(self) -> Vec3 {
247+
vec3(self.x.sign_gl(), self.y.sign_gl(), self.z.sign_gl())
248+
}
249+
}
250+
251+
impl VecExt for Vec4 {
252+
#[inline]
253+
fn sin(self) -> Vec4 {
254+
vec4(self.x.sin(), self.y.sin(), self.z.sin(), self.w.sin())
255+
}
256+
257+
#[inline]
258+
fn cos(self) -> Vec4 {
259+
vec4(self.x.cos(), self.y.cos(), self.z.cos(), self.w.cos())
260+
}
261+
262+
#[inline]
263+
fn powf_vec(self, p: Vec4) -> Vec4 {
264+
vec4(
265+
self.x.powf(p.x),
266+
self.y.powf(p.y),
267+
self.z.powf(p.z),
268+
self.w.powf(p.w),
269+
)
270+
}
271+
272+
#[inline]
273+
fn sqrt(self) -> Vec4 {
274+
vec4(self.x.sqrt(), self.y.sqrt(), self.z.sqrt(), self.w.sqrt())
275+
}
276+
277+
#[inline]
278+
fn ln(self) -> Vec4 {
279+
vec4(self.x.ln(), self.y.ln(), self.z.ln(), self.w.ln())
280+
}
281+
282+
#[inline]
283+
fn step(self, other: Vec4) -> Vec4 {
284+
vec4(
285+
self.x.step(other.x),
286+
self.y.step(other.y),
287+
self.z.step(other.z),
288+
self.w.step(other.w),
289+
)
290+
}
291+
292+
#[inline]
293+
fn sign_gl(self) -> Vec4 {
294+
vec4(
295+
self.x.sign_gl(),
296+
self.y.sign_gl(),
297+
self.z.sign_gl(),
298+
self.w.sign_gl(),
299+
)
300+
}
301+
}
302+
303+
#[inline(always)]
304+
pub fn discard() {
305+
unsafe { spirv_std::arch::demote_to_helper_invocation() }
306+
}

0 commit comments

Comments
 (0)