Skip to content

Commit 0d5179e

Browse files
committed
Add multithreading rust shaders
1 parent 62f2ade commit 0d5179e

File tree

10 files changed

+159
-0
lines changed

10 files changed

+159
-0
lines changed

shaders/rust/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shaders/rust/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ members = [
5151
"instancing/planet",
5252
"instancing/starfield",
5353
"multisampling/mesh",
54+
"multithreading/phong",
55+
"multithreading/starsphere",
5456
"multiview/multiview",
5557
"multiview/viewdisplay",
5658
"negativeviewportheight/quad",
2.11 KB
Binary file not shown.
3.26 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "multithreading-phong"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{mat3, vec3, vec4, Mat4, Vec3, Vec4}, num_traits::Float};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct PushConsts {
9+
pub mvp: Mat4,
10+
pub color: Vec3,
11+
}
12+
13+
#[spirv(vertex)]
14+
pub fn main_vs(
15+
in_pos: Vec3,
16+
in_normal: Vec3,
17+
in_color: Vec3,
18+
#[spirv(push_constant)] push_consts: &PushConsts,
19+
#[spirv(position)] out_position: &mut Vec4,
20+
out_normal: &mut Vec3,
21+
out_color: &mut Vec3,
22+
out_view_vec: &mut Vec3,
23+
out_light_vec: &mut Vec3,
24+
) {
25+
*out_normal = in_normal;
26+
27+
// If color is red (1.0, 0.0, 0.0), use push constant color
28+
if in_color.x == 1.0 && in_color.y == 0.0 && in_color.z == 0.0 {
29+
*out_color = push_consts.color;
30+
} else {
31+
*out_color = in_color;
32+
}
33+
34+
*out_position = push_consts.mvp * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
35+
36+
let pos = push_consts.mvp * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
37+
let mvp_mat3 = mat3(
38+
push_consts.mvp.x_axis.truncate(),
39+
push_consts.mvp.y_axis.truncate(),
40+
push_consts.mvp.z_axis.truncate(),
41+
);
42+
*out_normal = mvp_mat3 * in_normal;
43+
let l_pos = vec3(0.0, 0.0, 0.0);
44+
*out_light_vec = l_pos - pos.truncate();
45+
*out_view_vec = -pos.truncate();
46+
}
47+
48+
#[spirv(fragment)]
49+
pub fn main_fs(
50+
in_normal: Vec3,
51+
in_color: Vec3,
52+
in_view_vec: Vec3,
53+
in_light_vec: Vec3,
54+
out_frag_color: &mut Vec4,
55+
) {
56+
let n = in_normal.normalize();
57+
let l = in_light_vec.normalize();
58+
let v = in_view_vec.normalize();
59+
let r = (-l).reflect(n);
60+
let diffuse = n.dot(l).max(0.0) * in_color;
61+
let specular = r.dot(v).max(0.0).powf(8.0) * vec3(0.75, 0.75, 0.75);
62+
*out_frag_color = vec4(diffuse.x + specular.x, diffuse.y + specular.y, diffuse.z + specular.z, 1.0);
63+
}
1.91 KB
Binary file not shown.
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "multithreading-starsphere"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec3, vec4, Mat4, Vec3, Vec4}, num_traits::Float};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct PushConsts {
9+
pub mvp: Mat4,
10+
}
11+
12+
#[spirv(vertex)]
13+
pub fn main_vs(
14+
in_pos: Vec3,
15+
#[spirv(push_constant)] push_consts: &PushConsts,
16+
#[spirv(position)] out_position: &mut Vec4,
17+
out_uvw: &mut Vec3,
18+
) {
19+
*out_uvw = in_pos;
20+
*out_position = push_consts.mvp * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
21+
}
22+
23+
const HASHSCALE3: Vec3 = vec3(443.897, 441.423, 437.195);
24+
const STARFREQUENCY: f32 = 0.01;
25+
26+
// Hash function by Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
27+
fn hash33(p3_in: Vec3) -> f32 {
28+
let mut p3 = p3_in.fract() * HASHSCALE3;
29+
p3 += p3.dot(vec3(p3.y, p3.x, p3.z) + vec3(19.19, 19.19, 19.19));
30+
((p3.x + p3.y) * p3.z + (p3.x + p3.z) * p3.y + (p3.y + p3.z) * p3.x).fract()
31+
}
32+
33+
fn star_field(pos: Vec3) -> Vec3 {
34+
let mut color = vec3(0.0, 0.0, 0.0);
35+
let threshold = 1.0 - STARFREQUENCY;
36+
let rnd = hash33(pos);
37+
if rnd >= threshold {
38+
let star_col = ((rnd - threshold) / (1.0 - threshold)).powf(16.0);
39+
color += vec3(star_col, star_col, star_col);
40+
}
41+
color
42+
}
43+
44+
#[spirv(fragment)]
45+
pub fn main_fs(
46+
in_uvw: Vec3,
47+
out_frag_color: &mut Vec4,
48+
) {
49+
// Fake atmosphere at the bottom
50+
let atmosphere = vec3(0.1, 0.15, 0.4) * (in_uvw.y + 0.25);
51+
let atmosphere = vec3(
52+
atmosphere.x.clamp(0.0, 1.0),
53+
atmosphere.y.clamp(0.0, 1.0),
54+
atmosphere.z.clamp(0.0, 1.0)
55+
);
56+
57+
let color = star_field(in_uvw) + atmosphere;
58+
59+
*out_frag_color = vec4(color.x, color.y, color.z, 1.0);
60+
}

0 commit comments

Comments
 (0)