Skip to content

Commit 6498a08

Browse files
committed
Add vulkanscene rust shaders
1 parent ade52cf commit 6498a08

File tree

14 files changed

+229
-0
lines changed

14 files changed

+229
-0
lines changed

shaders/rust/Cargo.lock

Lines changed: 21 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ members = [
9494
"imgui/scene",
9595
"texturemipmapgen/texture",
9696
"pbrbasic/pbr",
97+
"vulkanscene/skybox",
98+
"vulkanscene/logo",
99+
"vulkanscene/mesh",
97100
]
98101

99102
[workspace.dependencies]
948 Bytes
Binary file not shown.
7.78 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 = "vulkanscene-logo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
spirv-std = { workspace = true }
8+
9+
[lib]
10+
crate-type = ["dylib"]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec4, Mat3, Mat4, Vec2, Vec3, Vec4, Vec4Swizzles}, num_traits::Float};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub projection: Mat4,
10+
pub model: Mat4,
11+
pub normal: Mat4,
12+
pub view: Mat4,
13+
pub lightpos: Vec3,
14+
}
15+
16+
#[spirv(vertex)]
17+
pub fn main_vs(
18+
in_pos: Vec4,
19+
in_normal: Vec3,
20+
in_tex_coord: Vec2,
21+
in_color: Vec3,
22+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
23+
#[spirv(position)] out_position: &mut Vec4,
24+
out_uv: &mut Vec2,
25+
out_normal: &mut Vec3,
26+
out_color: &mut Vec3,
27+
out_eye_pos: &mut Vec3,
28+
out_light_vec: &mut Vec3,
29+
) {
30+
let model_view = ubo.view * ubo.model;
31+
let pos = model_view * in_pos;
32+
*out_uv = in_tex_coord;
33+
*out_normal = (Mat3::from_mat4(ubo.normal) * in_normal).normalize();
34+
*out_color = in_color;
35+
*out_position = ubo.projection * pos;
36+
*out_eye_pos = (model_view * pos).xyz();
37+
let light_pos = model_view * vec4(1.0, 2.0, 0.0, 1.0);
38+
*out_light_vec = (light_pos.xyz() - *out_eye_pos).normalize();
39+
}
40+
41+
#[spirv(fragment)]
42+
pub fn main_fs(
43+
_in_uv: Vec2,
44+
in_normal: Vec3,
45+
in_color: Vec3,
46+
in_eye_pos: Vec3,
47+
in_light_vec: Vec3,
48+
out_frag_color: &mut Vec4,
49+
) {
50+
let eye = (-in_eye_pos).normalize();
51+
let reflected = (-in_light_vec).reflect(in_normal).normalize();
52+
53+
let diff = vec4(in_color.x, in_color.y, in_color.z, 1.0) * in_normal.dot(in_light_vec).max(0.0);
54+
let shininess = 0.0;
55+
let spec = vec4(1.0, 1.0, 1.0, 1.0) * reflected.dot(eye).max(0.0).powf(2.5) * shininess;
56+
57+
*out_frag_color = diff + spec;
58+
out_frag_color.w = 1.0;
59+
}
3.55 KB
Binary file not shown.
8.1 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 = "vulkanscene-mesh"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
spirv-std = { workspace = true }
8+
9+
[lib]
10+
crate-type = ["dylib"]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec4, Mat3, Mat4, Vec2, Vec3, Vec4, Vec4Swizzles}, Image, image::SampledImage, num_traits::Float};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub projection: Mat4,
10+
pub model: Mat4,
11+
pub normal: Mat4,
12+
pub view: Mat4,
13+
pub lightpos: Vec3,
14+
}
15+
16+
#[spirv(vertex)]
17+
pub fn main_vs(
18+
in_pos: Vec4,
19+
in_normal: Vec3,
20+
in_tex_coord: Vec2,
21+
in_color: Vec3,
22+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
23+
#[spirv(position)] out_position: &mut Vec4,
24+
out_uv: &mut Vec2,
25+
out_normal: &mut Vec3,
26+
out_color: &mut Vec3,
27+
out_eye_pos: &mut Vec3,
28+
out_light_vec: &mut Vec3,
29+
) {
30+
*out_uv = in_tex_coord;
31+
*out_normal = (Mat3::from_mat4(ubo.normal) * in_normal).normalize();
32+
*out_color = in_color;
33+
let model_view = ubo.view * ubo.model;
34+
let pos = model_view * in_pos;
35+
*out_position = ubo.projection * pos;
36+
*out_eye_pos = (model_view * pos).xyz();
37+
let light_pos = model_view * vec4(ubo.lightpos.x, ubo.lightpos.y, ubo.lightpos.z, 1.0);
38+
*out_light_vec = (light_pos.xyz() - *out_eye_pos).normalize();
39+
}
40+
41+
fn specpart(l: Vec3, n: Vec3, h: Vec3) -> f32 {
42+
if n.dot(l) > 0.0 {
43+
h.dot(n).clamp(0.0, 1.0).powf(64.0)
44+
} else {
45+
0.0
46+
}
47+
}
48+
49+
#[spirv(fragment)]
50+
pub fn main_fs(
51+
_in_uv: Vec2,
52+
in_normal: Vec3,
53+
in_color: Vec3,
54+
in_eye_pos: Vec3,
55+
in_light_vec: Vec3,
56+
#[spirv(descriptor_set = 0, binding = 1)] _tex: &SampledImage<Image!(2D, type=f32, sampled)>,
57+
out_frag_color: &mut Vec4,
58+
) {
59+
let eye = (-in_eye_pos).normalize();
60+
let reflected = (-in_light_vec).reflect(in_normal).normalize();
61+
62+
let half_vec = (in_light_vec + in_eye_pos).normalize();
63+
let diff = in_light_vec.dot(in_normal).clamp(0.0, 1.0);
64+
let spec = specpart(in_light_vec, in_normal, half_vec);
65+
let intensity = 0.1 + diff + spec;
66+
67+
let i_ambient = vec4(0.2, 0.2, 0.2, 1.0);
68+
let i_diffuse = vec4(0.5, 0.5, 0.5, 0.5) * in_normal.dot(in_light_vec).max(0.0);
69+
let shininess = 0.75;
70+
let i_specular = vec4(0.5, 0.5, 0.5, 1.0) * reflected.dot(eye).max(0.0).powf(2.0) * shininess;
71+
72+
*out_frag_color = (i_ambient + i_diffuse) * vec4(in_color.x, in_color.y, in_color.z, 1.0) + i_specular;
73+
74+
// Some manual saturation
75+
if intensity > 0.95 {
76+
*out_frag_color *= 2.25;
77+
}
78+
if intensity < 0.15 {
79+
*out_frag_color = vec4(0.1, 0.1, 0.1, 0.1);
80+
}
81+
}

0 commit comments

Comments
 (0)