Skip to content

Commit eab943a

Browse files
committed
Add inputattachments rust shaders
1 parent bb3d516 commit eab943a

File tree

10 files changed

+157
-0
lines changed

10 files changed

+157
-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
@@ -52,6 +52,8 @@ members = [
5252
"instancing/instancing",
5353
"instancing/planet",
5454
"instancing/starfield",
55+
"inputattachments/attachmentwrite",
56+
"inputattachments/attachmentread",
5557
"multisampling/mesh",
5658
"multithreading/phong",
5759
"multithreading/starsphere",
Binary file not shown.
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 = "inputattachments-attachmentread"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[dependencies]
7+
spirv-std = { workspace = true }
8+
9+
[lib]
10+
crate-type = ["dylib"]
11+
12+
[package.metadata.rust-gpu.build]
13+
capabilities = ["InputAttachment"]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec4, Vec2, Vec3, Vec4}};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub brightness_contrast: Vec2,
10+
pub range: Vec2,
11+
pub attachment_index: i32,
12+
}
13+
14+
#[spirv(vertex)]
15+
pub fn main_vs(
16+
#[spirv(vertex_index)] vert_idx: i32,
17+
#[spirv(position)] out_position: &mut Vec4,
18+
) {
19+
let x = ((vert_idx << 1) & 2) as f32;
20+
let y = (vert_idx & 2) as f32;
21+
*out_position = vec4(x * 2.0 - 1.0, y * 2.0 - 1.0, 0.0, 1.0);
22+
}
23+
24+
fn brightness_contrast(color: Vec3, brightness: f32, contrast: f32) -> Vec3 {
25+
(color - 0.5) * contrast + 0.5 + brightness
26+
}
27+
28+
#[spirv(fragment)]
29+
pub fn main_fs(
30+
#[spirv(frag_coord)] frag_coord: Vec4,
31+
#[spirv(input_attachment_index = 0, descriptor_set = 0, binding = 0)] input_color: &spirv_std::Image!(subpass, type=f32, sampled=false),
32+
#[spirv(input_attachment_index = 1, descriptor_set = 0, binding = 1)] input_depth: &spirv_std::Image!(subpass, type=f32, sampled=false),
33+
#[spirv(uniform, descriptor_set = 0, binding = 2)] ubo: &Ubo,
34+
out_color: &mut Vec4,
35+
) {
36+
let coord = spirv_std::glam::IVec2::new(frag_coord.x as i32, frag_coord.y as i32);
37+
38+
// Apply brightness and contrast filter to color input
39+
if ubo.attachment_index == 0 {
40+
// Read color from previous color input attachment
41+
let color = input_color.read_subpass(coord).truncate();
42+
let adjusted = brightness_contrast(color, ubo.brightness_contrast.x, ubo.brightness_contrast.y);
43+
*out_color = vec4(adjusted.x, adjusted.y, adjusted.z, 1.0);
44+
}
45+
46+
// Visualize depth input range
47+
if ubo.attachment_index == 1 {
48+
// Read depth from previous depth input attachment
49+
let depth = input_depth.read_subpass(coord).x;
50+
let normalized = (depth - ubo.range.x) * (1.0 / (ubo.range.y - ubo.range.x));
51+
*out_color = vec4(normalized, normalized, normalized, 1.0);
52+
}
53+
}
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 = "inputattachments-attachmentwrite"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[dependencies]
7+
spirv-std = { workspace = true }
8+
9+
[lib]
10+
crate-type = ["dylib"]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec3, vec4, Mat4, Vec3, Vec4}};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub projection: Mat4,
10+
pub model: Mat4,
11+
pub view: Mat4,
12+
}
13+
14+
#[spirv(vertex)]
15+
pub fn main_vs(
16+
in_pos: Vec3,
17+
in_color: Vec3,
18+
in_normal: Vec3,
19+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
20+
#[spirv(position)] out_position: &mut Vec4,
21+
out_color: &mut Vec3,
22+
out_normal: &mut Vec3,
23+
out_view_vec: &mut Vec3,
24+
out_light_vec: &mut Vec3,
25+
) {
26+
*out_position = ubo.projection * ubo.view * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
27+
*out_color = in_color;
28+
*out_normal = in_normal;
29+
*out_light_vec = vec3(0.0, 5.0, 15.0) - in_pos;
30+
*out_view_vec = -in_pos;
31+
}
32+
33+
#[spirv(fragment)]
34+
pub fn main_fs(
35+
in_color: Vec3,
36+
in_normal: Vec3,
37+
_in_view_vec: Vec3,
38+
in_light_vec: Vec3,
39+
out_color: &mut Vec4,
40+
) {
41+
// Toon shading color attachment output
42+
let intensity = in_normal.normalize().dot(in_light_vec.normalize());
43+
let mut shade = 1.0;
44+
if intensity < 0.5 {
45+
shade = 0.75;
46+
}
47+
if intensity < 0.35 {
48+
shade = 0.6;
49+
}
50+
if intensity < 0.25 {
51+
shade = 0.5;
52+
}
53+
if intensity < 0.1 {
54+
shade = 0.25;
55+
}
56+
57+
*out_color = vec4(
58+
in_color.x * 3.0 * shade,
59+
in_color.y * 3.0 * shade,
60+
in_color.z * 3.0 * shade,
61+
1.0
62+
);
63+
64+
// Depth attachment does not need to be explicitly written
65+
}

0 commit comments

Comments
 (0)