Skip to content

Commit f433298

Browse files
committed
Add tessellation rust shaders
1 parent 3c4f14a commit f433298

File tree

14 files changed

+361
-0
lines changed

14 files changed

+361
-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
@@ -110,6 +110,9 @@ members = [
110110
"ssao/ssao",
111111
"stencilbuffer/outline",
112112
"stencilbuffer/toon",
113+
"tessellation/base",
114+
"tessellation/passthrough",
115+
"tessellation/pntriangles",
113116
"textoverlay/mesh",
114117
"textoverlay/text",
115118
"texture",
1.27 KB
Binary file not shown.
668 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 = "tessellation-base"
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{Vec2, Vec3, Vec4}, Image, Sampler};
5+
6+
#[spirv(vertex)]
7+
pub fn main_vs(
8+
in_pos: Vec3,
9+
in_normal: Vec3,
10+
in_uv: Vec2,
11+
#[spirv(position)] out_position: &mut Vec4,
12+
out_normal: &mut Vec3,
13+
out_uv: &mut Vec2,
14+
) {
15+
*out_position = Vec4::new(in_pos.x, in_pos.y, in_pos.z, 1.0);
16+
*out_normal = in_normal;
17+
*out_uv = in_uv;
18+
}
19+
20+
#[spirv(fragment)]
21+
pub fn main_fs(
22+
#[spirv(descriptor_set = 1, binding = 0)] color_sampler: &Sampler,
23+
#[spirv(descriptor_set = 1, binding = 0)] color_texture: &Image!(2D, type=f32, sampled),
24+
in_normal: Vec3,
25+
in_uv: Vec2,
26+
out_frag_color: &mut Vec4,
27+
) {
28+
let n = in_normal.normalize();
29+
let l = Vec3::new(-4.0, -4.0, 0.0).normalize();
30+
31+
let color: Vec4 = color_texture.sample(*color_sampler, in_uv);
32+
33+
let lighting = n.dot(l).max(0.0).clamp(0.2, 1.0);
34+
*out_frag_color = Vec4::new(
35+
lighting * color.x,
36+
lighting * color.y,
37+
lighting * color.z,
38+
color.w
39+
);
40+
}
2.02 KB
Binary file not shown.
6.47 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 = "tessellation-passthrough"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
11+
12+
[package.metadata.rust-gpu.build]
13+
capabilities = ["Tessellation"]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{Mat4, Vec2, Vec3, Vec4}};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub projection: Mat4,
10+
pub model: Mat4,
11+
pub tess_alpha: f32,
12+
pub tess_level: f32,
13+
}
14+
15+
#[spirv(tessellation_control(output_vertices = 3))]
16+
pub fn main_tcs(
17+
#[spirv(invocation_id)] invocation_id: u32,
18+
#[spirv(position)] in_position: [Vec4; 3],
19+
in_normal: [Vec3; 3],
20+
in_uv: [Vec2; 3],
21+
#[spirv(position)] out_position: &mut [Vec4; 3],
22+
out_normal: &mut [Vec3; 3],
23+
out_uv: &mut [Vec2; 3],
24+
#[spirv(tess_level_inner)] tess_level_inner: &mut [f32; 2],
25+
#[spirv(tess_level_outer)] tess_level_outer: &mut [f32; 4],
26+
) {
27+
if invocation_id == 0 {
28+
tess_level_inner[0] = 1.0;
29+
tess_level_outer[0] = 1.0;
30+
tess_level_outer[1] = 1.0;
31+
tess_level_outer[2] = 1.0;
32+
}
33+
34+
out_position[invocation_id as usize] = in_position[invocation_id as usize];
35+
out_normal[invocation_id as usize] = in_normal[invocation_id as usize];
36+
out_uv[invocation_id as usize] = in_uv[invocation_id as usize];
37+
}
38+
39+
#[spirv(tessellation_evaluation(triangles, spacing_equal, vertex_order_cw))]
40+
pub fn main_tes(
41+
#[spirv(tess_coord)] tess_coord: Vec3,
42+
#[spirv(position)] in_position: [Vec4; 3],
43+
in_normal: [Vec3; 3],
44+
in_uv: [Vec2; 3],
45+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
46+
#[spirv(position)] out_position: &mut Vec4,
47+
out_normal: &mut Vec3,
48+
out_uv: &mut Vec2,
49+
) {
50+
let pos = tess_coord.x * in_position[0] +
51+
tess_coord.y * in_position[1] +
52+
tess_coord.z * in_position[2];
53+
54+
*out_position = ubo.projection * ubo.model * pos;
55+
56+
*out_normal = tess_coord.x * in_normal[0] +
57+
tess_coord.y * in_normal[1] +
58+
tess_coord.z * in_normal[2];
59+
60+
*out_uv = tess_coord.x * in_uv[0] +
61+
tess_coord.y * in_uv[1] +
62+
tess_coord.z * in_uv[2];
63+
}

0 commit comments

Comments
 (0)