Skip to content

Commit 381910c

Browse files
committed
Add shadowmappingomni rust shaders
1 parent 5644126 commit 381910c

File tree

14 files changed

+257
-0
lines changed

14 files changed

+257
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ members = [
99
"bloom/skybox",
1010
"computecloth/cloth",
1111
"computecloth/sphere",
12+
"computecullandlod/cull",
13+
"computecullandlod/indirectdraw",
1214
"computeheadless/headless",
1315
"computenbody/particle",
1416
"computenbody/particle_calculate",
@@ -91,6 +93,9 @@ members = [
9193
"shadowmapping/offscreen",
9294
"shadowmapping/quad",
9395
"shadowmapping/scene",
96+
"shadowmappingomni/cubemapdisplay",
97+
"shadowmappingomni/offscreen",
98+
"shadowmappingomni/scene",
9499
"specializationconstants/uber",
95100
"sphericalenvmapping/sem",
96101
"ssao/blur",
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 = "shadowmappingomni-cubemapdisplay"
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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#![no_std]
2+
3+
use spirv_std::spirv;
4+
use spirv_std::glam::{Vec2, Vec3, Vec4, Mat4, Vec4Swizzles};
5+
use spirv_std::num_traits::Float;
6+
7+
#[repr(C)]
8+
#[derive(Copy, Clone)]
9+
pub struct UBO {
10+
pub projection: Mat4,
11+
pub view: Mat4,
12+
pub model: Mat4,
13+
}
14+
15+
#[spirv(vertex)]
16+
pub fn main_vs(
17+
#[spirv(vertex_index)] vertex_index: i32,
18+
#[spirv(uniform, descriptor_set = 0, binding = 0)] _ubo: &UBO,
19+
#[spirv(position)] out_position: &mut Vec4,
20+
out_uv: &mut Vec2,
21+
) {
22+
let uv = Vec2::new(
23+
((vertex_index << 1) & 2) as f32,
24+
(vertex_index & 2) as f32,
25+
);
26+
*out_uv = uv;
27+
*out_position = Vec4::new(uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0);
28+
}
29+
30+
#[spirv(fragment)]
31+
pub fn main_fs(
32+
in_uv: Vec2,
33+
#[spirv(descriptor_set = 0, binding = 1)] shadow_cube_map: &spirv_std::Image!(cube, type=f32, sampled),
34+
#[spirv(descriptor_set = 0, binding = 1)] sampler: &spirv_std::Sampler,
35+
out_frag_color: &mut Vec4,
36+
) {
37+
*out_frag_color = Vec4::new(0.05, 0.05, 0.05, 1.0);
38+
39+
let mut sample_pos = Vec3::ZERO;
40+
41+
// Crude statement to visualize different cube map faces based on UV coordinates
42+
let x = (in_uv.x / 0.25).floor() as i32;
43+
let y = (in_uv.y / (1.0 / 3.0)).floor() as i32;
44+
45+
if y == 1 {
46+
let mut uv = Vec2::new(in_uv.x * 4.0, (in_uv.y - 1.0/3.0) * 3.0);
47+
uv = 2.0 * Vec2::new(uv.x - (x as f32) * 1.0, uv.y) - Vec2::ONE;
48+
match x {
49+
0 => { // NEGATIVE_X
50+
sample_pos = Vec3::new(-1.0, uv.y, uv.x);
51+
}
52+
1 => { // POSITIVE_Z
53+
sample_pos = Vec3::new(uv.x, uv.y, 1.0);
54+
}
55+
2 => { // POSITIVE_X
56+
sample_pos = Vec3::new(1.0, uv.y, -uv.x);
57+
}
58+
3 => { // NEGATIVE_Z
59+
sample_pos = Vec3::new(-uv.x, uv.y, -1.0);
60+
}
61+
_ => {}
62+
}
63+
} else {
64+
if x == 1 {
65+
let uv = 2.0 * Vec2::new((in_uv.x - 0.25) * 4.0, (in_uv.y - (y as f32) / 3.0) * 3.0) - Vec2::ONE;
66+
match y {
67+
0 => { // NEGATIVE_Y
68+
sample_pos = Vec3::new(uv.x, -1.0, uv.y);
69+
}
70+
2 => { // POSITIVE_Y
71+
sample_pos = Vec3::new(uv.x, 1.0, -uv.y);
72+
}
73+
_ => {}
74+
}
75+
}
76+
}
77+
78+
if sample_pos.x != 0.0 && sample_pos.y != 0.0 {
79+
let sampled: Vec4 = shadow_cube_map.sample_by_lod(*sampler, sample_pos, 0.0);
80+
let dist = sampled.xyz().length() * 0.005;
81+
*out_frag_color = Vec4::new(dist, dist, dist, 1.0);
82+
}
83+
}
708 Bytes
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 = "shadowmappingomni-offscreen"
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#![no_std]
2+
3+
use spirv_std::spirv;
4+
use spirv_std::glam::{Vec3, Vec4, Mat4, Vec4Swizzles};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct UBO {
9+
pub projection: Mat4,
10+
pub view: Mat4,
11+
pub model: Mat4,
12+
pub light_pos: Vec4,
13+
}
14+
15+
#[repr(C)]
16+
#[derive(Copy, Clone)]
17+
pub struct PushConsts {
18+
pub view: Mat4,
19+
}
20+
21+
#[spirv(vertex)]
22+
pub fn main_vs(
23+
#[spirv(vertex_index)] _vertex_index: i32,
24+
in_pos: Vec3,
25+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
26+
#[spirv(push_constant)] push_consts: &PushConsts,
27+
#[spirv(position)] out_position: &mut Vec4,
28+
out_pos: &mut Vec4,
29+
out_light_pos: &mut Vec3,
30+
) {
31+
*out_position = ubo.projection * push_consts.view * ubo.model * Vec4::new(in_pos.x, in_pos.y, in_pos.z, 1.0);
32+
*out_pos = Vec4::new(in_pos.x, in_pos.y, in_pos.z, 1.0);
33+
*out_light_pos = ubo.light_pos.xyz();
34+
}
35+
36+
#[spirv(fragment)]
37+
pub fn main_fs(
38+
in_pos: Vec4,
39+
in_light_pos: Vec3,
40+
out_frag_color: &mut f32,
41+
) {
42+
// Store distance to light as 32 bit float value
43+
let light_vec = in_pos.xyz() - in_light_pos;
44+
*out_frag_color = light_vec.length();
45+
}

0 commit comments

Comments
 (0)