Skip to content

Use spirv2 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions shaders/src/a_lot_of_spheres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ const SPEED: f32 = 0.5;

const _MR: Mat2 = mat2(vec2(0.84147, 0.54030), vec2(0.54030, -0.84147));
fn hash(n: f32) -> f32 {
(n.sin() * 43758.5453).gl_fract()
(n.sin() * 43758.5453).fract_gl()
}
fn _hash2(n: f32) -> Vec2 {
(vec2(n, n + 1.0).sin() * vec2(2.1459123, 3.3490423)).gl_fract()
(vec2(n, n + 1.0).sin() * vec2(2.1459123, 3.3490423)).fract_gl()
}
fn hash2_vec(n: Vec2) -> Vec2 {
(vec2(n.x * n.y, n.x + n.y).sin() * vec2(2.1459123, 3.3490423)).gl_fract()
(vec2(n.x * n.y, n.x + n.y).sin() * vec2(2.1459123, 3.3490423)).fract_gl()
}
fn _hash3(n: f32) -> Vec3 {
(vec3(n, n + 1.0, n + 2.0).sin() * vec3(3.5453123, 4.1459123, 1.3490423)).gl_fract()
(vec3(n, n + 1.0, n + 2.0).sin() * vec3(3.5453123, 4.1459123, 1.3490423)).fract_gl()
}
fn hash3_vec(n: Vec2) -> Vec3 {
(vec3(n.x, n.y, n.x + 2.0).sin() * vec3(3.5453123, 4.1459123, 1.3490423)).gl_fract()
(vec3(n.x, n.y, n.x + 2.0).sin() * vec3(3.5453123, 4.1459123, 1.3490423)).fract_gl()
}

