Skip to content

Single storage buffer for the walk and the scene copy: walkPacking 'shared' (issue #36 option 2) - #124

Open
DennisSmolek wants to merge 3 commits into
mainfrom
issue-36-single-buffer
Open

DennisSmolek wants to merge 3 commits into
mainfrom
issue-36-single-buffer

Conversation

@DennisSmolek

Copy link
Copy Markdown
Member

Part of #36 (option 2). Depends on #83 (branched from issue-36-host-buffer-budget; until #83 merges this PR's diff against main also shows #83's two commits, the new work is the last commit 2118244).

What changed

walkPacking: 'shared' puts the walk and the packed scene copy in ONE rtShared: array<vec4u>, so an inline rtModuleWgsl needs 1 storage buffer in total.

Layout (offsets in vec4s): [H0 | H1 | table | data | nodes | triangles | top level | positions].

  • H0 = the walk's four offsets (what Host-kernel storage-buffer budget: documented count, scene report, opt-in packed walk (#36) #83's rtWalk header holds). H1 = the packed scene header's words with absolute offsets (dataBase, tableBase, dataWords, totalVec4s).
  • The scene table keeps a compile-time base (rtShared[e + 2u], as the packed copy's rtScene[e + 1u]), the data goes through rtShared[1].x, the walk through rtShared[0].k. So a scene read costs what a packed one does, a walk read what a packed walk does. Table, data, walk in that order is what keeps the table base constant.
  • The generated scene text is unchanged. Two rewrites do the work: rtPackWalkAccess(walk, 'rtShared') and rtPackSceneAccess(source, 'shared').
  • rtPackShared(device, trace, copy) builds it (buildGpuScene({ walkPacking: 'shared' }) calls it): scene copy from the CPU, walk by Host-kernel storage-buffer budget: documented count, scene report, opt-in packed walk (#36) #83's compute copy (refactored into a shared helper). update(encoder) repacks the walk, writeScene(device, copy) rewrites the scene part (false if the table length changed or the data no longer fits; the walk sits behind both).
  • Textures: a shared scene copy has no buffer bindings, so rtSceneBufferBindings('shared') = 0, rtSceneCopyLayoutEntries(first, textures, 'shared') lists only textures and samplers, and the texture arrays start at the scene slot's firstBinding. The walk binding is at traceSlot. uploadSceneCopy(.., { packing: 'shared' }) uploads textures only. SceneCopyBinding = SceneCopyPacking | 'shared' is the widened type (GpuScene.packing, SceneCopyBuffers.packing).
  • The 128 MiB cap now applies to the sum: rtPackShared throws a RangeError naming every part (scene table, data, walk nodes / triangles / top level / positions) and suggests the two-buffer layout.
  • Budget helpers count it: rtModuleStorageBuffers, rtWalkStorageBuffers, rtSceneStorageBuffers, rtSceneEntries('shared') (empty), GpuScene.storageBuffers (walk 1, copy 0, total 1).
  • Not supported (documented, throws clearly): PtMegakernel / PtWavefront (ptSceneOptions throws for a shared scene), the batch tracers (walk only). walkPacking: 'shared' without scene throws.
  • DIVERGES V595, docs (inline-wgsl.mdx, api.mdx, design/trace-core.md), CLAUDE.md trap, TASKS P1.14, QUIET-PASS P1.14 (third arm).

Verified (correctness only, shared Mac at load 30-50: no timings)

  • Default kernels byte-identical: node bench/size-snapshot.ts 355 files diff -rq empty; plus 80 rtModuleWgsl texts (4 node formats x scene none / split / packed x shade x minify) for walkPacking: 'split' AND 'packed' byte-identical before / after (so Host-kernel storage-buffer budget: documented count, scene report, opt-in packed walk (#36) #83's packed mode is unchanged).
  • pnpm typecheck: no new errors (only the known @pmndrs/vdb ones). vitest: 18 files / 307 tests in packages/raytracer/src/{trace,scene} and the pathtracer scene-options / scene-packing tests (22 in walk-packing.test.ts, of which 9 new: one var<storage> for 4 node formats x shade, no old names left, traceSlot, budgets, texture bindings, layout).
  • node bench/runner/run.ts --page shared-buffer-check --no-lock (new): a host-style kernel with ONE storage buffer of its own runs rtClosest + rtShade (full mode, sun + sky) on Cornell, quad-light and tex-checker (textures) for split (5 module buffers), two (Host-kernel storage-buffer budget: documented count, scene report, opt-in packed walk (#36) #83's layout, 2) and shared (1). The module declares 1 buffer, equal to rtModuleStorageBuffers and GpuScene.storageBuffers.total; shared == two-buffer == split per pixel (0 differing pixels, hit instance / t / triangle / material bit-identical, max rel 0); after walk.update(encoder) + writeScene the image is unchanged; a changed table length is refused; the oversize error names all parts. (The split/packed control was already 0 for these scenes: max rel 0; P3.11 saw <= 3.2e-7 on other scenes.)
  • trace-core walkPacking=shared (inline kernel on the shared buffer, the batch kernels as the oracle, also on SceneTrees buffers): 3 of 3 runs pass on 6 formats (quantized, f32-indexed, bvh4 x2, cwbvh8 x2) x edges / cornell / quad-light / sponza-white. With bvh4-single-instanced included it failed in 2 of 3 runs, only that row, with the known inlineMismatch / coneMismatch / surfaceMismatch signature (trace-core fails intermittently: zeroed inline records in whole subgroups (shadow-masked, bvh4-single-instanced) #29, X-trace-core: cause of the trace-core flake (bvh4 single walk on Metal), repro args #85: a Metal miscompile of bvh's single walk); split control: 1 of 1 run passed.

The issue's table, with option 2

Storage buffers in the traced pass's compute stage (their pass 3, fallback, walk + scene copy, hit shading):

configuration today (packed) split option 1 (#83) option 2 (this PR)
three adapter, probe hit shading (default) 8 9 6 5
three adapter, environment hit shading 7 8 5 4
three adapter, rtShade hit shading 7 8 5 4
core, probe fallback + probe hit shading 9 10 7 6
core, probe fallback + rtShade 8 9 6 5
core, probe fallback + environment 8 9 6 5

Option 1 takes walk + scene copy from 4 to 2 (indexed leaves: 5 to 2), option 2 to 1 (indexed leaves too). Every configuration then fits the default 8 with 2-4 to spare.

Left for the maintainer

Cost of the header loads (option 1 vs 2 vs split) is not measured: quiet-pass row P1.14 has the third arm (--ab walkPacking=split,packed,shared, needs the trace-timing arg, which does not exist yet). Reflections must rebuild walk and scene copy together to use it (rtPackShared / writeScene); PtMegakernel / PtWavefront do not read it.

Check: node bench/runner/run.ts --page shared-buffer-check --no-lock (read *_shared_vs_two_differing_pixels = 0 and *_storage_buffers_declared).

🤖 Generated with Claude Code

DennisSmolek and others added 3 commits December 2, 2026 01:22
…et helpers (issue #36)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pass row, trap (issue #36)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… copy in ONE storage buffer (issue #36 option 2)

rtPackShared / rtSharedLayout, rtPackWalkAccess(.., 'rtShared'), rtPackSceneAccess(.., 'shared'), GpuScene.shared, scene copy binding 'shared' (textures only).
Module needs 1 storage buffer in all; default, split and packed kernels byte-identical. Check page shared-buffer-check, trace-core walkPacking=shared. V595.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant