|
| 1 | +#![cfg_attr( |
| 2 | + target_os = "cuda", |
| 3 | + no_std, |
| 4 | + feature(register_attr), |
| 5 | + register_attr(nvvm_internal) |
| 6 | +)] |
| 7 | +#![allow(non_snake_case, clippy::missing_safety_doc)] |
| 8 | + |
| 9 | +use cuda_std::kernel; |
| 10 | +use optix_device::{ |
| 11 | + get_launch_index, |
| 12 | + glam::*, |
| 13 | + misc::*, |
| 14 | + payload, |
| 15 | + trace::TraversableHandle, |
| 16 | + trace::{trace, RayFlags}, |
| 17 | + util::*, |
| 18 | +}; |
| 19 | + |
| 20 | +#[derive(Debug, Clone, Copy)] |
| 21 | +#[repr(C)] |
| 22 | +pub struct LaunchParams { |
| 23 | + pub frame: Frame, |
| 24 | + pub camera: Camera, |
| 25 | + pub traversable: TraversableHandle, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Clone, Copy)] |
| 29 | +#[repr(C)] |
| 30 | +pub struct Frame { |
| 31 | + pub color_buf: *mut Vec4, |
| 32 | + pub size: UVec2, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Clone, Copy)] |
| 36 | +#[repr(C)] |
| 37 | +pub struct Camera { |
| 38 | + pub position: Vec3, |
| 39 | + pub direction: Vec3, |
| 40 | + pub horizontal: Vec3, |
| 41 | + pub vertical: Vec3, |
| 42 | +} |
| 43 | + |
| 44 | +#[repr(u32)] |
| 45 | +#[derive(Debug, Clone, Copy)] |
| 46 | +pub enum RayType { |
| 47 | + SurfaceRay = 0, |
| 48 | +} |
| 49 | + |
| 50 | +fn get_color_buf() -> *mut Vec4 { |
| 51 | + unsafe { unpack_pointer(payload::get_payload(0), payload::get_payload(1)) } |
| 52 | +} |
| 53 | + |
| 54 | +fn random_color(i: u32) -> Vec4 { |
| 55 | + let r = i * 13 * 17 + 0x234235; |
| 56 | + let g = i * 7 * 3 * 5 + 0x773477; |
| 57 | + let b = i * 11 * 19 + 0x223766; |
| 58 | + Vec4::new( |
| 59 | + (r & 255) as f32 / 255.0, |
| 60 | + (g & 255) as f32 / 255.0, |
| 61 | + (b & 255) as f32 / 255.0, |
| 62 | + 1.0, |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +#[kernel] |
| 67 | +pub unsafe fn __closesthit__radiance() { |
| 68 | + let prim_id = primitive_index(); |
| 69 | + let buf = get_color_buf(); |
| 70 | + *buf = random_color(prim_id); |
| 71 | +} |
| 72 | + |
| 73 | +#[kernel] |
| 74 | +pub unsafe fn __anyhit__radiance() {} |
| 75 | + |
| 76 | +#[kernel] |
| 77 | +pub unsafe fn __miss__radiance() { |
| 78 | + let buf = get_color_buf(); |
| 79 | + // pure white background |
| 80 | + *buf = Vec4::ONE; |
| 81 | +} |
| 82 | + |
| 83 | +extern "C" { |
| 84 | + #[cfg_attr(target_os = "cuda", nvvm_internal(addrspace(4)))] |
| 85 | + static PARAMS: LaunchParams; |
| 86 | +} |
| 87 | + |
| 88 | +#[kernel] |
| 89 | +pub unsafe fn __raygen__renderFrame() { |
| 90 | + let i = get_launch_index(); |
| 91 | + let i = UVec2::new(i.x, i.y); |
| 92 | + |
| 93 | + let camera = PARAMS.camera; |
| 94 | + |
| 95 | + let px_color = Vec3::ZERO; |
| 96 | + let (mut p0, mut p1) = pack_pointer(&px_color as *const _ as *mut Vec3); |
| 97 | + |
| 98 | + let screen = (i.as_vec2() + Vec2::splat(0.5)) / PARAMS.frame.size.as_vec2(); |
| 99 | + let ray_dir = (camera.direction |
| 100 | + + (screen.x - 0.5) * camera.horizontal |
| 101 | + + (screen.y - 0.5) * camera.vertical) |
| 102 | + .normalize(); |
| 103 | + |
| 104 | + trace( |
| 105 | + PARAMS.traversable, |
| 106 | + camera.position, |
| 107 | + ray_dir, |
| 108 | + 0.0, |
| 109 | + 1e20, |
| 110 | + 0.0, |
| 111 | + 255, |
| 112 | + RayFlags::DISABLE_ANYHIT, |
| 113 | + RayType::SurfaceRay as u32, |
| 114 | + 1, |
| 115 | + RayType::SurfaceRay as u32, |
| 116 | + &mut p0, |
| 117 | + &mut p1, |
| 118 | + ); |
| 119 | + |
| 120 | + let fb_index = i.x + i.y * PARAMS.frame.size.x; |
| 121 | + *PARAMS.frame.color_buf.add(fb_index as usize) = |
| 122 | + Vec4::new(px_color.x, px_color.y, px_color.z, 1.0); |
| 123 | +} |
0 commit comments