Skip to content

Commit d32e247

Browse files
committed
Add meshshader rust shaders
1 parent 1f19968 commit d32e247

File tree

8 files changed

+100
-0
lines changed

8 files changed

+100
-0
lines changed

shaders/rust/Cargo.lock

Lines changed: 7 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ members = [
140140
"shadowmappingcascade/debugshadowmap",
141141
"shadowmappingcascade/scene",
142142
"pipelinestatistics/scene",
143+
"meshshader/meshshader",
143144
]
144145

145146
[workspace.package]

shaders/rust/compileshaders.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def compile_shader(shader_dir):
6363
shader_type = "tesc"
6464
elif entry_point == "main_tes" or "tesseval" in entry_point.lower():
6565
shader_type = "tese"
66+
elif entry_point == "main_task" or "task" in entry_point.lower():
67+
shader_type = "task"
68+
elif entry_point == "main_mesh" or "mesh" in entry_point.lower():
69+
shader_type = "mesh"
6670
else:
6771
# Skip unknown entry points
6872
continue
@@ -92,6 +96,8 @@ def compile_shader(shader_dir):
9296
("main_gs.spv", f"{shader_name}.geom.spv"),
9397
("main_tcs.spv", f"{shader_name}.tesc.spv"),
9498
("main_tes.spv", f"{shader_name}.tese.spv"),
99+
("main_task.spv", f"{shader_name}.task.spv"),
100+
("main_mesh.spv", f"{shader_name}.mesh.spv"),
95101
]
96102

97103
for old_name, new_name in renames:
448 Bytes
Binary file not shown.
8.23 KB
Binary file not shown.
244 Bytes
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "meshshader"
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 = ["MeshShadingEXT"]
14+
extensions = ["SPV_EXT_mesh_shader"]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#![no_std]
2+
3+
use spirv_std::arch::{emit_mesh_tasks_ext, set_mesh_outputs_ext};
4+
use spirv_std::glam::{vec4, Mat4, UVec3, Vec3, Vec4};
5+
use spirv_std::spirv;
6+
7+
#[repr(C)]
8+
#[derive(Copy, Clone)]
9+
pub struct UBO {
10+
pub projection: Mat4,
11+
pub model: Mat4,
12+
pub view: Mat4,
13+
}
14+
15+
#[spirv(task_ext(threads(1)))]
16+
pub fn main_task() {
17+
unsafe {
18+
emit_mesh_tasks_ext(3, 1, 1);
19+
}
20+
}
21+
22+
#[spirv(mesh_ext(
23+
threads(1),
24+
output_vertices = 3,
25+
output_primitives_ext = 1,
26+
output_triangles_ext
27+
))]
28+
pub fn main_mesh(
29+
#[spirv(local_invocation_id)] local_invocation_id: UVec3,
30+
#[spirv(global_invocation_id)] global_invocation_id: UVec3,
31+
#[spirv(local_invocation_index)] local_invocation_index: u32,
32+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
33+
#[spirv(position)] positions: &mut [Vec4; 3],
34+
#[spirv(primitive_triangle_indices_ext)] indices: &mut [UVec3; 1],
35+
out_colors: &mut [Vec3; 3],
36+
) {
37+
const POSITIONS: [Vec4; 3] = [
38+
vec4(0.0, -1.0, 0.0, 1.0),
39+
vec4(-1.0, 1.0, 0.0, 1.0),
40+
vec4(1.0, 1.0, 0.0, 1.0),
41+
];
42+
43+
const COLORS: [Vec3; 3] = [
44+
Vec3::new(0.0, 1.0, 0.0),
45+
Vec3::new(0.0, 0.0, 1.0),
46+
Vec3::new(1.0, 0.0, 0.0),
47+
];
48+
49+
let _iid = local_invocation_id.x;
50+
let offset = vec4(0.0, 0.0, global_invocation_id.x as f32, 0.0);
51+
52+
unsafe {
53+
set_mesh_outputs_ext(3, 1);
54+
}
55+
56+
let mvp = ubo.projection * ubo.view * ubo.model;
57+
positions[0] = mvp * (POSITIONS[0] + offset);
58+
positions[1] = mvp * (POSITIONS[1] + offset);
59+
positions[2] = mvp * (POSITIONS[2] + offset);
60+
out_colors[0] = COLORS[0];
61+
out_colors[1] = COLORS[1];
62+
out_colors[2] = COLORS[2];
63+
indices[local_invocation_index as usize] = UVec3::new(0, 1, 2);
64+
}
65+
66+
#[spirv(fragment)]
67+
pub fn main_fs(
68+
in_color: Vec3,
69+
out_frag_color: &mut Vec4,
70+
) {
71+
*out_frag_color = vec4(in_color.x, in_color.y, in_color.z, 1.0);
72+
}

0 commit comments

Comments
 (0)