Crafter.Graphics/examples/RayQueryPick
catbot 931500ddc3 perf(webgpu): byteCount-bounded readback for over-provisioned buffers (#133)
WebGPUBuffer::EnqueueReadback / PollReadback always copied the full
buffer `size` GPU→staging→wasm. Over-provisioned event-queue buffers
(e.g. Forts3D's GPU physics event drain) paid the full-capacity copy
every drain even when only a small live prefix held data.

Add an optional `byteCount` parameter (default 0 = whole buffer, so the
existing full-buffer callers are unchanged) bounding the readback to the
live prefix. Pass the same byteCount to the paired PollReadback so the
matching number of bytes lands in `.value`.

JS bridge: the staging buffer is now sized to the full device-buffer
capacity (not the first call's byteSize) so a varying prefix length never
overflows it, while the copyBufferToBuffer + mapAsync/getMappedRange are
bounded to the requested prefix — that's where the copy saving lands.

Verified end-to-end in the browser via RayQueryPick: the full readback
still resolves correctly, and a follow-up 8-byte prefix read copies only
hit+customIndex while primitiveIndex keeps its poison sentinel, proving
the copy was bounded. Native `crafter-build test` suite: 24 passed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 14:00:36 +00:00
..
closesthit.wgsl test(webgpu-rt): RayQueryPick example exercising the rayQuery TLAS shim (#25) 2026-06-04 13:33:04 +00:00
main.cpp perf(webgpu): byteCount-bounded readback for over-provisioned buffers (#133) 2026-06-18 14:00:36 +00:00
miss.wgsl test(webgpu-rt): RayQueryPick example exercising the rayQuery TLAS shim (#25) 2026-06-04 13:33:04 +00:00
project.cpp test(webgpu-rt): RayQueryPick example exercising the rayQuery TLAS shim (#25) 2026-06-04 13:33:04 +00:00
raygen.wgsl test(webgpu-rt): RayQueryPick example exercising the rayQuery TLAS shim (#25) 2026-06-04 13:33:04 +00:00
rayquery_pick.wgsl test(webgpu-rt): RayQueryPick example exercising the rayQuery TLAS shim (#25) 2026-06-04 13:33:04 +00:00
README.md test(webgpu-rt): RayQueryPick example exercising the rayQuery TLAS shim (#25) 2026-06-04 13:33:04 +00:00
resolve.wgsl test(webgpu-rt): RayQueryPick example exercising the rayQuery TLAS shim (#25) 2026-06-04 13:33:04 +00:00

RayQueryPick

Regression test for the WebGPU software ray-query shim (additional/dom-webgpu.js, _rqTraverseTlas).

Builds an 8³ = 512-instance TLAS and shoots one fully-determined ray through a rayQuery=true PlainComputeShader. The committed hit is read back to the host and checked against the analytically-known answer (instance customIndex = 484, t = 40.75). On success the console prints:

[RayQueryPick] result: hit=1 customIndex=484 prim=6 t=40.75
[RayQueryPick] PASS — rayQuery TLAS traversal hit the expected instance

Why 512 instances

The TLAS sweep tree is padded to next_pow2(instanceCount) leaves. The rayQuery shim used to detect BVH leaves with a hardcoded 16384 - 1 leaf start, so for any scene with fewer than 8193 instances no node index ever reached a leaf and every pick missed (issue #25). 512 sits squarely in that broken regime, so this example fails fast if the shim regresses to a static leaf start. The shim now derives the leaf start from a per-build RqTlasMeta.nPadded uniform, matching the megakernel _rtwTraverseTlas.

The scene also renders through the wavefront RT pipeline (same as RTStress) so the run produces a visible frame, but the pass/fail signal is the console line above.

cd examples/RayQueryPick
crafter-build -r --target=wasm32-wasip1

WebGPU/DOM only — the native path uses hardware ray queries.