Skip to content

Commit 531212f

Browse files
committed
Add imgui rust shaders
1 parent 816d03c commit 531212f

File tree

10 files changed

+127
-0
lines changed

10 files changed

+127
-0
lines changed

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
@@ -90,6 +90,8 @@ members = [
9090
"vertexattributes/scene",
9191
"multiview/multiview",
9292
"multiview/viewdisplay",
93+
"imgui/ui",
94+
"imgui/scene",
9395
]
9496

9597
[workspace.dependencies]

shaders/rust/imgui/scene.frag.spv

2.11 KB
Binary file not shown.

shaders/rust/imgui/scene.vert.spv

5.85 KB
Binary file not shown.

shaders/rust/imgui/scene/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "imgui-scene"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Sascha Willems <webmaster@saschawillems.de>"]
6+
7+
[lib]
8+
crate-type = ["dylib"]
9+
10+
[dependencies]
11+
spirv-std = { workspace = true }

shaders/rust/imgui/scene/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec3, vec4, Mat3, Mat4, Vec3, Vec4, Vec4Swizzles}, 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_color: Vec3,
19+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
20+
#[spirv(position)] out_position: &mut Vec4,
21+
out_normal: &mut Vec3,
22+
out_color: &mut Vec3,
23+
out_view_vec: &mut Vec3,
24+
out_light_vec: &mut Vec3,
25+
) {
26+
*out_color = in_color;
27+
*out_position = ubo.projection * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
28+
29+
let pos = ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
30+
*out_normal = Mat3::from_mat4(ubo.model) * in_normal;
31+
let l_pos = Mat3::from_mat4(ubo.model) * ubo.light_pos.xyz();
32+
*out_light_vec = l_pos - pos.xyz();
33+
*out_view_vec = -pos.xyz();
34+
}
35+
36+
#[spirv(fragment)]
37+
pub fn main_fs(
38+
in_normal: Vec3,
39+
in_color: Vec3,
40+
in_view_vec: Vec3,
41+
in_light_vec: Vec3,
42+
out_frag_color: &mut Vec4,
43+
) {
44+
let n = in_normal.normalize();
45+
let l = in_light_vec.normalize();
46+
let v = in_view_vec.normalize();
47+
let r = (-l).reflect(n);
48+
let diffuse = n.dot(l).max(0.0);
49+
let specular = r.dot(v).max(0.0).powf(16.0) * vec3(0.75, 0.75, 0.75);
50+
let color = diffuse * in_color + specular;
51+
*out_frag_color = vec4(color.x, color.y, color.z, 1.0);
52+
}

shaders/rust/imgui/ui.frag.spv

552 Bytes
Binary file not shown.

shaders/rust/imgui/ui.vert.spv

1.05 KB
Binary file not shown.

shaders/rust/imgui/ui/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "imgui-ui"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Sascha Willems <webmaster@saschawillems.de>"]
6+
7+
[lib]
8+
crate-type = ["dylib"]
9+
10+
[dependencies]
11+
spirv-std = { workspace = true }

shaders/rust/imgui/ui/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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, image::SampledImage};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct PushConstants {
9+
pub scale: Vec2,
10+
pub translate: Vec2,
11+
}
12+
13+
#[spirv(vertex)]
14+
pub fn main_vs(
15+
in_pos: Vec2,
16+
in_uv: Vec2,
17+
in_color: Vec4,
18+
#[spirv(push_constant)] push_constants: &PushConstants,
19+
#[spirv(position)] out_position: &mut Vec4,
20+
out_uv: &mut Vec2,
21+
out_color: &mut Vec4,
22+
) {
23+
*out_uv = in_uv;
24+
*out_color = in_color;
25+
let xy = in_pos * push_constants.scale + push_constants.translate;
26+
*out_position = vec4(xy.x, xy.y, 0.0, 1.0);
27+
}
28+
29+
#[spirv(fragment)]
30+
pub fn main_fs(
31+
in_uv: Vec2,
32+
in_color: Vec4,
33+
#[spirv(descriptor_set = 0, binding = 0)] font_sampler: &SampledImage<Image!(2D, type=f32, sampled)>,
34+
out_color: &mut Vec4,
35+
) {
36+
*out_color = in_color * font_sampler.sample(in_uv);
37+
}

0 commit comments

Comments
 (0)