Crafter.Graphics/examples/Sponza/raygen.wgsl
catbot 376e66aeed WebGPU RT: port Sponza to wavefront (shadow ray in SHADE)
Restructure Sponza for the wavefront model: raygen emits the primary ray;
closesthit (in SHADE) gathers albedo/normal, accumulates ambient, and
emits a shadow ray carrying the pending direct term; miss adds the sky
(primary) or the direct term (shadow miss). resolve.wgsl applies the same
Reinhard+gamma the megakernel raygen did inline. User bindings moved to
group 3 (groups 0..2 reserved). RTPass maxDepth=2.

Renders the atrium correctly through the wavefront pipeline (textures,
two-sided shading, sun+ambient, shadows, tonemap).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 20:16:04 +00:00

38 lines
1.3 KiB
WebGPU Shading Language

// Sponza raygen (runs in GENERATE). Emits the pixel's primary ray; all
// shading + the shadow trace now happen in SHADE (closesthit/miss). Camera
// state comes from the host each frame via a storage buffer at
// @group(3) @binding(2) (groups 0..2 are reserved by the wavefront
// pipeline). main.cpp drives it from WASD + mouse-delta.
struct Camera {
origin: vec3<f32>,
pad0: f32,
right: vec3<f32>,
tanHalf: f32,
up: vec3<f32>,
aspect: f32,
forward: vec3<f32>,
pad1: f32,
};
@group(3) @binding(2) var<storage, read> camera : Camera;
fn raygen_main(gid: vec3<u32>) {
if (gid.x >= wfParams.surfaceW || gid.y >= wfParams.surfaceH) { return; }
let pixel = vec2<f32>(f32(gid.x), f32(gid.y));
let resolution = vec2<f32>(f32(wfParams.surfaceW), f32(wfParams.surfaceH));
let uv = (pixel + vec2<f32>(0.5)) / resolution;
let ndc = uv * 2.0 - vec2<f32>(1.0);
let direction = normalize(
camera.right * (ndc.x * camera.aspect * camera.tanHalf) +
camera.up * (-ndc.y * camera.tanHalf) +
camera.forward);
var payload: Payload;
payload.color = vec3<f32>(0.0);
payload.shadowRay = 0u;
rtEmitPrimaryRay(camera.origin, 0.001, direction, 10000.0,
0u, 0xFFu, 0u, 0u, payload);
}