Skip to content

Commit 62f2ade

Browse files
committed
Add graphicspipelinelibrary rust shaders
1 parent 87b4e69 commit 62f2ade

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-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
@@ -36,6 +36,8 @@ members = [
3636
"gltfloading/mesh",
3737
"gltfscenerendering/scene",
3838
"gltfskinning/skinnedmodel",
39+
"graphicspipelinelibrary/shared",
40+
"graphicspipelinelibrary/uber",
3941
"hdr/bloom",
4042
"hdr/composition",
4143
"hdr/gbuffer",
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 = "graphicspipelinelibrary-shared"
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{mat3, vec4, Mat4, Vec3, Vec4}};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub projection: Mat4,
10+
pub model: Mat4,
11+
pub light_pos: Vec4,
12+
}
13+
14+
#[spirv(vertex)]
15+
pub fn main_vs(
16+
in_pos: Vec3,
17+
in_normal: Vec3,
18+
in_color: Vec3,
19+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
20+
#[spirv(position)] out_position: &mut Vec4,
21+
out_normal: &mut Vec3,
22+
out_color: &mut Vec3,
23+
out_view_vec: &mut Vec3,
24+
out_light_vec: &mut Vec3,
25+
#[spirv(flat)] out_flat_normal: &mut Vec3,
26+
) {
27+
*out_normal = in_normal;
28+
*out_color = in_color;
29+
let pos = vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
30+
31+
*out_position = ubo.projection * ubo.model * pos;
32+
33+
let world_pos = ubo.model * pos;
34+
let model_mat3 = mat3(
35+
ubo.model.x_axis.truncate(),
36+
ubo.model.y_axis.truncate(),
37+
ubo.model.z_axis.truncate(),
38+
);
39+
*out_normal = model_mat3 * in_normal;
40+
let l_pos = ubo.light_pos.truncate();
41+
*out_light_vec = l_pos - world_pos.truncate();
42+
*out_view_vec = -world_pos.truncate();
43+
44+
// Flat shading normal is not interpolated
45+
*out_flat_normal = *out_normal;
46+
}
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 = "graphicspipelinelibrary-uber"
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec3, vec4, Vec3, Vec4}, num_traits::Float};
5+
6+
#[spirv(fragment)]
7+
pub fn main_fs(
8+
in_normal: Vec3,
9+
in_color: Vec3,
10+
in_view_vec: Vec3,
11+
in_light_vec: Vec3,
12+
#[spirv(flat)] _in_flat_normal: Vec3,
13+
#[spirv(spec_constant(id = 0, default = 0))] lighting_model: u32,
14+
out_frag_color: &mut Vec4,
15+
) {
16+
let mut color = match lighting_model {
17+
0 => { // Phong
18+
let ambient = in_color * vec3(0.25, 0.25, 0.25);
19+
let n = in_normal.normalize();
20+
let l = in_light_vec.normalize();
21+
let v = in_view_vec.normalize();
22+
let r = (-l).reflect(n);
23+
let diffuse = n.dot(l).max(0.0) * in_color;
24+
let specular = r.dot(v).max(0.0).powf(32.0) * vec3(0.75, 0.75, 0.75);
25+
ambient + diffuse * 1.75 + specular
26+
}
27+
1 => { // Toon
28+
let n = in_normal.normalize();
29+
let l = in_light_vec.normalize();
30+
let intensity = n.dot(l);
31+
if intensity > 0.98 {
32+
in_color * 1.5
33+
} else if intensity > 0.9 {
34+
in_color * 1.0
35+
} else if intensity > 0.5 {
36+
in_color * 0.6
37+
} else if intensity > 0.25 {
38+
in_color * 0.4
39+
} else {
40+
in_color * 0.2
41+
}
42+
}
43+
2 => { // No shading
44+
in_color
45+
}
46+
3 => { // Greyscale
47+
let grey = in_color.x * 0.299 + in_color.y * 0.587 + in_color.z * 0.114;
48+
vec3(grey, grey, grey)
49+
}
50+
_ => in_color, // Default case
51+
};
52+
53+
// Scene is dark, brighten up a bit
54+
color *= 1.25;
55+
56+
*out_frag_color = vec4(color.x, color.y, color.z, 1.0);
57+
}

0 commit comments

Comments
 (0)