Commit Graph

16 Commits

Author SHA1 Message Date
9fd1aaed66 XDXR: CopyRaytracingAccelerationStructure (clone, compact) and postbuild sizes
CopyRaytracingAccelerationStructure and EmitRaytracingAccelerationStructure-
PostbuildInfo were not implemented, and the postbuild descs of
BuildRaytracingAccelerationStructure were ignored.

- CLONE and COMPACT copy the structure's bytes (MODE_COPY_AS). XDXR records
  are position-independent: node pointers count from the structure's start
  and a TLAS names its BLASes by address. The size comes from the source
  header, so a copy can follow a build in the same list; the destination
  resource bounds it.
- COMPACTED_SIZE and CURRENT_SIZE (MODE_POSTBUILD) are totalSizeIn64BNodes *
  64: a build is already exactly its layout's size. Written for
  EmitRaytracingAccelerationStructurePostbuildInfo and for each postbuild
  desc of a build, after it.
- New IR commands CopyAccelerationStructure and AccelerationStructurePostbuild.
  Serialization, deserialization and tools visualization are refused at
  replay. With LEONIDA_RT_AS=khr the postbuild descs stay ignored, as before.
- vk_xdxr_build_test: a clone of blas_random, its COMPACTED_SIZE (== layout
  size), a compaction into exactly that size with a guard word after it, and
  a clone of the refitted TLAS.
- d3d12_xdxr_rt_test: EmitRaytracingAccelerationStructurePostbuildInfo for
  both structures and a TLAS rebuild with a CURRENT_SIZE desc (all equal to
  the prebuild sizes), then COMPACT into a buffer of that size, checked as
  d3d12_blas_compacted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:29:57 +01:00
f6d6121c9c XDXR builds: PERFORM_UPDATE refits instead of rebuilding
An update (same topology, new positions or instance transforms) now keeps the
source's tree and recomputes its boxes: the source is copied to the
destination unless it is the destination (MODE_COPY), the elements are
rewritten at their slots, MODE_LEVEL with arg2 = 1 recomputes each node's
boxes from the children it already points to, and the header gets the new
bounds. No Morton pass, no sort, and the parent table is kept.

RTX 2050: 1,000 triangles 0.07 ms (build 0.19), 100,000 1.02 ms (2.24),
1,000,000 9.8 ms (22.0); Iris Xe 13.1 ms for 1M (47.9).

- XdxrBuilder::record_blas / record_tlas take `update_from`; the Vulkan
  backend passes the source (or the destination, for an in-place update)
  when the D3D12 build has PERFORM_UPDATE.
- vk_xdxr_build_test: a 3,000-triangle BLAS refitted in place (radix-sort
  tree), an R32-indexed grid refitted into another buffer (its untouched
  source is checked too), and the TLAS refitted after its instances move;
  the timing mode also times refits.
- d3d12_xdxr_rt_test: the BLAS is built with ALLOW_UPDATE, its soup moves,
  and a PERFORM_UPDATE in place is checked as d3d12_blas_update.
- tools/xdxr_bvh.py check-build: the case being checked answers reads at its
  address first (an in-place update leaves two dumps at one address).

A sabotaged refit (no box pass) fails every update case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:09:14 +01:00
fa2712e2b0 xdxr_build: one-workgroup sort for small builds, 256-element radix tiles
The radix sort's fixed latency dominated small builds (2,000 triangles: 0.81
ms with the sort, 0.08 ms without, on an RTX 2050; the 10k regression
@exhawk measured on #1572). Up to 1,024 keys are now sorted in one
workgroup's shared memory (bitonic, ties by element index: the stable
order; 8 KiB, within the 16 KiB minimum with the radix arrays), and the
radix tiles are 256 elements (4 chunks instead of 32 per workgroup).

RTX 2050: 1,000 triangles 0.24 ms, 2,000 0.28 ms (0.83), 10,000 0.38 ms
(0.81), 100,000 2.3 ms (2.5); 1M 21.9 ms (20.3, the larger scan).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 14:41:25 +01:00
1c58d6cfbc D3D12: BuildRaytracingAccelerationStructure writes XDXR BVH4 records
The game's translated RT shaders read the console's XDXR records, which a
VkAccelerationStructureKHR is not (docs/d3d12_vulkan.md 4.8). The standalone
Vulkan device now builds them with src/xdxr_build.h, unless LEONIDA_RT_AS=khr
keeps #743's KHR builds (the proposal in the maintainer decisions of #1548).

- VulkanDeviceInfo::xdxr_acceleration_structures; the standalone device sets
  it with bufferDeviceAddress and then leaves the KHR extensions off, so
  acceleration-structure buffers keep their console VAs.
