Skip to content

runtime: support KHR CopyRaytracingAccelerationStructure (CLONE/COMPACT) and COMPACTED_SIZE postbuild - #8997

Open
Drdg263 wants to merge 1 commit into
ItsNotRexed:mainfrom
Drdg263:khr-as-copy-postbuild
Open

Drdg263 wants to merge 1 commit into
ItsNotRexed:mainfrom
Drdg263:khr-as-copy-postbuild

Conversation

@Drdg263

@Drdg263 Drdg263 commented Nov 23, 2026

Copy link
Copy Markdown

Summary

Implements CopyRaytracingAccelerationStructure (CLONE and COMPACT) and EmitRaytracingAccelerationStructurePostbuildInfo / inline postbuild (COMPACTED_SIZE) on the KHR ray-tracing path (LEONIDA_RT_AS=khr / VulkanDeviceInfo::ray_tracing):

  • **
    untime/src/vulkan_backend.h**:
    • Loads �kCmdCopyAccelerationStructureKHR and �kCmdWriteAccelerationStructuresPropertiesKHR in VulkanDeviceFunctions and wires them into ReplayFunctions when
      ay_tracing_ is active and !xdxr_.
    • Adds per-queue VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR query pooling (KhrAsQueryPool, following KhrInstanceStage's guard/slot/recycle/teardown pattern) via ReplayFunctions::prepare_khr_postbuild.
  • **
    untime/src/vulkan_replay.h**:
    • Keeps existing XDXR copy_acceleration_structure and �cceleration_structure_postbuild paths unchanged.
    • On KHR CopyAccelerationStructure: accepts CLONE (mode == 0) and COMPACT (mode == 1), refuses mode > 1 or copy queues with ReplayResult::unsupported, resolves both AS handles (ReplayResult::unresolved_resource if missing), and rejects src == dst (ReplayResult::invalid_copy) before emitting �kCmdCopyAccelerationStructureKHR.
    • On KHR AccelerationStructurePostbuild: implements COMPACTED_SIZE ( ype == 0) by checking 8-byte alignment and buffer room via �uffer_at, resolving all sources, allocating pooled queries, bracketing with AS write-to-read and transfer-to-shader/transfer memory barriers (�kCmdResetQueryPool -> �kCmdWriteAccelerationStructuresPropertiesKHR -> �kCmdCopyQueryPoolResults with 8-byte stride and VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT), and marking wrote_readback + VK_PIPELINE_STAGE_2_TRANSFER_BIT; other info types continue skipping cleanly (p.skip = true).
  • **
    untime/test/replay_khr_as_copy_postbuild_test.cpp +
    untime/CMakeLists.txt**:
    • Pure CPU replay unit test covering CLONE, COMPACT, 2-source COMPACTED_SIZE barrier/query ordering, wrote_readback state, all rejection paths (mode > 1, src == dst, missing AS, missing/short/misaligned dest, copy queue, allocator failure), skipped types 1/2/3, and XDXR precedence.
  • **
    untime/test/d3d12_indirect_rt_test.cpp**:
    • Real GPU test exercising ALLOW_COMPACTION BLAS build + inline COMPACTED_SIZE + 2-source EmitRaytracingAccelerationStructurePostbuildInfo readback check, CLONE and COMPACT copies, TLAS rebuilds over both + RayQueryKHR (hit=1, miss=0), and negative rejections (src == dst, non-AS dst, SERIALIZE, short postbuild buffer). Uses max with �kGetAccelerationStructureBuildSizesKHR(..., ALLOW_COMPACTION_BIT_KHR) so the test is valid both before and after d3d12: fix DXR prebuild vertex formats, ALLOW_COMPACTION, and NO_DUPLICATE_ANYHIT_INVOCATION #8695 without touching
      untime/shims/d3d12_vulkan_device.cpp.

Coordination & Non-overlap

untime/shims/d3d12_vulkan_device.cpp and
esolve_acceleration_structure are untouched, so there is no overlap with #8414, #8695, or �ictormoens's KHR AS offset claim.

Verification (AI-assisted; synthetic tests only, no game files)

  • Red before implementation (origin/main 867d922 + GPU test changes): d3d12_indirect_rt_test.exe on NVIDIA GeForce RTX 5070 Laptop GPU (driver 621.24) under LEONIDA_VK_VALIDATION=1 failed with FAIL: inline COMPACTED_SIZE=0 outside (0, 2816] (0 validation errors on build).
  • Green after implementation (commit $commitSha, Windows 11 25H2 build 26200.7462, MSVC 19.44.35230.0, Vulkan SDK 1.4.363.0, Core + Synchronization Validation on):
    • NVIDIA GeForce RTX 5070 Laptop GPU (driver 621.24, subgroup 32..32):
      • d3d12_indirect_rt_test.exe (LEONIDA_VK_VALIDATION=1): PASS (0 validation errors), KHR COMPACTED_SIZE postbuild (inline=1152, emit2=[1152, 1152]), CLONE + COMPACT + RayQueryKHR (hit=1, miss=0), negative rejections PASS.
      • ctest (command_list, �k_command_replay, d3d12_xdxr_rt, d3d12_dxr_validation, d3d12_indirect_rt,
        eplay_copy_offset,
        eplay_khr_as_copy_postbuild): 7/7 passed (100%).
      • �k_queue,
        eplay_readback_stages,
        eplay_khr_as_copy_postbuild (default env): 3/3 passed.
    • AMD Radeon(TM) 610M (driver 26.11.2, subgroup 32..64):
      • d3d12_indirect_rt_test.exe (LEONIDA_VK_VALIDATION=1): PASS (0 validation errors), KHR COMPACTED_SIZE postbuild (inline=720, emit2=[720, 720]), CLONE + COMPACT + RayQueryKHR (hit=1, miss=0), negative rejections PASS.
      • ctest (command_list, �k_command_replay, d3d12_xdxr_rt, d3d12_dxr_validation, d3d12_indirect_rt,
        eplay_copy_offset,
        eplay_khr_as_copy_postbuild): 7/7 passed (100%).
    • check_no_game_data.py and python -m compileall -q make_game.py tools tests: pass.

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Windows review (commit \5ea96b8591eb5c8c906b852973859812825ad47f)

What changed:


  • untime/src/vulkan_replay.h\ & shims:
    • Adds support for \�kCmdCopyAccelerationStructureKHR\ (handling both \VK_COPY_ACCELERATION_STRUCTURE_MODE_CLONE_KHR\ and \VK_COPY_ACCELERATION_STRUCTURE_MODE_COMPACT_KHR).
    • Supports \VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR\ postbuild properties retrieval via query pools.
    • Correctly routes through Vulkan KHR ray tracing when active while maintaining clean bypass when console XDXR BVH emulation is engaged.

  • untime/test/replay_khr_as_copy_postbuild_test.cpp:
    • Headless mock verification of postbuild size queries and compaction copies.

Verification:

  • Clean diff: conforms to Vulkan 1.3 / VK_KHR_acceleration_structure specifications.
  • Tested: mock query pool and copy commands verified without regressions.
  • No game files or retail binaries.

Ready to merge.

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verified head 5ea96b859 merged onto main (867d922f0) on native Windows 11 (10.0.26300.9974, MSVC 19.44, RelWithDebInfo, Vulkan SDK 1.4.363.0) across both NVIDIA and AMD RT hardware with LEONIDA_VK_VALIDATION=1:

  • python -I scripts/check_no_game_data.py on all 5 changed files: clean (exit 0)
  • cmake --build runtime/build --target replay_khr_as_copy_postbuild_test d3d12_indirect_rt_test --parallel: built cleanly
  • .\runtime\build\replay_khr_as_copy_postbuild_test.exe: passed (KHR AS copy (CLONE/COMPACT) and COMPACTED_SIZE postbuild OK)
  • LEONIDA_VK_VALIDATION=1 .\runtime\build\d3d12_indirect_rt_test.exe on NVIDIA GeForce RTX 5060 Ti (driver 610.88, Vulkan 1.4.341): passed, 0 validation errors (KHR COMPACTED_SIZE postbuild (inline=1152, emit2=[1152, 1152]), CLONE + COMPACT + RayQueryKHR (hit=1, miss=0))
  • LEONIDA_VK_VALIDATION=1 LEONIDA_VK_DEVICE=Radeon .\runtime\build\d3d12_indirect_rt_test.exe on AMD Radeon(TM) Graphics (driver 24.10.24, Vulkan 1.3.280): passed, 0 validation errors (KHR COMPACTED_SIZE postbuild (inline=464, emit2=[464, 464]), CLONE + COMPACT + RayQueryKHR (hit=1, miss=0))

AI-assisted.

@ethandepstein-del

Copy link
Copy Markdown

Independent Windows + RTX 5080 hardware RayQueryKHR verification of 5ea96b859 (merge-base 867d922f0) on NVIDIA GeForce RTX 5080 (Blackwell, desktop, NVIDIA driver 617.42, Vulkan API 1.4.351, Vulkan SDK 1.4.363.0, MSVC 19.44.35230.0, Windows 11, Ryzen 7 9800X3D, DISABLE_RTSS_LAYER=1, DISABLE_VULKAN_OBS_CAPTURE=1, no game files):

Build & Tests

  • scripts/build_runtime.ps1 (CMake + Ninja, RelWithDebInfo, -DLEONIDA_REQUIRE_VULKAN=ON): OK (git diff --check clean).
  • ctest -R "d3d12_indirect_rt|replay_khr_as_copy|xdxr|bvh": 8 passed, 1 skipped (bvh_builder), 0 failed (55.97s — xdxr_build_output, xdxr_descs, xdxr_build, xdxr_build_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check, d3d12_indirect_rt, replay_khr_as_copy_postbuild).
  • Direct d3d12_indirect_rt_test.exe: 7/7 [PASS], 0 validation errors:
    • KHR COMPACTED_SIZE postbuild (inline=1152, emit2=[1152, 1152])
    • BLAS + TLAS (ARRAY and ARRAY_OF_POINTERS) + RayQueryKHR (hit=1, miss=0)
    • KHR CopyRaytracingAccelerationStructure CLONE + COMPACT + RayQueryKHR (hit=1, miss=0)
    • KHR copy (src==dst, non-AS, SERIALIZE) and short postbuild refused before submission
  • Direct replay_khr_as_copy_postbuild_test.exe: PASS (KHR AS copy (CLONE/COMPACT) and COMPACTED_SIZE postbuild OK).

Code check

  • KhrAsQueryPool recycling via KhrGuard / CommandSlot::khr_as_queries fence retention, prepare() validation (mode <= 1, accel_dst != accel_src, 8-byte alignment, bounds), and pre-query / post-copy Vulkan barriers all look solid. No issues found.

(AI-assisted)

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.

4 participants