Skip to content

Commit 71e6e0d

Browse files
committed
Add shadowmappingcascade rust shaders
1 parent 49fe96e commit 71e6e0d

File tree

14 files changed

+335
-0
lines changed

14 files changed

+335
-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
@@ -136,6 +136,9 @@ members = [
136136
"vulkanscene/logo",
137137
"vulkanscene/mesh",
138138
"vulkanscene/skybox",
139+
"shadowmappingcascade/depthpass",
140+
"shadowmappingcascade/debugshadowmap",
141+
"shadowmappingcascade/scene",
139142
]
140143

141144
[workspace.package]
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 = "shadowmappingcascade-debugshadowmap"
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![no_std]
2+
3+
use spirv_std::glam::{Vec2, Vec3, Vec4};
4+
use spirv_std::spirv;
5+
use spirv_std::{Image, Sampler};
6+
7+
#[repr(C)]
8+
#[derive(Copy, Clone)]
9+
pub struct PushConsts {
10+
pub position: Vec4,
11+
pub cascade_index: u32,
12+
}
13+
14+
#[spirv(vertex)]
15+
pub fn main_vs(
16+
#[spirv(vertex_index)] vertex_index: u32,
17+
#[spirv(push_constant)] push_consts: &PushConsts,
18+
#[spirv(position)] out_position: &mut Vec4,
19+
out_uv: &mut Vec2,
20+
#[spirv(flat)] out_cascade_index: &mut u32,
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_cascade_index = push_consts.cascade_index;
28+
*out_position = Vec4::new(uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0);
29+
}
30+
31+
#[spirv(fragment)]
32+
pub fn main_fs(
33+
in_uv: Vec2,
34+
#[spirv(flat)] in_cascade_index: u32,
35+
#[spirv(descriptor_set = 0, binding = 1)] shadow_map: &Image!(2D, type=f32, sampled, arrayed),
36+
#[spirv(descriptor_set = 0, binding = 1)] sampler: &Sampler,
37+
out_frag_color: &mut Vec4,
38+
) {
39+
let depth = shadow_map.sample(*sampler, Vec3::new(in_uv.x, in_uv.y, in_cascade_index as f32)).x;
40+
*out_frag_color = Vec4::new(depth, depth, depth, 1.0);
41+
}
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 = "shadowmappingcascade-depthpass"
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+
#![no_std]
2+
3+
use spirv_std::glam::{Mat4, Vec2, Vec3, Vec4, Vec4Swizzles};
4+
use spirv_std::spirv;
5+
use spirv_std::{Image, Sampler};
6+
7+
const SHADOW_MAP_CASCADE_COUNT: usize = 4;
8+
9+
#[repr(C)]
10+
#[derive(Copy, Clone)]
11+
pub struct PushConsts {
12+
pub position: Vec4,
13+
pub cascade_index: u32,
14+
}
15+
16+
#[repr(C)]
17+
#[derive(Copy, Clone)]
18+
pub struct UBO {
19+
pub cascade_view_proj_mat: [Mat4; SHADOW_MAP_CASCADE_COUNT],
20+
}
21+
22+
#[spirv(vertex)]
23+
pub fn main_vs(
24+
in_pos: Vec3,
25+
in_uv: Vec2,
26+
#[spirv(push_constant)] push_consts: &PushConsts,
27+
#[spirv(uniform, descriptor_set = 0, binding = 3)] ubo: &UBO,
28+
#[spirv(position)] out_position: &mut Vec4,
29+
out_uv: &mut Vec2,
30+
) {
31+
*out_uv = in_uv;
32+
let pos = in_pos + push_consts.position.xyz();
33+
*out_position = ubo.cascade_view_proj_mat[push_consts.cascade_index as usize] * Vec4::from((pos, 1.0));
34+
}
35+
36+
#[spirv(fragment)]
37+
pub fn main_fs(
38+
in_uv: Vec2,
39+
#[spirv(descriptor_set = 1, binding = 0)] color_map: &Image!(2D, type=f32, sampled),
40+
#[spirv(descriptor_set = 1, binding = 0)] sampler: &Sampler,
41+
) {
42+
let alpha = color_map.sample(*sampler, in_uv).w;
43+
if alpha < 0.5 {
44+
spirv_std::arch::kill();
45+
}
46+
}

0 commit comments

Comments
 (0)