- VulkanContext owns an XdxrBuilder; replay checks the inputs while the list
  is prepared (R32G32B32/R32G32 vertices, none/R16/R32 indices, addresses,
  counts) and records the build with heap_base added to every VA and as the
  bias for instance BLAS addresses.
- GetRaytracingAccelerationStructurePrebuildInfo answers from XdxrLayout,
  exactly; an update rebuilds, so it needs the same scratch.
- The geometry's vertex format is mapped per DXGI format (an unknown one is
  refused instead of read as R32G32B32), and its D3D12 flags reach the build.
- test/d3d12_xdxr_rt_test.cpp: a two-geometry BLAS (a soup; an R16-indexed
  grid with stride 16 and a transform) and a TLAS of three instances through
  the D3D12 API; ctest d3d12_xdxr_rt + d3d12_xdxr_rt_check.
- tools/xdxr_bvh.py: check-build compares TLAS hits by (geometry, primitive),
  and skips (77) when the build step wrote no cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 14:04:34 +01:00
51a0281154 xdxr_build: radix sort over the exact element count instead of a bitonic sort
The bitonic sort padded the keys to a power of two and needed O(log^2 n)
dispatches, so one triangle past a power of two doubled its work (2.1 ms ->
5.2 ms at 262,144 -> 262,145 triangles on an RTX 3060, reported on #1572).
It is now a stable LSD radix sort: 8 passes of 4-bit digits over the 30-bit
Morton keys, each a count per 2048-element tile, one exclusive scan and a
scatter; 24 dispatches whatever the count, shared memory only.

- vk_xdxr_build_test: a 5000-triangle case (three sort tiles, the last
  partial); stale cases from an earlier run are removed first, so a failed
  build cannot be checked against old files.
- tools/xdxr_bvh.py: validate reports a leaf reached twice; a case may ask
  check-build for fewer rays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 13:51:21 +01:00
83f13ed569 Merge remote-tracking branch 'origin/main' into rt/xdxr-gpu-builder-merge
# Conflicts:
#	runtime/CMakeLists.txt
2026-11-22 13:38:24 +01:00
86f7e9f887 Runtime: build XDXR BVH4 acceleration structures on the GPU
The game's translated ray-tracing shaders read the console's BVH records
themselves (docs/d3d12_vulkan.md 4.8), so the layer has to write them. This
adds the GPU builder, checked against tools/xdxr_bvh.py:

- src/xdxr_build.comp: one pipeline, one dispatch per step. Triangle leaves
  (any of R32G32B32/R32G32 vertices, none/R16/R32 indices, a stride, an
  optional 3x4 transform) or 128-byte INSTANCE records with world-to-object
  inverses; Morton keys and a bitonic sort; BOX32 nodes four consecutive
  children at a time, level by level, with the parent table; the header.
  Buffer device addresses as uvec2: needs bufferDeviceAddress, not
  shaderInt64. NaN triangles, mask-0 instances, instances without a BLAS and
  singular transforms get empty boxes.
- src/xdxr_build.h: XdxrLayout (exact result and scratch sizes from the
  element count, for the prebuild info too) and XdxrBuilder (records the
  dispatches). A bias maps the application's 40-bit BLAS addresses to device
  addresses.
- test/vk_xdxr_build_test.cpp: nine synthetic cases built on the GPU and
  dumped; ctest `xdxr_build` + `xdxr_build_check`, which runs
  `tools/xdxr_bvh.py check-build`: validate, then trace 1000 rays per case
  against brute force.
- vk_test_device.h: opt-in bufferDeviceAddress (request_buffer_device_address,
  device_address()).
- tools/xdxr_bvh.py: check-build; trace takes the bias; instances without a
  BLAS and inactive triangles are valid.

Not wired to BuildRaytracingAccelerationStructure yet: that path builds a
VkAccelerationStructureKHR today (#743), and which one the layer uses is a
maintainer decision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 13:19:18 +01:00
d55fa3d4d1 Translator: image_bvh triangle nodes read their type's vertex slots and the barycentric remap byte
image_bvh(64)_intersect_ray followed RADV's software path on triangle nodes,
which always reads vertex slots 0, 1, 2 and returns (v, w): RADV builds one
triangle per node. The hardware format packs up to four triangles per node,
and the node type picks the slots: TRI0 (0,1,2), TRI1 (1,3,2), TRI2 (2,3,4),
TRI3 (2,4,0). The type's byte of the triangle-ID word at +60 picks which two
vertex weights come back (bits [1:0] and [3:2]; 0x09 is RADV's v, w). A BVH
with paired leaves therefore traversed TRI1 against the wrong triangle.

Both rules come from AMD GPURT (CalcTriangleCompressionVertexIndices,
SwizzleBarycentrics, TRIANGLE_ID_I/J_SRC_SHIFT), and the public XDXR software
path (Read3Verts, TestXboxBvh4Leaf) agrees with it.

- tests/semantic/test_image_bvh_tri.py: every node type on a hand-built node,
  every remap selector pair, the byte per type, selector 3, a zero word
- spirv_mini_interp: OpVectorExtractDynamic and GLSL.std.450 FSign, from the
  specifications
- vk_compute_test "bvh": its hand-built triangle nodes now carry the 0x09
  triangle ID a builder writes; expected words unchanged
- docs/design.md Ray tracing row, docs/bvh.md 7a, translator docstring

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 12:56:07 +01:00
854d4100f0 tools/xdxr_bvh.py: reference encoder and traversal for the XDXR BVH4 runtime layout
The game's ray-tracing shaders read acceleration-structure nodes themselves
(docs/d3d12_vulkan.md 4.8), so a builder on the Vulkan layer must write the
console's XDXR BVH4 records. This adds the CPU reference for that builder:

- build_blas: triangles to header, parent table, leaves (TRI0, or TRI0/TRI1
  pairs with per-triangle barycentric remap) and BOX32 or conservative BOX16
  nodes
- build_tlas: 128-byte INSTANCE records with world-to-object transforms
- intersect_node: one traversal step with the hardware instruction's
  semantics as the public XDXR software path describes them
- trace / brute_force: closest hit, checked against every triangle
- validate: header, pointers, nested bounds, parent table; CLI `validate`
  for a locally dumped buffer, and `selftest`

Layouts come from docs/bvh.md 3.1 and the public XDXR traversal source it
cites; the region order, build heuristic and pairing are this tool's own
choices and are marked ASSUMPTION. docs/bvh.md 7a records each field's source
and an Open difference: the translator's image_bvh triangle path ignores the
TRI1-TRI3 vertex selection and the remap byte.

Standard library only; synthetic fixtures; no game files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 12:45:42 +01:00
dd4a2f549a Guards: ignore and detect the model, audio and world files the build writes
The export stages write .obj models, .glb chunks, .ogg Opus and .wav PCM
straight from the package, but .gitignore and the No Game Data check only
named the archive, shader and texture types. A run pointed inside a checkout
could leave those untracked and get them staged; the check would also miss a
force-added one, along with .r9modeldata and .msixvc that .gitignore already
covers.

The bvh and navmesh examples also wrote their index reports into the working
directory, which puts game-derived JSON and CSV in the tree; point them at
<temp> like the movies note does.

Verified with git check-ignore on every new pattern and by matching sample
filenames against the workflow regex. Docs and ignore rules only.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-21 23:27:15 -06:00
7b6139cdff Report invalid BVH roots and clarify index output 2026-11-21 23:46:04 +00:00
2a122c7cbc Cite BVH4 header source and report version field 2026-11-21 23:21:07 +00:00
eb629d41a8 Decode BVH4 header and node pointer fields
The indexer treated the pre-header wrapper words as the root and labelled the real header fields as counts. Parse the documented XboxBvh4Header fields, decode root node pointers, and document the runtime node and leaf layouts while keeping the dehydrated packed stream open.
2026-11-21 22:55:52 +00:00
76b7b5b329 Index tools: refuse --limit/--start values that scan nothing, survive empty archives
bvh_index, page_index, story_index, navmesh_index and psin_index:

- --limit 0 meant "no limit" (0 is falsy) and a negative --limit scanned
  nothing and still exited 0 with an empty index. --limit must now be at
  least 1 and --start 0 or more; argparse rejects anything else (rc 2).
- An empty .rpf crashed in mmap ("cannot mmap an empty file") and a file
  shorter than the header word crashed in struct.unpack_from. bvh, page and
  story now treat both as an archive with no streams.
- bvh_index directory mode: one bad archive no longer aborts the scan and
  loses the results of the others; it is reported and skipped.
- A --start past the end of the archive is a one-line error (rc 2) instead
  of a traceback (bvh also checked nothing and reported a negative span).
- page_index checks the output extension before the scan, not after it.

Docs: the accepted ranges and the empty-archive behaviour are written next
to each tool's usage in docs/{bvh,navmesh,page_directory,psin,story}.md.

Tests: synthetic cases for each tool; they fail on main and pass here.
2026-11-21 23:21:29 +01:00
83316689c9 docs: note the BVH family is 99.95% of inflated bytes 2026-11-21 19:21:25 +00:00
1681676f5a Docs: measured BVH archive composition
Index the three *_bvh4 RPF9 packs with the fixed carver and record the measured stream/byte composition. Add tools/bvh_index.py for the 0x81020006 header family (tag + 0x140 + 64*n == end), with synthetic tests. No game data, keys or carved payloads are included.
2026-11-21 19:19:53 +00:00