diff --git a/examples/Sponza/closesthit.wgsl b/examples/Sponza/closesthit.wgsl index 9fa15a8..293f8ba 100644 --- a/examples/Sponza/closesthit.wgsl +++ b/examples/Sponza/closesthit.wgsl @@ -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, - shadowRay: u32, - worldPos: vec3, - hit: u32, - worldNormal: vec3, - _pad: f32, + color: vec3, // 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; -@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; +@group(3) @binding(1) var samp : sampler; + +const SUN_DIR_TO_LIGHT: vec3 = vec3(-0.35, 1.00, -0.20); +const SUN_COLOR: vec3 = vec3( 1.10, 1.00, 0.85); +const AMBIENT_COLOR: vec3 = vec3( 0.18, 0.20, 0.28); -// VertexNormalTangentUVPacked is `packed` on the outer struct but each -// inner `Vector` 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 { } fn closesthit_main(ray: RayDesc, hit: HitInfo, payload: ptr) { - // 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) let i2 = indices[baseIdx + 2u]; let bary = vec3(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(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) 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); + } } diff --git a/examples/Sponza/main.cpp b/examples/Sponza/main.cpp index 73db99e..1df3a2c 100644 --- a/examples/Sponza/main.cpp +++ b/examples/Sponza/main.cpp @@ -253,10 +253,11 @@ int main() { DescriptorHeapWebGPU heap; heap.Initialize(/*images*/ 2, /*buffers*/ 2, /*samplers*/ 2); - std::array shaders {{ + std::array shaders {{ WebGPUShader(fs::path("raygen.wgsl"), "raygen_main", WebGPURTStage::Raygen), WebGPUShader(fs::path("miss.wgsl"), "miss_main", WebGPURTStage::Miss), WebGPUShader(fs::path("closesthit.wgsl"), "closesthit_main", WebGPURTStage::ClosestHit), + WebGPUShader(fs::path("resolve.wgsl"), "resolve_main", WebGPURTStage::Resolve), }}; ShaderBindingTableWebGPU sbt; sbt.Init(shaders); @@ -271,14 +272,15 @@ int main() { { .type = RTShaderGroupType::TrianglesHitGroup, .closestHitShader = 2 }, }}; - // Three user bindings at @group(2): + // Three user bindings at @group(3) (the wavefront pipeline reserves + // groups 0..2 for WfParams / data heaps / indirect args): // binding 0 — albedo texture_2d_array (one layer per material) // binding 1 — sampler (linear clamp) // binding 2 — Camera storage buffer (host-driven, updated per frame) std::array bindings {{ - { .group = 2, .binding = 0, .kind = UICustomBindingKind::SampledTextureArray, ._pad = 0, .pushOffset = 0 }, - { .group = 2, .binding = 1, .kind = UICustomBindingKind::Sampler, ._pad = 0, .pushOffset = 0 }, - { .group = 2, .binding = 2, .kind = UICustomBindingKind::Buffer, ._pad = 0, .pushOffset = 0 }, + { .group = 3, .binding = 0, .kind = UICustomBindingKind::SampledTextureArray, ._pad = 0, .pushOffset = 0 }, + { .group = 3, .binding = 1, .kind = UICustomBindingKind::Sampler, ._pad = 0, .pushOffset = 0 }, + { .group = 3, .binding = 2, .kind = UICustomBindingKind::Buffer, ._pad = 0, .pushOffset = 0 }, }}; PipelineRTWebGPU pipeline; @@ -367,6 +369,7 @@ int main() { RTPass rtPass(&pipeline); rtPass.handlesPtr = userHandles.data(); rtPass.handlesCount = static_cast(userHandles.size()); + rtPass.maxDepth = 2; // primary + shadow window.passes.push_back(&rtPass); // ── Free camera: WASD + mouse-delta look ─────────────────────────── @@ -375,9 +378,10 @@ int main() { // height, looking +X down the long axis (bbox: X[-1921..1800], // Y[-126..1429], Z[-1183..1105]). The user can fine-tune from there. struct CamState { - Vector position{ -1500.0f, 200.0f, 0.0f }; - float yaw = 0.0f; // radians, around world +Y - float pitch = 0.0f; // radians, +pitch looks up + // 3/4 view from a corner aimed at the atrium centre. + Vector position{ -1400.0f, 700.0f, -600.0f }; + float yaw = 0.405f; // radians, around world +Y + float pitch = -0.317f; // radians, +pitch looks up } cam; Input::Map inputMap; diff --git a/examples/Sponza/miss.wgsl b/examples/Sponza/miss.wgsl index 39ff71d..0bbbf4d 100644 --- a/examples/Sponza/miss.wgsl +++ b/examples/Sponza/miss.wgsl @@ -1,16 +1,12 @@ +// Sponza miss (runs in SHADE). Primary miss → two-stop sky gradient. +// Shadow miss → the sun is unoccluded, so add the pending direct term. fn miss_main(ray: RayDesc, payload: ptr) { if ((*payload).shadowRay == 1u) { - // Shadow ray escaped to infinity — the sun is visible from the - // origin, so the surface there should pick up full direct light. - // raygen reads color.x as the visibility coefficient. - (*payload).color = vec3(1.0); + rtAccumulate((*payload).color); return; } - - // Primary miss: cheap two-stop sky gradient. (*payload).hit stays 0 - // so raygen knows to skip the lighting path and just use this color. let t = clamp(ray.direction.y * 0.5 + 0.5, 0.0, 1.0); let sky = vec3(0.45, 0.65, 0.95); let zenith = vec3(0.95, 0.85, 0.65); - (*payload).color = mix(sky, zenith, t); + rtAccumulate(mix(sky, zenith, t)); } diff --git a/examples/Sponza/project.cpp b/examples/Sponza/project.cpp index b850c90..400b0c2 100644 --- a/examples/Sponza/project.cpp +++ b/examples/Sponza/project.cpp @@ -82,6 +82,7 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.files.emplace_back(fs::path("raygen.wgsl")); cfg.files.emplace_back(fs::path("closesthit.wgsl")); cfg.files.emplace_back(fs::path("miss.wgsl")); + cfg.files.emplace_back(fs::path("resolve.wgsl")); EnableWasiBrowserRuntime(cfg); } else { cfg.shaders.emplace_back(fs::path("raygen.glsl"), std::string("main"), ShaderType::RayGen); diff --git a/examples/Sponza/raygen.wgsl b/examples/Sponza/raygen.wgsl index a90ca84..6575b34 100644 --- a/examples/Sponza/raygen.wgsl +++ b/examples/Sponza/raygen.wgsl @@ -1,12 +1,8 @@ -// WebGPU raygen. Camera state comes from the host every frame via a -// storage buffer bound at @group(2) @binding(2); main.cpp drives that -// from WASD + mouse-delta through Crafter::Input. -// -// The shading + shadow trace all happens here because WGSL forbids -// recursive function call graphs — closesthit_main can't call traceRay -// (that would loop closesthit → traceRay → runClosestHit → closesthit). -// Raygen is the entry point and not called by anyone, so it can call -// traceRay twice (once primary, once shadow) without forming a cycle. +// 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, @@ -18,92 +14,25 @@ struct Camera { forward: vec3, pad1: f32, }; -@group(2) @binding(2) var camera : Camera; - -// Sun coming through Sponza's open roof. Y is up; this points "down and -// slightly along +X" so the light grazes the colonnades on one side. -const SUN_DIR_TO_LIGHT: vec3 = vec3(-0.35, 1.00, -0.20); -const SUN_COLOR: vec3 = vec3( 1.10, 1.00, 0.85); -const AMBIENT_COLOR: vec3 = vec3( 0.18, 0.20, 0.28); +@group(3) @binding(2) var camera : Camera; fn raygen_main(gid: vec3) { - if (gid.x >= hdr.surfaceW || gid.y >= hdr.surfaceH) { return; } + if (gid.x >= wfParams.surfaceW || gid.y >= wfParams.surfaceH) { return; } let pixel = vec2(f32(gid.x), f32(gid.y)); - let resolution = vec2(f32(hdr.surfaceW), f32(hdr.surfaceH)); + let resolution = vec2(f32(wfParams.surfaceW), f32(wfParams.surfaceH)); let uv = (pixel + vec2(0.5)) / resolution; let ndc = uv * 2.0 - vec2(1.0); - // Pinhole camera reconstructed from the host basis. ndc.x runs left- - // to-right across the screen → +right; ndc.y is top-down so we - // negate before applying +up. let direction = normalize( camera.right * (ndc.x * camera.aspect * camera.tanHalf) + camera.up * (-ndc.y * camera.tanHalf) + camera.forward); - // ── Primary ray ──────────────────────────────────────────────────── var payload: Payload; payload.color = vec3(0.0); payload.shadowRay = 0u; - payload.hit = 0u; - traceRay( - 0u, 0u, 0xFFu, - 0u, 0u, 0u, - camera.origin, 0.001, - direction, 10000.0, - &payload); - - var finalColor: vec3; - if (payload.hit == 1u) { - // Closesthit filled albedo/worldPos/worldNormal. Two-sided - // shading: flip the normal toward the camera if we hit the back - // face — Sponza's curtains in particular have inconsistent - // winding, and without this half the surface would go black. - let albedo = payload.color; - let nFacing = select(-payload.worldNormal, - payload.worldNormal, - dot(payload.worldNormal, direction) < 0.0); - let lightDir = normalize(SUN_DIR_TO_LIGHT); - let nDotL = max(0.0, dot(nFacing, lightDir)); - - // ── Shadow ray ──────────────────────────────────────────────── - // Only worth tracing if the surface faces the sun at all. - var visibility = 0.0; - if (nDotL > 0.0) { - // Normal-offset bias on Sponza's units (~3700 wide atrium) - // is hefty; 0.5 keeps the shadow ray clear of the originating - // triangle without producing visible "floating" shadows. - let shadowOrigin = payload.worldPos + nFacing * 0.5; - - var shadowPayload: Payload; - shadowPayload.color = vec3(0.0); // default: blocked - shadowPayload.shadowRay = 1u; - shadowPayload.hit = 0u; - traceRay( - 0u, - RT_FLAG_SKIP_CLOSEST_HIT | RT_FLAG_TERMINATE_ON_FIRST_HIT, - 0xFFu, - 0u, 0u, 0u, - shadowOrigin, 0.001, - lightDir, 10000.0, - &shadowPayload); - visibility = shadowPayload.color.x; - } - - let lit = AMBIENT_COLOR + SUN_COLOR * (nDotL * visibility); - finalColor = albedo * lit; - } else { - // Sky color was filled by miss_main. - finalColor = payload.color; - } - - // Reinhard tonemap + gamma 2.2 so sun-lit albedos don't clip and - // shadow detail stays readable. - let mapped = finalColor / (finalColor + vec3(1.0)); - let gamma = pow(mapped, vec3(1.0 / 2.2)); - textureStore(outImage, - vec2(i32(gid.x), i32(gid.y)), - vec4(gamma, 1.0)); + rtEmitPrimaryRay(camera.origin, 0.001, direction, 10000.0, + 0u, 0xFFu, 0u, 0u, payload); } diff --git a/examples/Sponza/resolve.wgsl b/examples/Sponza/resolve.wgsl new file mode 100644 index 0000000..346659e --- /dev/null +++ b/examples/Sponza/resolve.wgsl @@ -0,0 +1,7 @@ +// Sponza RESOLVE-stage tonemap: Reinhard + gamma 2.2 over the linear +// accumulator — matches the tonemap the megakernel raygen applied inline. +fn resolve_main(coord: vec2, hdr: vec4) -> vec4 { + let mapped = hdr.rgb / (hdr.rgb + vec3(1.0)); + let g = pow(mapped, vec3(1.0 / 2.2)); + return vec4(g, 1.0); +}