//
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Inputs {
fn get_moving_sphere_position(&self, grid: Vec2, sphere_offset: Vec2, center: &mut Vec3) {
// falling?
let s: f32 = 0.1 + hash(grid.x * 1.23114 + 5.342 + 74.324231 * grid.y);
let t: f32 = (14. * s + self.time / s * 0.3).gl_fract();
let t: f32 = (14. * s + self.time / s * 0.3).fract_gl();

let y: f32 = s * MAXHEIGHT * (4.0 * t * (1. - t)).abs();
let offset: Vec2 = grid + sphere_offset;
Expand Down Expand Up @@ -150,12 +150,11 @@ impl Inputs {
// trace grid
let mut pos: Vec3 = (ro / GRIDSIZE).floor() * GRIDSIZE;
let ri: Vec3 = 1.0 / rd;
let rs: Vec3 = rd.gl_sign() * GRIDSIZE;
let rs: Vec3 = rd.sign_gl() * GRIDSIZE;
let mut dis: Vec3 = (pos - ro + 0.5 * Vec3::splat(GRIDSIZE) + rs * 0.5) * ri;
let mut mm: Vec3;

let mut i = 0;
while i < RAYCASTSTEPS {
for _ in 0..RAYCASTSTEPS {
if *material > 1 || ro.xz().distance(pos.xz()) > *dist + GRIDSIZE {
break;
}
Expand Down Expand Up @@ -184,7 +183,6 @@ impl Inputs {
mm = dis.step(dis.zyx());
dis += mm * rs * ri;
pos += mm * rs;
i += 1;
}

let mut color: Vec3 = Vec3::ZERO;
Expand All @@ -195,10 +193,8 @@ impl Inputs {
if *material == 1 || *material == 3 {
// lightning
let c: Vec3 = vec3(-GRIDSIZE, 0.0, GRIDSIZE);
let mut x = 0;
while x < 3 {
let mut y = 0;
while y < 3 {
for x in 0..3 {
for y in 0..3 {
let mapoffset: Vec2 = map + vec2([c.x, c.y, c.z][x], [c.x, c.y, c.z][y]);
let mut offset: Vec2 = Vec2::ZERO;
get_sphere_offset(mapoffset, &mut offset);
Expand All @@ -209,12 +205,9 @@ impl Inputs {
let mut shadow: f32 = 1.0;

if SHADOW && *material == 1 {
let mut sx = 0;
while sx < 3 {
let mut sy = 0;
while sy < 3 {
for _ in 0..3 {
for _ in 0..3 {
if shadow < 1.0 {
sy += 1;
continue;
}

Expand All @@ -235,19 +228,15 @@ impl Inputs {
) {
shadow = 0.0;
}
sy += 1;
}
sx += 1;
}
}
color += col
* lcolor
* (shadow
* ((lpos - *intersection).normalize().dot(*normal)).max(0.0)
* (1. - (lpos.distance(*intersection) / GRIDSIZE).clamp(0.0, 1.)));
y += 1;
}
x += 1;
}
} else {
// emitter
Expand Down
22 changes: 8 additions & 14 deletions shaders/src/a_question_of_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn inversion(uv: Vec2, r: f32) -> Vec2 {
}
// seeded random number
fn hash(s: Vec2) -> f32 {
((s.dot(vec2(12.9898, 78.2333))).sin() * 43758.5453123).gl_fract()
((s.dot(vec2(12.9898, 78.2333))).sin() * 43758.5453123).fract_gl()
}

// this is an algorithm to construct an apollonian packing with a descartes configuration
Expand All @@ -93,35 +93,30 @@ fn apollonian(uv: Vec2) -> Vec3 {
dec[0] = vec3(0.0, 0.0, -1.0 / ra);
let radius: f32 = 0.5 * (ra - rb);
let bend: f32 = 1.0 / radius;
let mut i = 1;
while i < 4 {
for i in 1..4 {
dec[i] = vec3((i as f32 * a).cos(), (i as f32 * a).sin(), bend);
// if the point is in one of the starting circles we have already found our solution
if (uv - dec[i].xy()).length() < radius {
return (uv - dec[i].xy()).extend(radius);
}
i += 1;
}

// Now that we have a starting DEC we are going to try to
// find the solution for the current point
let mut i = 0;
while i < 7 {
for _ in 0..7 {
// find the circle that is further away from the point uv, using euclidean distance
let mut fi: usize = 0;
let mut d: f32 = uv.distance(dec[0].xy()) - (1.0 / dec[0].z).abs();
// for some reason, the euclidean distance doesn't work for the circle with negative bend
// can anyone with proper math skills, explain me why?
d *= if dec[0].z < 0.0 { -0.5 } else { 1.0 }; // just scale it to make it work...
let mut j = 1;
while j < 4 {
for j in 1..4 {
let mut fd: f32 = uv.distance(dec[j].xy()) - (1. / dec[j].z).abs();
fd *= if dec[j].z < 0.0 { -0.5 } else { 1.0 };
if fd > d {
fi = j;
d = fd;
}
j += 1;
}
// put the cicle found in the last slot, to generate a solution
// in the "direction" of the point
Expand All @@ -147,7 +142,6 @@ fn apollonian(uv: Vec2) -> Vec3 {
// else update the descartes configuration,
dec[3] = solution;
// and repeat...
i += 1;
}
// if nothing is found we return by default the inner circle of the Steiner chain
uv.extend(rb)
Expand All @@ -159,7 +153,7 @@ impl Inputs {

// drag your mouse to apply circle inversion
if ms.y != -2.0 && ms.w > -2.0 {
uv = inversion(uv, (60.0.deg_to_radians()).cos());
uv = inversion(uv, 60.0.to_radians().cos());
ci = ms.xy();
}

Expand Down Expand Up @@ -209,7 +203,7 @@ impl Inputs {

// needles rotation
let uvrh: Vec2 = uvr_rotate(
(hash(Vec2::splat(uv_apo.z)) * d * 180.0).cos().gl_sign()
(hash(Vec2::splat(uv_apo.z)) * d * 180.0).cos().sign_gl()
* d
* self.time
* (1.0 / uv_apo.z * 10.0)
Expand All @@ -220,7 +214,7 @@ impl Inputs {
let uvrm: Vec2 = uvr_rotate(
(hash(Vec2::splat(uv_apo.z) * 4.0) * d * 180.0)
.cos()
.gl_sign()
.sign_gl()
* d
* self.time
* (1.0 / uv_apo.z * 120.0)
Expand Down Expand Up @@ -264,7 +258,7 @@ impl Inputs {
let uvrg: Vec2 = uvr_rotate(
(hash(Vec2::splat(uv_apo.z + 2.0)) * d * 180.0)
.cos()
.gl_sign()
.sign_gl()
* d
* self.time
* (1.0 / uv_apo.z * 20.0),
Expand Down
18 changes: 5 additions & 13 deletions shaders/src/apollonian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ impl State {
fn map(&mut self, mut p: Vec3, s: f32) -> f32 {
let mut scale: f32 = 1.0;
self.orb = Vec4::splat(1000.0);
let mut i = 0;
while i < 8 {
p = Vec3::splat(-1.0) + 2.0 * (0.5 * p + Vec3::splat(0.5)).gl_fract();
for _ in 0..8 {
p = Vec3::splat(-1.0) + 2.0 * (0.5 * p + Vec3::splat(0.5)).fract_gl();

let r2: f32 = p.dot(p);

Expand All @@ -62,7 +61,6 @@ impl State {
let k: f32 = s / r2;
p *= k;
scale *= k;
i += 1;
}
0.25 * p.y.abs() / scale
}
Expand All @@ -71,16 +69,14 @@ impl State {
let maxd = 30.0;
let mut t: f32 = 0.01;

let mut i = 0;
while i < 512 {
for _ in 0..512 {
let precis = 0.001 * t;

let h: f32 = self.map(ro + rd * t, s);
if h < precis || t > maxd {
break;
}
t += h;
i += 1;
}
if t > maxd {
t = -1.0;
Expand Down Expand Up @@ -141,10 +137,8 @@ impl State {
let anim: f32 = 1.1 + 0.5 * smoothstep(-0.3, 0.3, (0.1 * self.inputs.time).cos());
let mut tot: Vec3 = Vec3::ZERO;

let mut jj = 0;
while jj < AA {
let mut ii = 0;
while ii < AA {
for jj in 0..AA {
for ii in 0..AA {
let q: Vec2 = frag_coord + vec2(ii as f32, jj as f32) / AA as f32;
let p: Vec2 = (2.0 * q - self.inputs.resolution.xy()) / self.inputs.resolution.y;

Expand All @@ -167,9 +161,7 @@ impl State {
let rd: Vec3 = (p.x * cu + p.y * cv + 2.0 * cw).normalize();

tot += self.render(ro, rd, anim);
ii += 1;
}
jj += 1;
}

tot = tot / (AA * AA) as f32;
Expand Down
15 changes: 5 additions & 10 deletions shaders/src/atmosphere_system_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! // ----------------------------------------------------------------------------
//! ```

use shared::*;
use spirv_std::glam::{vec2, vec3, Mat3, Vec2, Vec3, Vec3Swizzles, Vec4};

// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
Expand Down Expand Up @@ -60,7 +59,7 @@ struct _Plane {
}

fn rotate_around_x(angle_degrees: f32) -> Mat3 {
let angle: f32 = angle_degrees.deg_to_radians();
let angle: f32 = angle_degrees.to_radians();
let _sin: f32 = angle.sin();
let _cos: f32 = angle.cos();
Mat3::from_cols_array(&[1.0, 0.0, 0.0, 0.0, _cos, -_sin, 0.0, _sin, _cos])
Expand Down Expand Up @@ -149,8 +148,7 @@ fn get_sun_light(ray: Ray, optical_depth_r: &mut f32, optical_depth_m: &mut f32)
let mut march_pos: f32 = 0.0;
let march_step: f32 = t1 / NUM_SAMPLES_LIGHT as f32;

let mut i = 0;
while i < NUM_SAMPLES_LIGHT {
for _ in 0..NUM_SAMPLES_LIGHT {
let s: Vec3 = ray.origin + ray.direction * (march_pos + 0.5 * march_step);
let height: f32 = s.length() - EARTH_RADIUS;
if height < 0.0 {
Expand All @@ -161,7 +159,6 @@ fn get_sun_light(ray: Ray, optical_depth_r: &mut f32, optical_depth_m: &mut f32)
*optical_depth_m += (-height / H_M).exp() * march_step;

march_pos += march_step;
i += 1;
}
true
}
Expand Down Expand Up @@ -203,8 +200,7 @@ impl State {
let mut sum_m: Vec3 = Vec3::ZERO;
let mut march_pos: f32 = 0.0;

let mut i = 0;
while i < NUM_SAMPLES {
for _ in 0..NUM_SAMPLES {
let s: Vec3 = ray.origin + ray.direction * (march_pos + 0.5 * march_step);
let height: f32 = s.length() - EARTH_RADIUS;

Expand All @@ -230,22 +226,21 @@ impl State {
if overground {
let tau: Vec3 = BETA_R * (optical_depth_r + optical_depth_light_r)
+ BETA_M * 1.1 * (optical_depth_m + optical_depth_light_m);
let attenuation: Vec3 = exp(-tau);
let attenuation: Vec3 = Vec3::exp(-tau);

sum_r += hr * attenuation;
sum_m += hm * attenuation;
}

march_pos += march_step;
i += 1;
}

SUN_POWER * (sum_r * phase_r * BETA_R + sum_m * phase_m * BETA_M)
}

pub fn main_image(&mut self, frag_color: &mut Vec4, frag_coord: Vec2) {
let aspect_ratio: Vec2 = vec2(self.inputs.resolution.x / self.inputs.resolution.y, 1.0);
let fov: f32 = 45.0.deg_to_radians().tan();
let fov: f32 = 45.0.to_radians().tan();
let point_ndc: Vec2 = frag_coord / self.inputs.resolution.xy();
let point_cam: Vec3 = ((2.0 * point_ndc - Vec2::ONE) * aspect_ratio * fov).extend(-1.0);

Expand Down
12 changes: 3 additions & 9 deletions shaders/src/bubble_buckey_balls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ impl<C0, C1> State<C0, C1> {
let mut dist: f32 = 10.0 * epsilon;
let mut t: f32 = 0.0;
let mut material: f32 = 0.0;
let mut i = 0;
while i < DISTMARCH_STEPS {
for _ in 0..DISTMARCH_STEPS {
if dist.abs() < epsilon || t > maxd {
break;
}
Expand All @@ -253,7 +252,6 @@ impl<C0, C1> State<C0, C1> {
let dfresult: Vec2 = self.scenedf(ro + t * rd);
dist = dfresult.x;
material = dfresult.y;
i += 1;
}

if t > maxd {
Expand All @@ -274,14 +272,12 @@ impl<C0, C1> State<C0, C1> {
let mut shadow: f32 = 1.0;
let mut t: f32 = mint;

let mut i = 0;
while i < SOFTSHADOW_STEPS {
for _ in 0..SOFTSHADOW_STEPS {
if t < maxt {
let h: f32 = self.scenedf(ro + rd * t).x;
shadow = shadow.min(k * h / t);
t += SOFTSHADOW_STEPSIZE;
}
i += 1;
}
shadow.clamp(0.0, 1.0)
}
Expand All @@ -296,15 +292,13 @@ impl<C0, C1> State<C0, C1> {
let mut ao: f32 = 0.0;
let mut aoscale: f32 = 1.0;

let mut aoi = 0;
while aoi < AO_NUMSAMPLES {
for aoi in 0..AO_NUMSAMPLES {
let step: f32 = 0.01 + AO_STEPSIZE * aoi as f32;
let aop: Vec3 = n * step + p;

let d: f32 = self.scenedf(aop).x;
ao += -(d - step) * aoscale;
aoscale *= AO_STEPSCALE;
aoi += 1;
}

ao.clamp(0.0, 1.0)
Expand Down
Loading