// 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);
}
