From 5dd1086f0893cf1ddaacfbd326d1d9a19c4c4f3e Mon Sep 17 00:00:00 2001 From: catbot Date: Tue, 2 Jun 2026 22:09:30 +0000 Subject: [PATCH] docs(webgpu-rt): add RTVolume example (procedural spheres + any-hit cut-out) A 3x3x3 grid of AABB-geometry spheres rendered through an analytic ray-sphere intersection shader, with an any-hit spherical-checkerboard cut-out so the background shows through. Exercises both features end to end on the WebGPU wavefront tracer. Co-Authored-By: Claude Opus 4.8 --- README.md | 6 +- examples/RTVolume/README.md | 26 ++++ examples/RTVolume/anyhit.wgsl | 24 ++++ examples/RTVolume/closesthit.wgsl | 37 ++++++ examples/RTVolume/intersection.wgsl | 33 +++++ examples/RTVolume/main.cpp | 199 ++++++++++++++++++++++++++++ examples/RTVolume/miss.wgsl | 7 + examples/RTVolume/project.cpp | 48 +++++++ examples/RTVolume/raygen.wgsl | 34 +++++ examples/RTVolume/resolve.wgsl | 7 + 10 files changed, 420 insertions(+), 1 deletion(-) create mode 100644 examples/RTVolume/README.md create mode 100644 examples/RTVolume/anyhit.wgsl create mode 100644 examples/RTVolume/closesthit.wgsl create mode 100644 examples/RTVolume/intersection.wgsl create mode 100644 examples/RTVolume/main.cpp create mode 100644 examples/RTVolume/miss.wgsl create mode 100644 examples/RTVolume/project.cpp create mode 100644 examples/RTVolume/raygen.wgsl create mode 100644 examples/RTVolume/resolve.wgsl diff --git a/README.md b/README.md index 8778c52..ac40143 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,11 @@ The two backends share the same C++ surface for the high-level pieces (`*Vulkan` vs `*WebGPU`) live behind `#ifdef CRAFTER_GRAPHICS_WINDOW_DOM`. Vulkan ray tracing is hardware (`VK_KHR_ray_tracing_pipeline`); WebGPU ray tracing is a library-built software path (BVH + traceRay in a -compute pipeline composed from user-supplied WGSL stages). +compute pipeline composed from user-supplied WGSL stages). The WebGPU +path supports triangle and AABB (procedural, `VK_GEOMETRY_TYPE_AABBS_KHR`) +geometry, closest-hit / miss / any-hit / intersection shaders — see +[examples/RTVolume](examples/RTVolume/README.md) for procedural spheres +shaded through an intersection shader with an any-hit cut-out. > **Native RT status:** reading an acceleration structure through > `VK_EXT_descriptor_heap` currently aborts with `VK_ERROR_DEVICE_LOST` on diff --git a/examples/RTVolume/README.md b/examples/RTVolume/README.md new file mode 100644 index 0000000..2c73e7b --- /dev/null +++ b/examples/RTVolume/README.md @@ -0,0 +1,26 @@ +# RTVolume + +WebGPU software ray tracing of **procedural (AABB) geometry** with an +**any-hit** cut-out — the two features added for issue #13. + +A 3×3×3 grid of unit boxes is registered as an AABB BLAS +(`Mesh::BuildProcedural`, the WebGPU analog of `VK_GEOMETRY_TYPE_AABBS_KHR`). +The hit group is a `RTShaderGroupType::ProceduralHitGroup` carrying: + +- `intersection.wgsl` — analytic ray–sphere test that turns each box into a + radius-1 sphere (runs in TRACE, once per box the ray enters); +- `anyhit.wgsl` — returns `RT_ANYHIT_IGNORE` for half the cells of a + spherical checkerboard, so the ray passes through and the background / + spheres behind show through (the visible proof any-hit runs); +- `closesthit.wgsl` — normal-based Lambert shading, tinted per instance. + +The geometry is registered **non-opaque** and the instances clear their +force-opaque flag, which is what lets the any-hit shader run. Flip the +instance flag to `kRTGeometryInstanceForceOpaque` (or build the mesh with +`opaque = true`) to skip any-hit and see solid spheres. + +WebGPU/DOM only: + +``` +crafter-build --target=wasm32-wasip1 -r +``` diff --git a/examples/RTVolume/anyhit.wgsl b/examples/RTVolume/anyhit.wgsl new file mode 100644 index 0000000..88fc21d --- /dev/null +++ b/examples/RTVolume/anyhit.wgsl @@ -0,0 +1,24 @@ +// RTVolume any-hit shader (runs in TRACE on every candidate sphere hit, +// because the geometry is registered non-opaque). Punches a spherical +// checkerboard of holes: for half the cells it returns RT_ANYHIT_IGNORE, +// so the ray passes straight through and the background / spheres behind +// show through. Returning RT_ANYHIT_ACCEPT keeps the hit. This is the +// visible proof the any-hit path runs — with it the spheres are perforated, +// without it they would be solid. +fn anyhit_main(ray: RayDesc, hit: HitInfo, payload: ptr) -> u32 { + // Object-space hit point on the unit sphere → its normal/direction. + let posObj = hit.objectRayOrigin + hit.objectRayDirection * hit.t; + let n = normalize(posObj); + + let PI = 3.14159265; + let longitude = atan2(n.z, n.x); // [-PI, PI] + let latitude = asin(clamp(n.y, -1.0, 1.0)); // [-PI/2, PI/2] + + let cu = i32(floor((longitude + PI) / PI * 6.0)); + let cv = i32(floor((latitude + PI * 0.5) / PI * 6.0)); + + if (((cu + cv) & 1) == 0) { + return RT_ANYHIT_IGNORE; // cut-out cell — see through + } + return RT_ANYHIT_ACCEPT; +} diff --git a/examples/RTVolume/closesthit.wgsl b/examples/RTVolume/closesthit.wgsl new file mode 100644 index 0000000..4597101 --- /dev/null +++ b/examples/RTVolume/closesthit.wgsl @@ -0,0 +1,37 @@ +// RTVolume closest-hit (runs in SHADE). Shades the procedural sphere by +// its surface normal with a fixed sun + ambient, tinted per instance. +// +// Payload declared here so the assembler sees it before wfPayload / SHADE. +struct Payload { + color: vec3, +}; + +const SUN_DIR_TO_LIGHT: vec3 = vec3(0.40, 0.85, 0.35); +const SUN_COLOR: vec3 = vec3(1.20, 1.10, 0.95); +const AMBIENT_COLOR: vec3 = vec3(0.16, 0.18, 0.24); + +fn instanceAlbedo(i: u32) -> vec3 { + let h = i * 2654435761u; + return vec3( + 0.35 + 0.6 * f32((h >> 0u) & 255u) / 255.0, + 0.35 + 0.6 * f32((h >> 8u) & 255u) / 255.0, + 0.35 + 0.6 * f32((h >> 16u) & 255u) / 255.0); +} + +fn closesthit_main(ray: RayDesc, hit: HitInfo, payload: ptr) { + // Object-space hit point on the unit sphere is its object-space normal. + let posObj = hit.objectRayOrigin + hit.objectRayDirection * hit.t; + let nObj = normalize(posObj); + let nWorld = normalize(vec3( + dot(hit.objectToWorldR0.xyz, nObj), + dot(hit.objectToWorldR1.xyz, nObj), + dot(hit.objectToWorldR2.xyz, nObj))); + + let albedo = instanceAlbedo(hit.customIndex); + let viewDir = -ray.direction; + let nFacing = select(-nWorld, nWorld, dot(nWorld, viewDir) > 0.0); + let sunDir = normalize(SUN_DIR_TO_LIGHT); + let nDotL = max(0.0, dot(nFacing, sunDir)); + + rtAccumulate(albedo * (AMBIENT_COLOR + SUN_COLOR * nDotL)); +} diff --git a/examples/RTVolume/intersection.wgsl b/examples/RTVolume/intersection.wgsl new file mode 100644 index 0000000..1139668 --- /dev/null +++ b/examples/RTVolume/intersection.wgsl @@ -0,0 +1,33 @@ +// RTVolume intersection shader (runs in TRACE, per AABB the ray enters). +// Analytic ray-sphere test: the unit box [-1,1]^3 is treated as the +// bounding volume of a sphere of radius 1 centred at the box centre. The +// ray is in object space and is NOT normalised (it is worldToObject * +// worldRay), so the returned t is directly comparable to the world-space +// ray parameter the tracer commits — solve the quadratic with the general +// a = dot(d,d) form rather than assuming |d| == 1. +fn intersection_main(ray: RayDesc, aabbMin: vec3, aabbMax: vec3, + primitiveId: u32) -> IntersectionResult { + var r: IntersectionResult; + r.hit = false; + + let center = (aabbMin + aabbMax) * 0.5; + let radius = (aabbMax.x - aabbMin.x) * 0.5; + + let oc = ray.origin - center; + let a = dot(ray.direction, ray.direction); + let b = 2.0 * dot(oc, ray.direction); + let c = dot(oc, oc) - radius * radius; + let disc = b * b - 4.0 * a * c; + if (disc < 0.0) { return r; } + + let sq = sqrt(disc); + var t = (-b - sq) / (2.0 * a); // near root + if (t < ray.tMin) { t = (-b + sq) / (2.0 * a); } // fall back to far root + if (t < ray.tMin || t > ray.tMax) { return r; } + + r.hit = true; + r.t = t; + r.attribs = vec2(0.0); + r.hitKind = 0u; + return r; +} diff --git a/examples/RTVolume/main.cpp b/examples/RTVolume/main.cpp new file mode 100644 index 0000000..48cbed2 --- /dev/null +++ b/examples/RTVolume/main.cpp @@ -0,0 +1,199 @@ +// RTVolume — procedural (AABB) ray tracing on the WebGPU wavefront tracer. +// Demonstrates the two features this example was written to exercise: +// +// * VK_GEOMETRY_TYPE_AABBS_KHR equivalent — a BLAS built from AABBs +// (Mesh::BuildProcedural) whose surface is supplied by an intersection +// shader (here an analytic ray–sphere test). The boxes are unit cubes +// [-1,1]^3; the intersection shader turns each into a sphere. +// +// * any-hit — the spheres are registered non-opaque, and an any-hit +// shader punches a spherical checkerboard of holes by returning +// RT_ANYHIT_IGNORE for half the cells. Without any-hit the spheres are +// solid; with it you can see the background (and other spheres) +// through the cut-out cells. +// +// A 3×3×3 grid of these procedural spheres is shaded by surface normal + +// a fixed sun. WebGPU/DOM only — this is the software RT path. + +#ifndef CRAFTER_GRAPHICS_WINDOW_DOM +int main() { return 0; } // native path is hardware RT; out of scope here +#else + +import Crafter.Graphics; +import Crafter.Math; +import Crafter.Event; +import std; + +using namespace Crafter; +namespace fs = std::filesystem; + +namespace { + constexpr int kGrid = 3; + constexpr float kSpacing = 3.0f; + + struct CameraGPU { + float origin[3]; float pad0; + float right[3]; float tanHalf; + float up[3]; float aspect; + float forward[3]; float pad1; + }; + static_assert(sizeof(CameraGPU) == 64); +} + +int main() { + const int instanceCount = kGrid * kGrid * kGrid; + std::println("[RTVolume] grid {}^3 = {} procedural spheres", kGrid, instanceCount); + + Device::Initialize(); + static Window window(1280, 720, "RTVolume"); + auto cmd = window.StartInit(); + + DescriptorHeapWebGPU heap; + heap.Initialize(/*images*/ 1, /*buffers*/ 2, /*samplers*/ 1); + + // SBT order fixes the shader indices used by the groups below. + 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("anyhit.wgsl"), "anyhit_main", WebGPURTStage::AnyHit), + WebGPUShader(fs::path("intersection.wgsl"), "intersection_main", WebGPURTStage::Intersection), + WebGPUShader(fs::path("resolve.wgsl"), "resolve_main", WebGPURTStage::Resolve), + }}; + ShaderBindingTableWebGPU sbt; + sbt.Init(shaders); + + std::array raygenGroups {{ { .type = RTShaderGroupType::General, .generalShader = 0 } }}; + std::array missGroups {{ { .type = RTShaderGroupType::General, .generalShader = 1 } }}; + // One procedural hit group: closest-hit + any-hit + intersection. + std::array hitGroups {{ { + .type = RTShaderGroupType::ProceduralHitGroup, + .closestHitShader = 2, + .anyHitShader = 3, + .intersectionShader = 4, + } }}; + + std::array bindings {{ + { .group = 3, .binding = 0, .kind = UICustomBindingKind::Buffer, ._pad = 0, .pushOffset = 0 }, + }}; + + PipelineRTWebGPU pipeline; + pipeline.Init(cmd, raygenGroups, missGroups, hitGroups, sbt, bindings); + + // ── One procedural unit-box BLAS. The intersection shader treats the + // box as the bounding volume of a radius-1 sphere centred at the + // object origin. opaque=false so the any-hit cut-out runs. ───────── + static std::array boxes {{ + { .min = {-1.0f, -1.0f, -1.0f}, .max = {1.0f, 1.0f, 1.0f} }, + }}; + static Mesh sphere; + sphere.BuildProcedural(boxes, /*opaque*/ false, cmd); + + // ── Camera buffer + handle array. ───────────────────────────────── + WebGPUBuffer cameraBuf; + cameraBuf.Create(1); + static std::array userHandles { cameraBuf.handle }; + + // ── Instance grid. ──────────────────────────────────────────────── + static std::vector renderers; + renderers.reserve(static_cast(instanceCount)); + const float origin0 = -0.5f * static_cast(kGrid - 1) * kSpacing; + for (int x = 0; x < kGrid; ++x) + for (int y = 0; y < kGrid; ++y) + for (int z = 0; z < kGrid; ++z) { + renderers.emplace_back(); + RenderingElement3D& r = renderers.back(); + auto& tx = r.instance.transform.matrix; + tx[0][0] = 1; tx[0][1] = 0; tx[0][2] = 0; tx[0][3] = origin0 + float(x) * kSpacing; + tx[1][0] = 0; tx[1][1] = 1; tx[1][2] = 0; tx[1][3] = origin0 + float(y) * kSpacing; + tx[2][0] = 0; tx[2][1] = 0; tx[2][2] = 1; tx[2][3] = origin0 + float(z) * kSpacing; + r.instance.instanceCustomIndex = static_cast(renderers.size() - 1); + r.instance.mask = 0xFF; + r.instance.instanceShaderBindingTableRecordOffset = 0; + // flags = 0: do NOT force opaque, so the any-hit shader runs. + r.instance.flags = 0; + r.instance.accelerationStructureReference = sphere.blasAddr; + RenderingElement3D::Add(&r); + } + RenderingElement3D::BuildTLAS(cmd, 0); + + window.descriptorHeap = &heap; + window.FinishInit(); + + RTPass rtPass(&pipeline); + rtPass.handlesPtr = userHandles.data(); + rtPass.handlesCount = static_cast(userHandles.size()); + rtPass.maxDepth = 1; // primary only + window.passes.push_back(&rtPass); + + // ── Free camera framing the grid. ───────────────────────────────── + const float ext = float(kGrid - 1) * kSpacing; + struct CamState { + Vector position; + float yaw; + float pitch; + } cam { + Vector{ ext * 1.1f, ext * 0.8f, ext * 1.6f }, + 0.0f, 0.0f, + }; + { + Vector d { -cam.position.x, -cam.position.y, -cam.position.z }; + const float len = std::sqrt(d.x*d.x + d.y*d.y + d.z*d.z); + cam.yaw = std::atan2(d.z, d.x); + cam.pitch = std::asin(d.y / len); + } + + Input::Map inputMap; + Input::Action& moveAct = inputMap.AddAction("Move", Input::ActionType::Vector2); + Input::Action& lookAct = inputMap.AddAction("Look", Input::ActionType::Vector2); + moveAct.bindings = { Input::WASDBind{ + Key(CrafterKeys::W), Key(CrafterKeys::S), Key(CrafterKeys::A), Key(CrafterKeys::D) } }; + lookAct.bindings = { Input::MouseDeltaBind{ 1.0f } }; + inputMap.Attach(window); + + const float kMoveSpeed = ext * 0.8f + 1.0f; + const float kLookSens = 0.05f; + const float kDt = 1.0f / 60.0f; + + EventListener camTick(&window.onBeforeUpdate, [&]() { + inputMap.Tick(); + cam.yaw += lookAct.vector2.x * kLookSens; + cam.pitch -= lookAct.vector2.y * kLookSens; + cam.pitch = std::clamp(cam.pitch, -1.55f, 1.55f); + + const float cp = std::cos(cam.pitch), sp = std::sin(cam.pitch); + const float cy = std::cos(cam.yaw), sy = std::sin(cam.yaw); + Vector forward { cp * cy, sp, cp * sy }; + Vector worldUp { 0.0f, 1.0f, 0.0f }; + Vector right { forward.y*worldUp.z - forward.z*worldUp.y, + forward.z*worldUp.x - forward.x*worldUp.z, + forward.x*worldUp.y - forward.y*worldUp.x }; + const float rLen = std::sqrt(right.x*right.x + right.y*right.y + right.z*right.z); + right.x /= rLen; right.y /= rLen; right.z /= rLen; + Vector up { right.y*forward.z - right.z*forward.y, + right.z*forward.x - right.x*forward.z, + right.x*forward.y - right.y*forward.x }; + + const float dx = moveAct.vector2.x * kMoveSpeed * kDt; + const float dy = moveAct.vector2.y * kMoveSpeed * kDt; + cam.position.x += right.x*dx + forward.x*dy; + cam.position.y += right.y*dx + forward.y*dy; + cam.position.z += right.z*dx + forward.z*dy; + + CameraGPU& g = cameraBuf.value[0]; + g.origin[0]=cam.position.x; g.origin[1]=cam.position.y; g.origin[2]=cam.position.z; g.pad0=0; + g.right[0]=right.x; g.right[1]=right.y; g.right[2]=right.z; + g.up[0]=up.x; g.up[1]=up.y; g.up[2]=up.z; + g.forward[0]=forward.x; g.forward[1]=forward.y; g.forward[2]=forward.z; + g.aspect = float(window.width) / float(window.height); + g.tanHalf = std::tan(70.0f * 3.14159265f / 360.0f); + g.pad1 = 0; + cameraBuf.FlushDevice(); + }); + + window.Render(); + window.StartUpdate(); + window.StartSync(); + return 0; +} +#endif diff --git a/examples/RTVolume/miss.wgsl b/examples/RTVolume/miss.wgsl new file mode 100644 index 0000000..533d437 --- /dev/null +++ b/examples/RTVolume/miss.wgsl @@ -0,0 +1,7 @@ +// RTVolume miss (runs in SHADE). Vertical sky gradient — also what shows +// through the any-hit cut-out cells. +fn miss_main(ray: RayDesc, payload: ptr) { + let t = clamp(ray.direction.y * 0.5 + 0.5, 0.0, 1.0); + rtAccumulate(mix(vec3(0.05, 0.07, 0.12), + vec3(0.45, 0.60, 0.85), t)); +} diff --git a/examples/RTVolume/project.cpp b/examples/RTVolume/project.cpp new file mode 100644 index 0000000..53200c2 --- /dev/null +++ b/examples/RTVolume/project.cpp @@ -0,0 +1,48 @@ +import std; +import Crafter.Build; +namespace fs = std::filesystem; +using namespace Crafter; + +extern "C" Configuration CrafterBuildProject(std::span args) { + bool isWasm = false; + for (std::string_view a : args) { + if (a.starts_with("--target=") && a.find("wasm") != std::string_view::npos) { + isWasm = true; + break; + } + } + + std::vector graphicsArgs(args.begin(), args.end()); + Configuration* graphics = LocalProject({ + .projectFile = "../../project.cpp", + .args = graphicsArgs, + }); + + Configuration cfg; + cfg.path = "./"; + cfg.name = "RTVolume"; + cfg.outputName = "RTVolume"; + cfg.type = ConfigurationType::Executable; + if (isWasm) { + cfg.target = "wasm32-wasip1"; + cfg.defines.push_back({"CRAFTER_GRAPHICS_WINDOW_DOM", ""}); + cfg.compileFlags.push_back("-msimd128"); + } + ApplyStandardArgs(cfg, args); + cfg.dependencies = { graphics }; + + std::array ifaces = {}; + std::array impls = { "main" }; + cfg.GetInterfacesAndImplementations(ifaces, impls); + + if (isWasm) { + cfg.files.emplace_back(fs::path("raygen.wgsl")); + cfg.files.emplace_back(fs::path("intersection.wgsl")); + cfg.files.emplace_back(fs::path("anyhit.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); + } + return cfg; +} diff --git a/examples/RTVolume/raygen.wgsl b/examples/RTVolume/raygen.wgsl new file mode 100644 index 0000000..24fde0d --- /dev/null +++ b/examples/RTVolume/raygen.wgsl @@ -0,0 +1,34 @@ +// RTVolume raygen (runs in GENERATE). Host-driven pinhole camera at +// @group(3) (groups 0..2 are reserved by the wavefront pipeline: +// 0 = WfParams, 1 = data heaps, 2 = indirect args). +struct Camera { + origin: vec3, + pad0: f32, + right: vec3, + tanHalf: f32, + up: vec3, + aspect: f32, + forward: vec3, + pad1: f32, +}; +@group(3) @binding(0) var camera : Camera; + +fn raygen_main(gid: vec3) { + if (gid.x >= wfParams.surfaceW || gid.y >= wfParams.surfaceH) { return; } + + let pixelf = vec2(f32(gid.x), f32(gid.y)); + let res = vec2(f32(wfParams.surfaceW), f32(wfParams.surfaceH)); + let uv = (pixelf + vec2(0.5)) / res; + let ndc = uv * 2.0 - vec2(1.0); + + let direction = normalize( + camera.right * (ndc.x * camera.aspect * camera.tanHalf) + + camera.up * (-ndc.y * camera.tanHalf) + + camera.forward); + + var p: Payload; + p.color = vec3(0.0); + + rtEmitPrimaryRay(camera.origin, 0.01, direction, 100000.0, + 0u, 0xFFu, 0u, 0u, p); +} diff --git a/examples/RTVolume/resolve.wgsl b/examples/RTVolume/resolve.wgsl new file mode 100644 index 0000000..260a033 --- /dev/null +++ b/examples/RTVolume/resolve.wgsl @@ -0,0 +1,7 @@ +// RTVolume RESOLVE-stage tonemap: Reinhard + gamma 2.2 over the linear +// accumulator. +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); +}