Skip to content

Commit cc5f6d2

Browse files
committed
Add raytracingbasic rust shaders
1 parent ee5c4a5 commit cc5f6d2

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-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
@@ -146,6 +146,7 @@ members = [
146146
"conservativeraster/triangle",
147147
"conservativeraster/triangleoverlay",
148148
"conservativeraster/fullscreen",
149+
"raytracingbasic",
149150
]
150151

151152
[workspace.package]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "raytracingbasic"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
11+
12+
[package.metadata.rust-gpu.build]
13+
capabilities = ["RayTracingKHR", "StorageImageWriteWithoutFormat"]
14+
extensions = ["SPV_KHR_ray_tracing"]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#![cfg_attr(target_arch = "spirv", no_std)]
2+
#![allow(clippy::missing_safety_doc)]
3+
4+
use spirv_std::{spirv, glam::{vec2, vec3, vec4, Mat4, Vec2, Vec3, IVec3}};
5+
use spirv_std::glam::Vec4Swizzles;
6+
use spirv_std::ray_tracing::{AccelerationStructure, RayFlags};
7+
use spirv_std::Image;
8+
9+
#[repr(C)]
10+
#[derive(Copy, Clone)]
11+
pub struct CameraProperties {
12+
pub view_inverse: Mat4,
13+
pub proj_inverse: Mat4,
14+
}
15+
16+
#[spirv(ray_generation)]
17+
pub fn main_rgen(
18+
#[spirv(launch_id)] launch_id: IVec3,
19+
#[spirv(launch_size)] launch_size: IVec3,
20+
#[spirv(descriptor_set = 0, binding = 0)] top_level_as: &AccelerationStructure,
21+
#[spirv(descriptor_set = 0, binding = 1)] image: &Image!(2D, type=f32, sampled=false),
22+
#[spirv(uniform, descriptor_set = 0, binding = 2)] cam: &CameraProperties,
23+
#[spirv(ray_payload)] hit_value: &mut Vec3,
24+
) {
25+
let pixel_center = vec2(launch_id.x as f32, launch_id.y as f32) + vec2(0.5, 0.5);
26+
let in_uv = pixel_center / vec2(launch_size.x as f32, launch_size.y as f32);
27+
let d = in_uv * 2.0 - 1.0;
28+
29+
let origin = cam.view_inverse * vec4(0.0, 0.0, 0.0, 1.0);
30+
let target = cam.proj_inverse * vec4(d.x, d.y, 1.0, 1.0);
31+
let normalized_target = target.xyz().normalize();
32+
let direction = cam.view_inverse * vec4(normalized_target.x, normalized_target.y, normalized_target.z, 0.0);
33+
34+
let tmin = 0.001;
35+
let tmax = 10000.0;
36+
37+
*hit_value = vec3(0.0, 0.0, 0.0);
38+
39+
unsafe {
40+
top_level_as.trace_ray(
41+
RayFlags::OPAQUE,
42+
0xff,
43+
0,
44+
0,
45+
0,
46+
origin.xyz(),
47+
tmin,
48+
direction.xyz(),
49+
tmax,
50+
hit_value,
51+
);
52+
}
53+
54+
unsafe {
55+
image.write(
56+
spirv_std::glam::IVec2::new(launch_id.x, launch_id.y),
57+
vec4(hit_value.x, hit_value.y, hit_value.z, 0.0)
58+
);
59+
}
60+
}
61+
62+
#[spirv(closest_hit)]
63+
pub fn main_rchit(
64+
#[spirv(hit_attribute)] attribs: &Vec2,
65+
#[spirv(incoming_ray_payload)] hit_value: &mut Vec3,
66+
) {
67+
let barycentric_coords = vec3(1.0 - attribs.x - attribs.y, attribs.x, attribs.y);
68+
*hit_value = barycentric_coords;
69+
}
70+
71+
#[spirv(miss)]
72+
pub fn main_rmiss(
73+
#[spirv(incoming_ray_payload)] hit_value: &mut Vec3,
74+
) {
75+
*hit_value = vec3(0.0, 0.0, 0.2);
76+
}

0 commit comments

Comments
 (0)