Skip to content

Commit 28d62eb

Browse files
committed
Add texturecubemaparray rust shader
1 parent f375090 commit 28d62eb

File tree

10 files changed

+148
-0
lines changed

10 files changed

+148
-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
@@ -91,6 +91,8 @@ members = [
9191
"texturearray/instancing",
9292
"texturecubemap/reflect",
9393
"texturecubemap/skybox",
94+
"texturecubemaparray/reflect",
95+
"texturecubemaparray/skybox",
9496
"texturemipmapgen/texture",
9597
"triangle",
9698
"vertexattributes/scene",
Binary file not shown.
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 = "texturecubemaparray-reflect"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#![no_std]
2+
3+
use spirv_std::spirv;
4+
use spirv_std::{glam::{vec3, vec4, Mat3, Mat4, Vec3, Vec4}, Image, Sampler};
5+
use spirv_std::num_traits::Float;
6+
7+
#[repr(C)]
8+
pub struct UBO {
9+
pub projection: Mat4,
10+
pub model: Mat4,
11+
pub inv_model: Mat4,
12+
pub lod_bias: f32,
13+
pub cube_map_index: i32,
14+
}
15+
16+
#[spirv(vertex)]
17+
pub fn main_vs(
18+
#[spirv(vertex_index)] _vertex_index: i32,
19+
in_pos: Vec3,
20+
in_normal: Vec3,
21+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
22+
#[spirv(position)] out_pos: &mut Vec4,
23+
out_world_pos: &mut Vec3,
24+
out_normal: &mut Vec3,
25+
out_view_vec: &mut Vec3,
26+
out_light_vec: &mut Vec3,
27+
) {
28+
*out_pos = ubo.projection * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
29+
30+
*out_world_pos = (ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0)).truncate();
31+
*out_normal = Mat3::from_mat4(ubo.model) * in_normal;
32+
33+
let light_pos = vec3(0.0, -5.0, 5.0);
34+
*out_light_vec = light_pos - *out_world_pos;
35+
*out_view_vec = -*out_world_pos;
36+
}
37+
38+
#[spirv(fragment)]
39+
pub fn main_fs(
40+
in_pos: Vec3,
41+
in_normal: Vec3,
42+
in_view_vec: Vec3,
43+
in_light_vec: Vec3,
44+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
45+
#[spirv(descriptor_set = 0, binding = 1)] sampler: &Sampler,
46+
#[spirv(descriptor_set = 0, binding = 1)] image: &Image!(cube, type=f32, sampled, arrayed),
47+
out_frag_color: &mut Vec4,
48+
) {
49+
let ci = in_pos.normalize();
50+
let cr = ci.reflect(in_normal.normalize());
51+
52+
let mut cr_transformed = (ubo.inv_model * vec4(cr.x, cr.y, cr.z, 0.0)).truncate();
53+
cr_transformed.y *= -1.0;
54+
cr_transformed.z *= -1.0;
55+
56+
let coord = vec4(cr_transformed.x, cr_transformed.y, cr_transformed.z, ubo.cube_map_index as f32);
57+
let color = image.sample_by_lod(*sampler, coord, ubo.lod_bias);
58+
59+
let n = in_normal.normalize();
60+
let l = in_light_vec.normalize();
61+
let v = in_view_vec.normalize();
62+
let r = (-l).reflect(n);
63+
64+
let ambient = vec3(0.5, 0.5, 0.5) * color.truncate();
65+
let diffuse = n.dot(l).max(0.0) * vec3(1.0, 1.0, 1.0);
66+
let specular = r.dot(v).max(0.0).powf(16.0) * vec3(0.5, 0.5, 0.5);
67+
68+
let final_color = ambient + diffuse * color.truncate() + specular;
69+
*out_frag_color = vec4(final_color.x, final_color.y, final_color.z, 1.0);
70+
}
Binary file not shown.
3.43 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 = "texturecubemaparray-skybox"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![no_std]
2+
3+
use spirv_std::spirv;
4+
use spirv_std::{glam::{vec4, Mat3, Mat4, Vec3, Vec4}, Image, Sampler};
5+
6+
#[repr(C)]
7+
pub struct UBO {
8+
pub projection: Mat4,
9+
pub model: Mat4,
10+
pub inv_model: Mat4,
11+
pub lod_bias: f32,
12+
pub cube_map_index: i32,
13+
}
14+
15+
#[spirv(vertex)]
16+
pub fn main_vs(
17+
#[spirv(vertex_index)] _vertex_index: i32,
18+
in_pos: Vec3,
19+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
20+
#[spirv(position)] out_pos: &mut Vec4,
21+
out_uvw: &mut Vec3,
22+
) {
23+
*out_uvw = in_pos;
24+
out_uvw.x *= -1.0;
25+
out_uvw.y *= -1.0;
26+
27+
// Remove translation from view matrix
28+
let view_mat = Mat3::from_mat4(ubo.model);
29+
*out_pos = ubo.projection * Mat4::from_mat3(view_mat) * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
30+
}
31+
32+
#[spirv(fragment)]
33+
pub fn main_fs(
34+
in_uvw: Vec3,
35+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
36+
#[spirv(descriptor_set = 0, binding = 1)] sampler: &Sampler,
37+
#[spirv(descriptor_set = 0, binding = 1)] image: &Image!(cube, type=f32, sampled, arrayed),
38+
out_frag_color: &mut Vec4,
39+
) {
40+
let coord = vec4(in_uvw.x, in_uvw.y, in_uvw.z, ubo.cube_map_index as f32);
41+
*out_frag_color = image.sample_by_lod(*sampler, coord, ubo.lod_bias);
42+
}

0 commit comments

Comments
 (0)