Skip to content

Commit 2083c5f

Browse files
committed
Add texture3d rust shader
1 parent c2bcb94 commit 2083c5f

6 files changed

Lines changed: 98 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
@@ -61,6 +61,7 @@ members = [
6161
"instancing/instancing",
6262
"instancing/planet",
6363
"instancing/starfield",
64+
"texture3d",
6465
]
6566

6667
[workspace.dependencies]

shaders/rust/texture3d/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "texture3d"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }

shaders/rust/texture3d/src/lib.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
3+
use spirv_std::spirv;
4+
use spirv_std::glam::{vec3, vec4, Mat4, Vec2, Vec3, Vec4};
5+
use spirv_std::num_traits::Float;
6+
7+
#[repr(C)]
8+
pub struct UBO {
9+
projection: Mat4,
10+
model: Mat4,
11+
view_pos: Vec4,
12+
depth: f32,
13+
}
14+
15+
#[spirv(vertex)]
16+
pub fn main_vs(
17+
in_pos: Vec3,
18+
in_uv: Vec2,
19+
in_normal: Vec3,
20+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
21+
#[spirv(position)] out_position: &mut Vec4,
22+
out_uv: &mut Vec3,
23+
out_lod_bias: &mut f32,
24+
out_normal: &mut Vec3,
25+
out_view_vec: &mut Vec3,
26+
out_light_vec: &mut Vec3,
27+
) {
28+
*out_uv = vec3(in_uv.x, in_uv.y, ubo.depth);
29+
30+
let world_pos = (ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0)).truncate();
31+
32+
*out_position = ubo.projection * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
33+
34+
let pos = ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
35+
36+
// Transform normal by inverse transpose of model matrix
37+
let normal_matrix = ubo.model.inverse().transpose();
38+
*out_normal = normal_matrix.transform_vector3(in_normal);
39+
40+
let light_pos = Vec3::ZERO;
41+
let l_pos = ubo.model.transform_vector3(light_pos);
42+
*out_light_vec = l_pos - pos.truncate();
43+
*out_view_vec = ubo.view_pos.truncate() - pos.truncate();
44+
45+
// Note: out_lod_bias is not used in the GLSL shader, leaving uninitialized
46+
*out_lod_bias = 0.0;
47+
}
48+
49+
// Reflect function
50+
fn reflect(i: Vec3, n: Vec3) -> Vec3 {
51+
i - 2.0 * n.dot(i) * n
52+
}
53+
54+
#[spirv(fragment)]
55+
pub fn main_fs(
56+
in_uv: Vec3,
57+
_in_lod_bias: f32,
58+
in_normal: Vec3,
59+
in_view_vec: Vec3,
60+
in_light_vec: Vec3,
61+
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &spirv_std::image::SampledImage<spirv_std::image::Image3d>,
62+
out_color: &mut Vec4,
63+
) {
64+
let color = sampler_color.sample(in_uv);
65+
66+
let n = in_normal.normalize();
67+
let l = in_light_vec.normalize();
68+
let v = in_view_vec.normalize();
69+
let r = reflect(-l, n);
70+
71+
let diffuse = n.dot(l).max(0.0) * vec3(1.0, 1.0, 1.0);
72+
let specular = r.dot(v).max(0.0).powf(16.0) * color.x;
73+
74+
*out_color = vec4(
75+
diffuse.x * color.x + specular,
76+
diffuse.y * color.x + specular,
77+
diffuse.z * color.x + specular,
78+
1.0
79+
);
80+
}
2.25 KB
Binary file not shown.
8.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)