Skip to content

Commit ab02b21

Browse files
committed
Add occlusion query rust shaders
1 parent 3de09c9 commit ab02b21

22 files changed

Lines changed: 189 additions & 1 deletion

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
@@ -66,6 +66,9 @@ members = [
6666
"texturecubemap/skybox",
6767
"renderheadless/triangle",
6868
"sphericalenvmapping/sem",
69+
"occlusionquery/simple",
70+
"occlusionquery/mesh",
71+
"occlusionquery/occluder",
6972
]
7073

7174
[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", "sphericalenvmapping"]:
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", "occlusionquery"]:
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.34 KB
Binary file not shown.
8.87 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 = "occlusionquery-mesh"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
2.34 KB
Binary file not shown.
8.87 KB
Binary file not shown.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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}, 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 color: Vec4,
13+
pub light_pos: Vec4,
14+
pub visible: f32,
15+
}
16+
17+
#[spirv(vertex)]
18+
pub fn main_vs(
19+
in_pos: Vec3,
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_normal: &mut Vec3,
25+
out_color: &mut Vec3,
26+
out_visible: &mut f32,
27+
out_view_vec: &mut Vec3,
28+
out_light_vec: &mut Vec3,
29+
) {
30+
*out_normal = in_normal;
31+
*out_color = in_color * ubo.color.truncate();
32+
*out_visible = ubo.visible;
33+
34+
*out_position = ubo.projection * ubo.view * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
35+
36+
let pos = ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
37+
let model_mat3 = Mat3::from_cols(
38+
ubo.model.x_axis.truncate(),
39+
ubo.model.y_axis.truncate(),
40+
ubo.model.z_axis.truncate(),
41+
);
42+
*out_normal = model_mat3 * in_normal;
43+
*out_light_vec = ubo.light_pos.truncate() - pos.truncate();
44+
*out_view_vec = -pos.truncate();
45+
}
46+
47+
#[spirv(fragment)]
48+
pub fn main_fs(
49+
in_normal: Vec3,
50+
in_color: Vec3,
51+
in_visible: f32,
52+
in_view_vec: Vec3,
53+
in_light_vec: Vec3,
54+
out_frag_color: &mut Vec4,
55+
) {
56+
if in_visible > 0.0 {
57+
let n = in_normal.normalize();
58+
let l = in_light_vec.normalize();
59+
let v = in_view_vec.normalize();
60+
let r = (-l).reflect(n);
61+
let diffuse = n.dot(l).max(0.25) * in_color;
62+
let specular = r.dot(v).max(0.0).powf(8.0) * vec3(0.75, 0.75, 0.75);
63+
*out_frag_color = vec4(diffuse.x + specular.x, diffuse.y + specular.y, diffuse.z + specular.z, 1.0);
64+
} else {
65+
*out_frag_color = vec4(0.1, 0.1, 0.1, 1.0);
66+
}
67+
}
416 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)