Skip to content

Commit e5b7541

Browse files
committed
Add texturemipmapgen rust shader
1 parent 531212f commit e5b7541

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

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
@@ -92,6 +92,7 @@ members = [
9292
"multiview/viewdisplay",
9393
"imgui/ui",
9494
"imgui/scene",
95+
"texturemipmapgen/texture",
9596
]
9697

9798
[workspace.dependencies]
3.13 KB
Binary file not shown.
11.3 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 = "texturemipmapgen-texture"
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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec2, vec3, vec4, Mat3, Mat4, Vec2, Vec3, Vec4, Vec4Swizzles}, Image, Sampler, num_traits::Float};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub struct Ubo {
9+
pub projection: Mat4,
10+
pub view: Mat4,
11+
pub model: Mat4,
12+
pub view_pos: Vec4,
13+
pub lod_bias: f32,
14+
pub sampler_index: i32,
15+
}
16+
17+
#[spirv(vertex)]
18+
pub fn main_vs(
19+
in_pos: Vec3,
20+
in_uv: Vec2,
21+
in_normal: Vec3,
22+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
23+
#[spirv(position)] out_position: &mut Vec4,
24+
out_uv: &mut Vec2,
25+
out_lod_bias: &mut f32,
26+
out_normal: &mut Vec3,
27+
out_view_vec: &mut Vec3,
28+
out_light_vec: &mut Vec3,
29+
) {
30+
*out_uv = in_uv * vec2(2.0, 1.0);
31+
*out_lod_bias = ubo.lod_bias;
32+
33+
let world_pos = (ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0)).xyz();
34+
35+
*out_position = ubo.projection * ubo.view * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
36+
37+
*out_normal = Mat3::from_mat4(ubo.model.inverse().transpose()) * in_normal;
38+
let light_pos = vec3(-30.0, 0.0, 0.0);
39+
*out_light_vec = world_pos - light_pos;
40+
*out_view_vec = ubo.view_pos.xyz() - world_pos;
41+
}
42+
43+
#[spirv(fragment)]
44+
pub fn main_fs(
45+
in_uv: Vec2,
46+
in_lod_bias: f32,
47+
in_normal: Vec3,
48+
in_view_vec: Vec3,
49+
in_light_vec: Vec3,
50+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &Ubo,
51+
#[spirv(descriptor_set = 0, binding = 1)] texture_color: &Image!(2D, type=f32, sampled),
52+
#[spirv(descriptor_set = 0, binding = 2)] samplers: &[Sampler; 3],
53+
out_frag_color: &mut Vec4,
54+
) {
55+
let sampler = &samplers[ubo.sampler_index as usize];
56+
let color = texture_color.sample_bias(*sampler, in_uv, in_lod_bias);
57+
58+
let n = in_normal.normalize();
59+
let l = in_light_vec.normalize();
60+
let v = in_view_vec.normalize();
61+
let r = l.reflect(n);
62+
let diffuse = n.dot(l).max(0.65) * vec3(1.0, 1.0, 1.0);
63+
let specular = r.dot(v).max(0.0).powf(16.0) * color.w;
64+
let final_color = diffuse * color.xyz() + vec3(specular, specular, specular);
65+
*out_frag_color = vec4(final_color.x, final_color.y, final_color.z, 1.0);
66+
}

0 commit comments

Comments
 (0)