From f4d6493d91b4488894cc3ba3a7a9c728965e7a5d Mon Sep 17 00:00:00 2001 From: catbot Date: Sun, 31 May 2026 16:28:38 +0000 Subject: [PATCH] wip: uncommitted changes from claude run on issue #3 --- examples/RTStress/closesthit.wgsl | 54 ++++++++ examples/RTStress/main.cpp | 200 ++++++++++++++++++++++++++++++ examples/RTStress/miss.wgsl | 11 ++ examples/RTStress/project.cpp | 46 +++++++ examples/RTStress/raygen.wgsl | 35 ++++++ examples/RTStress/resolve.wgsl | 7 ++ 6 files changed, 353 insertions(+) create mode 100644 examples/RTStress/closesthit.wgsl create mode 100644 examples/RTStress/main.cpp create mode 100644 examples/RTStress/miss.wgsl create mode 100644 examples/RTStress/project.cpp create mode 100644 examples/RTStress/raygen.wgsl create mode 100644 examples/RTStress/resolve.wgsl diff --git a/examples/RTStress/closesthit.wgsl b/examples/RTStress/closesthit.wgsl new file mode 100644 index 0000000..c80f5bb --- /dev/null +++ b/examples/RTStress/closesthit.wgsl @@ -0,0 +1,54 @@ +// RTStress closest-hit (runs in SHADE). Computes flat-shaded Lambert from +// the hit triangle's geometric normal, accumulates ambient, and — if the +// surface faces the sun — emits a shadow ray toward the sun. The shadow +// ray's miss (sun visible) adds the direct term; its hit (occluded) adds +// nothing because RT_FLAG_SKIP_CLOSEST_HIT suppresses closesthit on hit. +// +// Payload declared here so the assembler sees it before wfPayload / SHADE. +struct Payload { + color: vec3, // shadow ray: pending direct contribution + shadowRay: u32, // 0 primary, 1 shadow +}; + +const SUN_DIR_TO_LIGHT: vec3 = vec3(0.40, 0.85, 0.35); +const SUN_COLOR: vec3 = vec3(1.15, 1.05, 0.90); +const AMBIENT_COLOR: vec3 = vec3(0.12, 0.13, 0.18); + +// Cheap per-instance albedo so the grid reads as distinct cubes (and any +// TLAS flicker as instance count scales is obvious). +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) { + let meshRec = meshRecords[tlasEntries[hit.instanceId].blasMeshIdx]; + let verts = _rtFetchTri(meshRec, hit.primitiveId); + let nObj = normalize(cross(verts[1] - verts[0], verts[2] - verts[0])); + 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 worldPos = ray.origin + ray.direction * hit.t; + 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); + + if (nDotL > 0.0) { + var sp: Payload; + sp.color = albedo * SUN_COLOR * nDotL; + sp.shadowRay = 1u; + let shadowOrigin = worldPos + nFacing * 0.05; + rtEmitRay(shadowOrigin, 0.01, sunDir, 100000.0, + RT_FLAG_SKIP_CLOSEST_HIT | RT_FLAG_TERMINATE_ON_FIRST_HIT, + 0xFFu, 0u, 0u, sp); + } +} diff --git a/examples/RTStress/main.cpp b/examples/RTStress/main.cpp new file mode 100644 index 0000000..d9581a2 --- /dev/null +++ b/examples/RTStress/main.cpp @@ -0,0 +1,200 @@ +// RTStress — the standing many-instance wavefront RT benchmark. An +// N×N×N grid of a small cube mesh (one BLAS, many TLAS instances), shaded +// with primary + shadow rays through the wavefront pipeline. The grid edge +// `kGrid` is the instance-count knob: 8 → 512, 16 → 4096, 20 → 8000 +// (LBVH_MAX = 16384). Frame time is printed to the console each second so +// fps-vs-instance-count can be read off without external tooling; the JS +// bridge additionally prints a GPU timestamp-query per-pass breakdown. +// +// WebGPU/DOM only — the wavefront tracer is the WebGPU 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 { + // Instance-count knob. instances = kGrid³. Bump to 16 (4096) or 20 + // (8000) to stress the TLAS; the LBVH build caps at 16384. + constexpr int kGrid = 8; + constexpr float kSpacing = 2.5f; + constexpr float kHalf = 0.5f; // cube half-extent + + 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("[RTStress] grid {}^3 = {} instances", kGrid, instanceCount); + + Device::Initialize(); + static Window window(1280, 720, "RTStress"); + auto cmd = window.StartInit(); + + DescriptorHeapWebGPU heap; + heap.Initialize(/*images*/ 1, /*buffers*/ 2, /*samplers*/ 1); + + 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); + + std::array raygenGroups {{ { .type = RTShaderGroupType::General, .generalShader = 0 } }}; + std::array missGroups {{ { .type = RTShaderGroupType::General, .generalShader = 1 } }}; + std::array hitGroups {{ { .type = RTShaderGroupType::TrianglesHitGroup, .closestHitShader = 2 } }}; + + // One user binding: the camera storage buffer at @group(3). + std::array bindings {{ + { .group = 3, .binding = 0, .kind = UICustomBindingKind::Buffer, ._pad = 0, .pushOffset = 0 }, + }}; + + PipelineRTWebGPU pipeline; + pipeline.Init(cmd, raygenGroups, missGroups, hitGroups, sbt, bindings); + + // ── Unit cube mesh (8 verts, 12 tris). ──────────────────────────── + static std::array, 8> verts {{ + {-kHalf, -kHalf, -kHalf}, { kHalf, -kHalf, -kHalf}, + { kHalf, kHalf, -kHalf}, {-kHalf, kHalf, -kHalf}, + {-kHalf, -kHalf, kHalf}, { kHalf, -kHalf, kHalf}, + { kHalf, kHalf, kHalf}, {-kHalf, kHalf, kHalf}, + }}; + static std::array indices {{ + 0,1,2, 0,2,3, 5,4,7, 5,7,6, 4,0,3, 4,3,7, + 1,5,6, 1,6,2, 4,5,1, 4,1,0, 3,2,6, 3,6,7, + }}; + static Mesh cube; + cube.Build(verts, indices, cmd); + + // ── Camera buffer + handle array. ───────────────────────────────── + WebGPUBuffer cameraBuf; + cameraBuf.Create(1); + static std::array userHandles { cameraBuf.handle }; + + // ── Instance grid. Reserve so RenderingElement3D::Add pointers stay + // valid across vector growth. ───────────────────────────────────── + 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; + r.instance.flags = kRTGeometryInstanceForceOpaque; + r.instance.accelerationStructureReference = cube.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 = 2; // primary + shadow + window.passes.push_back(&rtPass); + + // ── Free camera framing the grid from a corner. ─────────────────── + const float ext = float(kGrid - 1) * kSpacing; + struct CamState { + Vector position; + float yaw; + float pitch; + } cam { + Vector{ ext * 1.4f, ext * 1.0f, ext * 1.4f }, + 0.0f, 0.0f, + }; + { + // Aim at the grid centre (origin). + 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; + const float kLookSens = 0.05f; + const float kDt = 1.0f / 60.0f; + + static int frames = 0; + static double tAccum = 0.0; + 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(); + + if (++frames >= 60) { + std::println("[RTStress] {} instances @ ~{} frames since last report", instanceCount, frames); + frames = 0; + } + }); + + window.Render(); + window.StartUpdate(); + window.StartSync(); + return 0; +} +#endif diff --git a/examples/RTStress/miss.wgsl b/examples/RTStress/miss.wgsl new file mode 100644 index 0000000..d23a0bc --- /dev/null +++ b/examples/RTStress/miss.wgsl @@ -0,0 +1,11 @@ +// RTStress miss (runs in SHADE). Primary miss → sky gradient. Shadow miss +// → the sun is unoccluded, so add the pending direct contribution. +fn miss_main(ray: RayDesc, payload: ptr) { + if ((*payload).shadowRay == 1u) { + rtAccumulate((*payload).color); + return; + } + let t = clamp(ray.direction.y * 0.5 + 0.5, 0.0, 1.0); + rtAccumulate(mix(vec3(0.50, 0.62, 0.85), + vec3(0.90, 0.94, 1.00), t)); +} diff --git a/examples/RTStress/project.cpp b/examples/RTStress/project.cpp new file mode 100644 index 0000000..b1e4f03 --- /dev/null +++ b/examples/RTStress/project.cpp @@ -0,0 +1,46 @@ +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 = "RTStress"; + cfg.outputName = "RTStress"; + 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("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/RTStress/raygen.wgsl b/examples/RTStress/raygen.wgsl new file mode 100644 index 0000000..def54fc --- /dev/null +++ b/examples/RTStress/raygen.wgsl @@ -0,0 +1,35 @@ +// RTStress 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); + p.shadowRay = 0u; + + rtEmitPrimaryRay(camera.origin, 0.01, direction, 100000.0, + 0u, 0xFFu, 0u, 0u, p); +} diff --git a/examples/RTStress/resolve.wgsl b/examples/RTStress/resolve.wgsl new file mode 100644 index 0000000..7950c96 --- /dev/null +++ b/examples/RTStress/resolve.wgsl @@ -0,0 +1,7 @@ +// RTStress RESOLVE-stage tonemap: Reinhard + gamma 2.2 over the linear +// accumulator. Registered as a WebGPURTStage::Resolve shader. +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); +}