Skip to content

Commit e22e3d8

Browse files
committed
Add viewportarray rust shaders
1 parent 7f3ab45 commit e22e3d8

File tree

9 files changed

+136
-0
lines changed

9 files changed

+136
-0
lines changed

shaders/rust/Cargo.lock

Lines changed: 16 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
@@ -128,6 +128,8 @@ members = [
128128
"texturemipmapgen/texture",
129129
"triangle",
130130
"vertexattributes/scene",
131+
"viewportarray/scene",
132+
"viewportarray/multiview",
131133
"vulkanscene/logo",
132134
"vulkanscene/mesh",
133135
"vulkanscene/skybox",
6.05 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 = "viewportarray-multiview"
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 = ["Geometry", "ShaderViewportIndex", "MultiViewport"]
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::{Mat3, Mat4, Vec3, Vec4}};
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(geometry(triangles = 3, output_triangle_strip = 3, invocations = 2))]
15+
pub fn main_gs(
16+
#[spirv(position)] in_position: [Vec4; 3],
17+
in_normal: [Vec3; 3],
18+
in_color: [Vec3; 3],
19+
#[spirv(invocation_id)] invocation_id: u32,
20+
#[spirv(primitive_id)] primitive_id_in: u32,
21+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
22+
#[spirv(position)] out_position: &mut Vec4,
23+
out_normal: &mut Vec3,
24+
out_color: &mut Vec3,
25+
out_view_vec: &mut Vec3,
26+
out_light_vec: &mut Vec3,
27+
#[spirv(viewport_index, flat)] out_viewport_index: &mut u32,
28+
#[spirv(primitive_id, flat)] out_primitive_id: &mut u32,
29+
) {
30+
let inv_id = invocation_id as usize;
31+
32+
for i in 0..3 {
33+
*out_normal = Mat3::from_mat4(ubo.modelview[inv_id]) * in_normal[i];
34+
*out_color = in_color[i];
35+
36+
let pos = in_position[i];
37+
let world_pos = ubo.modelview[inv_id] * pos;
38+
39+
let l_pos = (ubo.modelview[inv_id] * ubo.light_pos).truncate();
40+
*out_light_vec = l_pos - world_pos.truncate();
41+
*out_view_vec = -world_pos.truncate();
42+
43+
*out_position = ubo.projection[inv_id] * world_pos;
44+
45+
// Set the viewport index that the vertex will be emitted to
46+
*out_viewport_index = invocation_id;
47+
*out_primitive_id = primitive_id_in;
48+
49+
unsafe { spirv_std::arch::emit_vertex() };
50+
}
51+
52+
unsafe { spirv_std::arch::end_primitive() };
53+
}
2.15 KB
Binary file not shown.
620 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 = "viewportarray-scene"
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{Vec3, Vec4}};
5+
use spirv_std::num_traits::Float;
6+
7+
#[spirv(vertex)]
8+
pub fn main_vs(
9+
in_pos: Vec3,
10+
in_normal: Vec3,
11+
in_color: Vec3,
12+
#[spirv(position)] out_position: &mut Vec4,
13+
out_normal: &mut Vec3,
14+
out_color: &mut Vec3,
15+
) {
16+
*out_color = in_color;
17+
*out_normal = in_normal;
18+
*out_position = Vec4::new(in_pos.x, in_pos.y, in_pos.z, 1.0);
19+
}
20+
21+
#[spirv(fragment)]
22+
pub fn main_fs(
23+
in_normal: Vec3,
24+
in_color: Vec3,
25+
in_view_vec: Vec3,
26+
in_light_vec: Vec3,
27+
out_color: &mut Vec4,
28+
) {
29+
let n = in_normal.normalize();
30+
let l = in_light_vec.normalize();
31+
let v = in_view_vec.normalize();
32+
let r = (-l).reflect(n);
33+
let ambient = Vec3::splat(0.1);
34+
let diffuse = n.dot(l).max(0.0) * Vec3::ONE;
35+
let specular = r.dot(v).max(0.0).powf(16.0) * Vec3::splat(0.75);
36+
*out_color = Vec4::new(
37+
((ambient + diffuse) * in_color + specular).x,
38+
((ambient + diffuse) * in_color + specular).y,
39+
((ambient + diffuse) * in_color + specular).z,
40+
1.0
41+
);
42+
}

0 commit comments

Comments
 (0)