Skip to content

Commit 6af30af

Browse files
committed
Add offscreen rust shaders
1 parent 3d889c4 commit 6af30af

14 files changed

Lines changed: 204 additions & 0 deletions

File tree

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
@@ -81,6 +81,9 @@ members = [
8181
"textoverlay/text",
8282
"textoverlay/mesh",
8383
"shaderobjects/phong",
84+
"offscreen/quad",
85+
"offscreen/mirror",
86+
"offscreen/phong",
8487
]
8588

8689
[workspace.dependencies]
4.47 KB
Binary file not shown.
6.93 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 = "offscreen-mirror"
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::{vec2, vec4, Mat4, Vec3, Vec4}, Image};
5+
use spirv_std::image::SampledImage;
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+
in_pos: Vec3,
18+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
19+
#[spirv(position)] out_position: &mut Vec4,
20+
out_pos: &mut Vec4,
21+
) {
22+
let pos = ubo.projection * ubo.view * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
23+
*out_pos = pos;
24+
*out_position = pos;
25+
}
26+
27+
#[spirv(fragment)]
28+
pub fn main_fs(
29+
in_pos: Vec4,
30+
#[spirv(front_facing)] front_facing: bool,
31+
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<Image!(2D, type=f32, sampled)>,
32+
out_frag_color: &mut Vec4,
33+
) {
34+
let tmp = vec4(1.0 / in_pos.w, 1.0 / in_pos.w, 1.0 / in_pos.w, 1.0 / in_pos.w);
35+
let proj_coord = in_pos * tmp;
36+
37+
// Scale and bias
38+
let proj_coord = proj_coord + vec4(1.0, 1.0, 1.0, 1.0);
39+
let proj_coord = proj_coord * vec4(0.5, 0.5, 0.5, 0.5);
40+
41+
// Slow single pass blur
42+
// For demonstration purposes only
43+
const BLUR_SIZE: f32 = 1.0 / 512.0;
44+
45+
*out_frag_color = vec4(0.0, 0.0, 0.0, 1.0);
46+
47+
if front_facing {
48+
// Only render mirrored scene on front facing (upper) side of mirror surface
49+
let mut reflection = vec4(0.0, 0.0, 0.0, 0.0);
50+
for x in -3..=3 {
51+
for y in -3..=3 {
52+
let offset = vec2(x as f32 * BLUR_SIZE, y as f32 * BLUR_SIZE);
53+
let uv = vec2(proj_coord.x + offset.x, proj_coord.y + offset.y);
54+
reflection = reflection + sampler_color.sample(uv) / 49.0;
55+
}
56+
}
57+
*out_frag_color = *out_frag_color + reflection;
58+
}
59+
}
2.29 KB
Binary file not shown.
9.92 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 = "offscreen-phong"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
spirv-std = { workspace = true }
8+
9+
[lib]
10+
crate-type = ["dylib"]
11+
12+
[package.metadata.rust-gpu.build]
13+
capabilities = ["ClipDistance"]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec4, Mat4, Vec3, Vec4}, num_traits::Float};
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+
#[spirv(vertex)]
16+
pub fn main_vs(
17+
in_pos: Vec3,
18+
in_color: Vec3,
19+
in_normal: Vec3,
20+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
21+
#[spirv(position)] out_position: &mut Vec4,
22+
#[spirv(clip_distance)] clip_distance: &mut [f32; 1],
23+
out_normal: &mut Vec3,
24+
out_color: &mut Vec3,
25+
out_eye_pos: &mut Vec3,
26+
out_light_vec: &mut Vec3,
27+
) {
28+
*out_normal = in_normal;
29+
*out_color = in_color;
30+
*out_position = ubo.projection * ubo.view * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
31+
let eye_pos = ubo.view * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
32+
*out_eye_pos = eye_pos.truncate();
33+
*out_light_vec = (ubo.light_pos.truncate() - *out_eye_pos).normalize();
34+
35+
// Clip against reflection plane
36+
let clip_plane = vec4(0.0, -1.0, 0.0, 0.0);
37+
clip_distance[0] = vec4(in_pos.x, in_pos.y, in_pos.z, 1.0).dot(clip_plane);
38+
}
39+
40+
#[spirv(fragment)]
41+
pub fn main_fs(
42+
in_normal: Vec3,
43+
in_color: Vec3,
44+
in_eye_pos: Vec3,
45+
in_light_vec: Vec3,
46+
out_frag_color: &mut Vec4,
47+
) {
48+
let eye = (-in_eye_pos).normalize();
49+
let reflected = (-in_light_vec).reflect(in_normal).normalize();
50+
51+
let i_ambient = vec4(0.1, 0.1, 0.1, 1.0);
52+
let i_diffuse = vec4(in_normal.dot(in_light_vec).max(0.0), in_normal.dot(in_light_vec).max(0.0), in_normal.dot(in_light_vec).max(0.0), in_normal.dot(in_light_vec).max(0.0));
53+
let specular = 0.75;
54+
let mut i_specular = vec4(0.0, 0.0, 0.0, 0.0);
55+
if in_eye_pos.dot(in_normal) < 0.0 {
56+
let spec_factor = reflected.dot(eye).max(0.0).powf(16.0) * specular;
57+
i_specular = vec4(0.5, 0.5, 0.5, 1.0) * spec_factor;
58+
}
59+
60+
let color_vec4 = vec4(in_color.x, in_color.y, in_color.z, 1.0);
61+
*out_frag_color = (i_ambient + i_diffuse) * color_vec4 + i_specular;
62+
}

0 commit comments

Comments
 (0)