Skip to content

Commit 816d03c

Browse files
committed
Add multiview rust shaders
1 parent 0d78b30 commit 816d03c

File tree

10 files changed

+149
-0
lines changed

10 files changed

+149
-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
@@ -88,6 +88,8 @@ members = [
8888
"debugutils/postprocess",
8989
"debugutils/toon",
9090
"vertexattributes/scene",
91+
"multiview/multiview",
92+
"multiview/viewdisplay",
9193
]
9294

9395
[workspace.dependencies]
2.16 KB
Binary file not shown.
4.39 KB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "multiview-multiview"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
11+
12+
[package.metadata.rust-gpu.build]
13+
capabilities = ["MultiView"]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec3, vec4, Mat3, Mat4, Vec3, Vec4}, num_traits::Float};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub projection: [Mat4; 2],
10+
pub modelview: [Mat4; 2],
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(view_index)] view_index: u32,
20+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
21+
#[spirv(position)] out_position: &mut Vec4,
22+
out_normal: &mut Vec3,
23+
out_color: &mut Vec3,
24+
out_view_vec: &mut Vec3,
25+
out_light_vec: &mut Vec3,
26+
) {
27+
*out_color = in_color;
28+
*out_normal = Mat3::from_mat4(ubo.modelview[view_index as usize]) * in_normal;
29+
30+
let pos = vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
31+
let world_pos = ubo.modelview[view_index as usize] * pos;
32+
33+
let l_pos = ubo.modelview[view_index as usize] * ubo.light_pos;
34+
*out_light_vec = vec3(l_pos.x, l_pos.y, l_pos.z) - vec3(world_pos.x, world_pos.y, world_pos.z);
35+
*out_view_vec = -vec3(world_pos.x, world_pos.y, world_pos.z);
36+
37+
*out_position = ubo.projection[view_index as usize] * world_pos;
38+
}
39+
40+
#[spirv(fragment)]
41+
pub fn main_fs(
42+
in_normal: Vec3,
43+
in_color: Vec3,
44+
in_view_vec: Vec3,
45+
in_light_vec: Vec3,
46+
out_color: &mut Vec4,
47+
) {
48+
let n = in_normal.normalize();
49+
let l = in_light_vec.normalize();
50+
let v = in_view_vec.normalize();
51+
let r = (-l).reflect(n);
52+
let ambient = vec3(0.1, 0.1, 0.1);
53+
let diffuse = n.dot(l).max(0.0) * vec3(1.0, 1.0, 1.0);
54+
let specular = r.dot(v).max(0.0).powf(16.0) * vec3(0.75, 0.75, 0.75);
55+
*out_color = vec4(
56+
(ambient.x + diffuse.x) * in_color.x + specular.x,
57+
(ambient.y + diffuse.y) * in_color.y + specular.y,
58+
(ambient.z + diffuse.z) * in_color.z + specular.z,
59+
1.0
60+
);
61+
}
2.12 KB
Binary file not shown.
736 Bytes
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 = "multiview-viewdisplay"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec2, vec3, vec4, Vec2, Vec4}, Image, image::SampledImage};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
_padding: [Vec4; 17], // offset = 272 bytes = 17 vec4s
10+
pub distortion_alpha: f32,
11+
}
12+
13+
14+
#[spirv(vertex)]
15+
pub fn main_vs(
16+
#[spirv(vertex_index)] vertex_index: i32,
17+
#[spirv(position)] out_position: &mut Vec4,
18+
out_uv: &mut Vec2,
19+
) {
20+
*out_uv = vec2(
21+
((vertex_index << 1) & 2) as f32,
22+
(vertex_index & 2) as f32,
23+
);
24+
let pos_xy = *out_uv * 2.0 - vec2(1.0, 1.0);
25+
*out_position = vec4(pos_xy.x, pos_xy.y, 0.0, 1.0);
26+
}
27+
28+
#[spirv(fragment)]
29+
pub fn main_fs(
30+
in_uv: Vec2,
31+
#[spirv(descriptor_set = 0, binding = 1)] sampler_view: &SampledImage<Image!(2D, type=f32, sampled, arrayed)>,
32+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
33+
#[spirv(spec_constant(id = 0, default = 0))] view_layer: u32,
34+
out_color: &mut Vec4,
35+
) {
36+
let alpha = ubo.distortion_alpha;
37+
38+
let p1 = 2.0 * in_uv - vec2(1.0, 1.0);
39+
let p2 = p1 / (1.0 - alpha * p1.length());
40+
let p2 = (p2 + vec2(1.0, 1.0)) * 0.5;
41+
42+
let inside = p2.x >= 0.0 && p2.x <= 1.0 && p2.y >= 0.0 && p2.y <= 1.0;
43+
*out_color = if inside {
44+
let layer = if view_layer == 0 { 0.0 } else { 1.0 };
45+
sampler_view.sample(vec3(p2.x, p2.y, layer))
46+
} else {
47+
vec4(0.0, 0.0, 0.0, 0.0)
48+
};
49+
}

0 commit comments

Comments
 (0)