Skip to content

Commit 8c7c921

Browse files
committed
Add distancefieldfonts rust shaders
1 parent ae5fa9c commit 8c7c921

10 files changed

Lines changed: 131 additions & 0 deletions

File tree

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ members = [
7575
"descriptorbuffer/cube",
7676
"dynamicrendering/texture",
7777
"computeparticles/particle",
78+
"distancefieldfonts/bitmap",
79+
"distancefieldfonts/sdf",
7880
]
7981

8082
[workspace.dependencies]
512 Bytes
Binary file not shown.
4.37 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 = "distancefieldfonts-bitmap"
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec4, Mat4, Vec2, Vec3, Vec4}, Image};
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+
}
13+
14+
#[spirv(vertex)]
15+
pub fn main_vs(
16+
in_pos: Vec3,
17+
in_uv: Vec2,
18+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
19+
#[spirv(position)] out_position: &mut Vec4,
20+
out_uv: &mut Vec2,
21+
) {
22+
*out_uv = in_uv;
23+
*out_position = ubo.projection * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
24+
}
25+
26+
#[spirv(fragment)]
27+
pub fn main_fs(
28+
in_uv: Vec2,
29+
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<Image!(2D, type=f32, sampled)>,
30+
out_frag_color: &mut Vec4,
31+
) {
32+
let alpha = sampler_color.sample(in_uv).w;
33+
*out_frag_color = vec4(alpha, alpha, alpha, alpha);
34+
}
2.24 KB
Binary file not shown.
4.44 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 = "distancefieldfonts-sdf"
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec3, vec4, Mat4, Vec2, Vec3, Vec4}, Image};
5+
use spirv_std::image::SampledImage;
6+
use spirv_std::arch::Derivative;
7+
8+
#[repr(C)]
9+
#[derive(Copy, Clone)]
10+
pub struct Ubo {
11+
pub projection: Mat4,
12+
pub model: Mat4,
13+
pub outline_color: Vec4,
14+
pub outline_width: f32,
15+
pub outline: f32,
16+
}
17+
18+
#[spirv(vertex)]
19+
pub fn main_vs(
20+
in_pos: Vec3,
21+
in_uv: Vec2,
22+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
23+
#[spirv(position)] out_position: &mut Vec4,
24+
out_uv: &mut Vec2,
25+
) {
26+
*out_uv = in_uv;
27+
*out_position = ubo.projection * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
28+
}
29+
30+
fn smooth_step(edge0: f32, edge1: f32, x: f32) -> f32 {
31+
let t = ((x - edge0) / (edge1 - edge0)).clamp(0.0, 1.0);
32+
t * t * (3.0 - 2.0 * t)
33+
}
34+
35+
#[spirv(fragment)]
36+
pub fn main_fs(
37+
in_uv: Vec2,
38+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
39+
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<Image!(2D, type=f32, sampled)>,
40+
out_frag_color: &mut Vec4,
41+
) {
42+
let distance = sampler_color.sample(in_uv).w;
43+
let smooth_width = distance.fwidth();
44+
let mut alpha = smooth_step(0.5 - smooth_width, 0.5 + smooth_width, distance);
45+
let mut rgb = vec3(alpha, alpha, alpha);
46+
47+
if ubo.outline > 0.0 {
48+
let w = 1.0 - ubo.outline_width;
49+
alpha = smooth_step(w - smooth_width, w + smooth_width, distance);
50+
rgb += rgb.lerp(ubo.outline_color.truncate(), alpha);
51+
}
52+
53+
*out_frag_color = vec4(rgb.x, rgb.y, rgb.z, alpha);
54+
}

0 commit comments

Comments
 (0)