Skip to content

Commit c2bcb94

Browse files
committed
Add instancing rust shaders
1 parent ad009a1 commit c2bcb94

15 files changed

Lines changed: 319 additions & 1 deletion

File tree

shaders/rust/Cargo.lock

Lines changed: 21 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ members = [
5858
"indirectdraw/indirectdraw",
5959
"indirectdraw/ground",
6060
"indirectdraw/skysphere",
61+
"instancing/instancing",
62+
"instancing/planet",
63+
"instancing/starfield",
6164
]
6265

6366
[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"]:
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"]:
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))
2.89 KB
Binary file not shown.
9.79 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 = "instancing-instancing"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
3+
use spirv_std::spirv;
4+
use spirv_std::glam::{vec3, vec4, Mat3, Mat4, Vec2, Vec3, Vec4};
5+
use spirv_std::num_traits::Float;
6+
7+
#[repr(C)]
8+
pub struct UBO {
9+
projection: Mat4,
10+
modelview: Mat4,
11+
light_pos: Vec4,
12+
loc_speed: f32,
13+
glob_speed: f32,
14+
}
15+
16+
#[spirv(vertex)]
17+
pub fn main_vs(
18+
in_pos: Vec3,
19+
in_normal: Vec3,
20+
in_uv: Vec2,
21+
in_color: Vec3,
22+
instance_pos: Vec3,
23+
instance_rot: Vec3,
24+
instance_scale: f32,
25+
instance_tex_index: i32,
26+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
27+
#[spirv(position)] out_position: &mut Vec4,
28+
out_normal: &mut Vec3,
29+
out_color: &mut Vec3,
30+
out_uv: &mut Vec3,
31+
out_view_vec: &mut Vec3,
32+
out_light_vec: &mut Vec3,
33+
) {
34+
*out_color = in_color;
35+
*out_uv = vec3(in_uv.x, in_uv.y, instance_tex_index as f32);
36+
37+
// rotate around x
38+
let s = (instance_rot.x + ubo.loc_speed).sin();
39+
let c = (instance_rot.x + ubo.loc_speed).cos();
40+
41+
let mx = Mat3::from_cols(
42+
vec3(c, s, 0.0),
43+
vec3(-s, c, 0.0),
44+
vec3(0.0, 0.0, 1.0),
45+
);
46+
47+
// rotate around y
48+
let s = (instance_rot.y + ubo.loc_speed).sin();
49+
let c = (instance_rot.y + ubo.loc_speed).cos();
50+
51+
let my = Mat3::from_cols(
52+
vec3(c, 0.0, s),
53+
vec3(0.0, 1.0, 0.0),
54+
vec3(-s, 0.0, c),
55+
);
56+
57+
// rotate around z
58+
let s = (instance_rot.z + ubo.loc_speed).sin();
59+
let c = (instance_rot.z + ubo.loc_speed).cos();
60+
61+
let mz = Mat3::from_cols(
62+
vec3(1.0, 0.0, 0.0),
63+
vec3(0.0, c, s),
64+
vec3(0.0, -s, c),
65+
);
66+
67+
let rot_mat = mz * my * mx;
68+
69+
// Global rotation matrix
70+
let s = (instance_rot.y + ubo.glob_speed).sin();
71+
let c = (instance_rot.y + ubo.glob_speed).cos();
72+
73+
let g_rot_mat = Mat4::from_cols(
74+
vec4(c, 0.0, s, 0.0),
75+
vec4(0.0, 1.0, 0.0, 0.0),
76+
vec4(-s, 0.0, c, 0.0),
77+
vec4(0.0, 0.0, 0.0, 1.0),
78+
);
79+
80+
let loc_pos = rot_mat * in_pos;
81+
let pos = vec4(
82+
loc_pos.x * instance_scale + instance_pos.x,
83+
loc_pos.y * instance_scale + instance_pos.y,
84+
loc_pos.z * instance_scale + instance_pos.z,
85+
1.0
86+
);
87+
88+
*out_position = ubo.projection * ubo.modelview * g_rot_mat * pos;
89+
*out_normal = ubo.modelview.transform_vector3(g_rot_mat.transform_vector3(rot_mat.inverse() * in_normal));
90+
91+
let pos = ubo.modelview * vec4(
92+
in_pos.x + instance_pos.x,
93+
in_pos.y + instance_pos.y,
94+
in_pos.z + instance_pos.z,
95+
1.0
96+
);
97+
let l_pos = ubo.modelview.transform_vector3(ubo.light_pos.truncate());
98+
*out_light_vec = l_pos - pos.truncate();
99+
*out_view_vec = -pos.truncate();
100+
}
101+
102+
// Reflect function
103+
fn reflect(i: Vec3, n: Vec3) -> Vec3 {
104+
i - 2.0 * n.dot(i) * n
105+
}
106+
107+
#[spirv(fragment)]
108+
pub fn main_fs(
109+
in_normal: Vec3,
110+
in_color: Vec3,
111+
in_uv: Vec3,
112+
in_view_vec: Vec3,
113+
in_light_vec: Vec3,
114+
#[spirv(descriptor_set = 0, binding = 1)] sampler_array: &spirv_std::image::SampledImage<spirv_std::image::Image2dArray>,
115+
out_color: &mut Vec4,
116+
) {
117+
let color = sampler_array.sample(vec3(in_uv.x, in_uv.y, in_uv.z)) * vec4(in_color.x, in_color.y, in_color.z, 1.0);
118+
let n = in_normal.normalize();
119+
let l = in_light_vec.normalize();
120+
let v = in_view_vec.normalize();
121+
let r = reflect(-l, n);
122+
123+
let diffuse = n.dot(l).max(0.1) * in_color;
124+
let specular = if n.dot(l) > 0.0 {
125+
r.dot(v).max(0.0).powf(16.0) * vec3(0.75, 0.75, 0.75) * color.x
126+
} else {
127+
Vec3::ZERO
128+
};
129+
130+
*out_color = vec4(
131+
diffuse.x * color.x + specular.x,
132+
diffuse.y * color.y + specular.y,
133+
diffuse.z * color.z + specular.z,
134+
1.0
135+
);
136+
}
2.76 KB
Binary file not shown.
6.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 = "planet"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }

0 commit comments

Comments
 (0)