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>
This commit is contained in:
parent
1d2e12dbc9
commit
376e66aeed
6 changed files with 70 additions and 134 deletions
|
|
@ -1,35 +1,25 @@
|
|||
// Payload declared here so the WGSL assembler sees it before raygen
|
||||
// (the assembler concatenates closesthit/anyhit/miss BEFORE raygen).
|
||||
// Sponza closest-hit (runs in SHADE). In the wavefront model the lighting
|
||||
// + shadow trace that used to live in raygen happens here: gather surface
|
||||
// data, accumulate ambient, and emit a shadow ray toward the sun carrying
|
||||
// the pending direct contribution. The shadow ray's miss adds that
|
||||
// contribution (sun visible); its hit adds nothing (occluded), since
|
||||
// RT_FLAG_SKIP_CLOSEST_HIT suppresses closesthit on the shadow ray.
|
||||
//
|
||||
// WGSL forbids cycles in the function call graph, so closesthit_main
|
||||
// CAN'T call traceRay (that would create closesthit → traceRay →
|
||||
// runClosestHit → closesthit). The lighting + shadow trace therefore
|
||||
// happens in raygen; closesthit's job is just to gather surface data
|
||||
// into the payload.
|
||||
//
|
||||
// shadowRay = 0 (primary): closesthit fills albedo/worldPos/normal/hit.
|
||||
// shadowRay = 1 (shadow): closesthit is skipped (RT_FLAG_SKIP_CLOSEST_HIT),
|
||||
// miss flips color to white = "lit".
|
||||
// Payload declared here so the assembler sees it before wfPayload / SHADE.
|
||||
struct Payload {
|
||||
color: vec3<f32>,
|
||||
shadowRay: u32,
|
||||
worldPos: vec3<f32>,
|
||||
hit: u32,
|
||||
worldNormal: vec3<f32>,
|
||||
_pad: f32,
|
||||
color: vec3<f32>, // shadow ray: pending albedo·sun·nDotL
|
||||
shadowRay: u32, // 0 primary, 1 shadow
|
||||
};
|
||||
|
||||
// User-bound resources at group(2). Matches the UICustomBinding span the
|
||||
// host hands to PipelineRTWebGPU::Init.
|
||||
// binding 0 — albedo texture_2d_array, one layer per Sponza material
|
||||
// binding 1 — sampler (linear clamp)
|
||||
// binding 2 — camera storage buffer (read by raygen only)
|
||||
@group(2) @binding(0) var albedos : texture_2d_array<f32>;
|
||||
@group(2) @binding(1) var samp : sampler;
|
||||
// User resources at @group(3) (0..2 are the wavefront pipeline's reserved
|
||||
// groups). binding 0 albedo array, 1 sampler, 2 camera (raygen only).
|
||||
@group(3) @binding(0) var albedos : texture_2d_array<f32>;
|
||||
@group(3) @binding(1) var samp : sampler;
|
||||
|
||||
const SUN_DIR_TO_LIGHT: vec3<f32> = vec3<f32>(-0.35, 1.00, -0.20);
|
||||
const SUN_COLOR: vec3<f32> = vec3<f32>( 1.10, 1.00, 0.85);
|
||||
const AMBIENT_COLOR: vec3<f32> = vec3<f32>( 0.18, 0.20, 0.28);
|
||||
|
||||
// VertexNormalTangentUVPacked is `packed` on the outer struct but each
|
||||
// inner `Vector<float, N, 4>` is SIMD-aligned to a 16-byte stride. So
|
||||
// each vertex is 12 u32 words: normal at 0..2, tangent at 4..6, uv at 8..9.
|
||||
const ATTRIB_STRIDE_U32: u32 = 12u;
|
||||
const ATTRIB_NORMAL_OFFSET: u32 = 0u;
|
||||
const ATTRIB_UV_OFFSET: u32 = 8u;
|
||||
|
|
@ -52,7 +42,6 @@ fn fetchNormal(meshRec: MeshRecord, vertexIdx: u32) -> vec3<f32> {
|
|||
}
|
||||
|
||||
fn closesthit_main(ray: RayDesc, hit: HitInfo, payload: ptr<function, Payload>) {
|
||||
// Resolve hit triangle → 3 vertex indices.
|
||||
let meshIdx = tlasEntries[hit.instanceId].blasMeshIdx;
|
||||
let meshRec = meshRecords[meshIdx];
|
||||
let baseIdx = meshRec.indexOffset + hit.primitiveId * 3u;
|
||||
|
|
@ -61,19 +50,14 @@ fn closesthit_main(ray: RayDesc, hit: HitInfo, payload: ptr<function, Payload>)
|
|||
let i2 = indices[baseIdx + 2u];
|
||||
let bary = vec3<f32>(1.0 - hit.attribs.x - hit.attribs.y, hit.attribs.x, hit.attribs.y);
|
||||
|
||||
// Albedo via barycentric UV interpolation.
|
||||
let uv0 = fetchUV(meshRec, i0);
|
||||
let uv1 = fetchUV(meshRec, i1);
|
||||
let uv2 = fetchUV(meshRec, i2);
|
||||
let uv = uv0 * bary.x + uv1 * bary.y + uv2 * bary.z;
|
||||
// OBJ V is bottom-up; sampler is top-down. fract for manual tiling.
|
||||
let uvTiled = vec2<f32>(fract(uv.x), fract(1.0 - uv.y));
|
||||
let layer = i32(hit.customIndex);
|
||||
let albedo = textureSampleLevel(albedos, samp, uvTiled, layer, 0.0).rgb;
|
||||
|
||||
// World-space smooth shading normal. Multiply through the
|
||||
// object-to-world rotation so this stays correct if a future scene
|
||||
// rotates instances (Sponza itself is all identities).
|
||||
let n0 = fetchNormal(meshRec, i0);
|
||||
let n1 = fetchNormal(meshRec, i1);
|
||||
let n2 = fetchNormal(meshRec, i2);
|
||||
|
|
@ -83,8 +67,23 @@ fn closesthit_main(ray: RayDesc, hit: HitInfo, payload: ptr<function, Payload>)
|
|||
dot(hit.objectToWorldR1.xyz, nObj),
|
||||
dot(hit.objectToWorldR2.xyz, nObj)));
|
||||
|
||||
(*payload).color = albedo;
|
||||
(*payload).worldPos = ray.origin + ray.direction * hit.t;
|
||||
(*payload).worldNormal = nWorld;
|
||||
(*payload).hit = 1u;
|
||||
// Two-sided: flip the normal toward the camera (Sponza curtains have
|
||||
// inconsistent winding).
|
||||
let nFacing = select(-nWorld, nWorld, dot(nWorld, ray.direction) < 0.0);
|
||||
let lightDir = normalize(SUN_DIR_TO_LIGHT);
|
||||
let nDotL = max(0.0, dot(nFacing, lightDir));
|
||||
let worldPos = ray.origin + ray.direction * hit.t;
|
||||
|
||||
// Ambient is unconditional; direct light is gated behind the shadow ray.
|
||||
rtAccumulate(albedo * AMBIENT_COLOR);
|
||||
|
||||
if (nDotL > 0.0) {
|
||||
let shadowOrigin = worldPos + nFacing * 0.5;
|
||||
var sp: Payload;
|
||||
sp.color = albedo * SUN_COLOR * nDotL;
|
||||
sp.shadowRay = 1u;
|
||||
rtEmitRay(shadowOrigin, 0.001, lightDir, 10000.0,
|
||||
RT_FLAG_SKIP_CLOSEST_HIT | RT_FLAG_TERMINATE_ON_FIRST_HIT,
|
||||
0xFFu, 0u, 0u, sp);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue