Skip to content

Commit 3de09c9

Browse files
committed
Add spherical environment mapping rust shaders
1 parent b8d1465 commit 3de09c9

8 files changed

Lines changed: 95 additions & 1 deletion

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ members = [
6565
"texturecubemap/reflect",
6666
"texturecubemap/skybox",
6767
"renderheadless/triangle",
68+
"sphericalenvmapping/sem",
6869
]
6970

7071
[workspace.dependencies]

shaders/rust/compileshaders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def compile_shader(shader_dir):
6868
print(f" Created {final_path.name} (entry point: {entry_point})")
6969

7070
# Special case: move to parent directory for nested shader structures
71-
if shader_dir.parent.name in ["base", "descriptorsets", "dynamicuniformbuffer", "multisampling", "pipelines", "specializationconstants", "computeshader", "texturearray", "screenshot", "negativeviewportheight", "stencilbuffer", "parallaxmapping", "computecloth", "ssao", "shadowmapping", "deferred", "computenbody", "bloom", "hdr", "radialblur", "pbribl", "indirectdraw", "instancing", "texturecubemap", "renderheadless"]:
71+
if shader_dir.parent.name in ["base", "descriptorsets", "dynamicuniformbuffer", "multisampling", "pipelines", "specializationconstants", "computeshader", "texturearray", "screenshot", "negativeviewportheight", "stencilbuffer", "parallaxmapping", "computecloth", "ssao", "shadowmapping", "deferred", "computenbody", "bloom", "hdr", "radialblur", "pbribl", "indirectdraw", "instancing", "texturecubemap", "renderheadless", "sphericalenvmapping"]:
7272
parent_dir = shader_dir.parent
7373
parent_final_path = parent_dir / f"{shader_name}.{shader_type}.spv"
7474
shutil.move(str(final_path), str(parent_final_path))
1.64 KB
Binary file not shown.
9.05 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 = "sem"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
10.5 KB
Binary file not shown.
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::{vec3, vec4, Mat3, Mat4, Vec3, Vec4, Vec3Swizzles}, 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 normal: Mat4,
13+
pub view: Mat4,
14+
pub tex_index: i32,
15+
}
16+
17+
#[spirv(vertex)]
18+
pub fn main_vs(
19+
in_pos: Vec4,
20+
in_normal: Vec3,
21+
in_color: Vec3,
22+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
23+
#[spirv(position)] out_position: &mut Vec4,
24+
out_color: &mut Vec3,
25+
out_eye_pos: &mut Vec3,
26+
out_normal: &mut Vec3,
27+
#[spirv(flat)] out_tex_index: &mut i32,
28+
) {
29+
*out_color = in_color;
30+
let model_view = ubo.view * ubo.model;
31+
*out_eye_pos = (model_view * in_pos).truncate().normalize();
32+
*out_tex_index = ubo.tex_index;
33+
34+
let normal_mat3 = Mat3::from_cols(
35+
ubo.normal.x_axis.truncate(),
36+
ubo.normal.y_axis.truncate(),
37+
ubo.normal.z_axis.truncate(),
38+
);
39+
*out_normal = (normal_mat3 * in_normal).normalize();
40+
41+
let r = out_eye_pos.reflect(*out_normal);
42+
let _m = 2.0 * (r.x.powi(2) + r.y.powi(2) + (r.z + 1.0).powi(2)).sqrt();
43+
*out_position = ubo.projection * model_view * in_pos;
44+
}
45+
46+
#[spirv(fragment)]
47+
pub fn main_fs(
48+
in_color: Vec3,
49+
in_eye_pos: Vec3,
50+
in_normal: Vec3,
51+
#[spirv(flat)] in_tex_index: i32,
52+
#[spirv(descriptor_set = 0, binding = 1)] mat_cap: &SampledImage<Image!(2D, type=f32, sampled, arrayed)>,
53+
out_frag_color: &mut Vec4,
54+
) {
55+
let r = in_eye_pos.reflect(in_normal);
56+
let r2 = vec3(r.x, r.y, r.z + 1.0);
57+
let m = 2.0 * r2.length();
58+
let v_n = r.xy() / m + 0.5;
59+
60+
let tex_coord = vec3(v_n.x, v_n.y, in_tex_index as f32);
61+
let sampled_color = mat_cap.sample(tex_coord);
62+
63+
*out_frag_color = vec4(
64+
sampled_color.x * (in_color.x * 2.0).clamp(0.0, 1.0),
65+
sampled_color.y * (in_color.x * 2.0).clamp(0.0, 1.0),
66+
sampled_color.z * (in_color.x * 2.0).clamp(0.0, 1.0),
67+
1.0
68+
);
69+
}

0 commit comments

Comments
 (0)