Skip to content

Commit 139307c

Browse files
committed
Add dynamicrendering rust shader
1 parent 68240c0 commit 139307c

6 files changed

Lines changed: 87 additions & 0 deletions

File tree

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
@@ -73,6 +73,7 @@ members = [
7373
"debugprintf/toon",
7474
"pushdescriptors/cube",
7575
"descriptorbuffer/cube",
76+
"dynamicrendering/texture",
7677
]
7778

7879
[workspace.dependencies]
2.3 KB
Binary file not shown.
8.4 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 = "dynamicrendering-texture"
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{mat3, vec3, vec4, Mat4, Vec2, Vec3, Vec4}, Image, num_traits::Float};
5+
use spirv_std::image::SampledImage;
6+
7+
#[repr(C)]
8+
#[derive(Copy, Clone)]
9+
pub struct Ubo {
10+
pub projection: Mat4,
11+
pub model: Mat4,
12+
pub view_pos: Vec4,
13+
}
14+
15+
#[spirv(vertex)]
16+
pub fn main_vs(
17+
in_pos: Vec3,
18+
in_normal: Vec3,
19+
in_uv: Vec2,
20+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
21+
#[spirv(position)] out_position: &mut Vec4,
22+
out_uv: &mut Vec2,
23+
out_normal: &mut Vec3,
24+
out_view_vec: &mut Vec3,
25+
out_light_vec: &mut Vec3,
26+
) {
27+
*out_uv = in_uv;
28+
29+
*out_position = ubo.projection * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
30+
31+
let pos = ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
32+
let inverse_transpose_model = ubo.model.inverse().transpose();
33+
let normal_mat3 = mat3(
34+
inverse_transpose_model.x_axis.truncate(),
35+
inverse_transpose_model.y_axis.truncate(),
36+
inverse_transpose_model.z_axis.truncate(),
37+
);
38+
*out_normal = normal_mat3 * in_normal;
39+
let light_pos = vec3(0.0, 0.0, 0.0);
40+
let model_mat3 = mat3(
41+
ubo.model.x_axis.truncate(),
42+
ubo.model.y_axis.truncate(),
43+
ubo.model.z_axis.truncate(),
44+
);
45+
let l_pos = model_mat3 * light_pos;
46+
*out_light_vec = l_pos - pos.truncate();
47+
*out_view_vec = ubo.view_pos.truncate() - pos.truncate();
48+
}
49+
50+
#[spirv(fragment)]
51+
pub fn main_fs(
52+
in_uv: Vec2,
53+
in_normal: Vec3,
54+
in_view_vec: Vec3,
55+
in_light_vec: Vec3,
56+
#[spirv(descriptor_set = 1, binding = 0)] sampler_color: &SampledImage<Image!(2D, type=f32, sampled)>,
57+
out_frag_color: &mut Vec4,
58+
) {
59+
let color = sampler_color.sample(in_uv);
60+
61+
let n = in_normal.normalize();
62+
let l = in_light_vec.normalize();
63+
let v = in_view_vec.normalize();
64+
let r = (-l).reflect(n);
65+
let diffuse = n.dot(l).max(0.0) * vec3(1.0, 1.0, 1.0);
66+
let specular = r.dot(v).max(0.0).powf(16.0) * color.w;
67+
68+
*out_frag_color = vec4(diffuse.x * color.x + specular, diffuse.y * color.y + specular, diffuse.z * color.z + specular, 1.0);
69+
}

0 commit comments

Comments
 (0)