Skip to content

Commit d50dd05

Browse files
committed
Add textoverlay rust shaders
1 parent 3a9b89d commit d50dd05

10 files changed

Lines changed: 121 additions & 0 deletions

File tree

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
@@ -78,6 +78,8 @@ members = [
7878
"distancefieldfonts/bitmap",
7979
"distancefieldfonts/sdf",
8080
"conditionalrender/model",
81+
"textoverlay/text",
82+
"textoverlay/mesh",
8183
]
8284

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

0 commit comments

Comments
 (0)