Skip to content

d3d12: translate ARRAY_OF_POINTERS instances on the KHR path - #4060

Open
sds-228 wants to merge 2 commits into
ItsNotRexed:mainfrom
sds-228:d3d425-khr-instance-pointers
Open

sds-228 wants to merge 2 commits into
ItsNotRexed:mainfrom
sds-228:d3d425-khr-instance-pointers

Conversation

@sds-228

@sds-228 sds-228 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Fixes D3D-425 on the opt-in LEONIDA_RT_AS=khr path. TLAS builds with D3D12_ELEMENTS_LAYOUT_ARRAY_OF_POINTERS were sent to Vulkan with arrayOfPointers = VK_FALSE and the guest's console-VA list, so pointer bytes were read as instance descs. The layout is now recorded, each pointer is validated, and a translated device-address copy is staged for the build without modifying guest input. GPU-written pointer lists are refused.

Tests: full CTest 153 passed, 9 skipped, 1 disabled in both KHR and default XDXR modes (Windows/MSVC, RTX 3090). d3d12_indirect_rt passes with Core + Sync validation, 0 validation errors. Negative controls: reverting only the emit change fails the ARRAY_OF_POINTERS RayQuery case; a null pointer entry is refused before submission. No game files.

AI-assisted (OpenCode)

KHR TLAS builds hardcoded arrayOfPointers = VK_FALSE and passed the guest's console-VA pointer list straight to Vulkan. Carry DescsLayout into the recorded build, validate each pointer, stage a copy with heap_base added into a device-addressable buffer, and use it as the build input. The guest input is not modified and GPU-written pointer lists are refused. Adds ARRAY and ARRAY_OF_POINTERS RayQuery coverage plus a null-entry refusal check.

@Kam471 Kam471 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.

Tested cc4b3744 on Windows 11, RTX 5070 Ti (621.24).

  • KHR path (LEONIDA_RT_AS=khr): d3d12_indirect_rt_test.exe -> exit 0, all cases PASS: ExecuteIndirect with suballocated argument/count buffers, BLAS + TLAS (ARRAY and ARRAY_OF_POINTERS) + RayQueryKHR (hit=1, miss=0), and ARRAY_OF_POINTERS null entry refused before submission; 0 validation errors.
  • Default XDXR path: same three PASS, 0 validation errors.
  • ctest -R d3d12_indirect_rt -> 1/1 passed, 0.24 s.

The ExecuteCommandLists failed: invalid copy or resolve line is the expected negative-path log; it appears before the passing cases and does not affect the verdict. The pointer-list validation and the staged address copy are exercised on real hardware. No game files; no blocker.

@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Review note about allocation cost. An AI tool helped me to read the diff. I did not measure the cost. I found no defect in the logic.

Possible cost

stage_khr_instances runs for each TLAS build that uses D3D12_ELEMENTS_LAYOUT_ARRAY_OF_POINTERS. Each call does these steps:

  1. vkCreateBuffer
  2. vkAllocateMemory with VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT
  3. vkBindBufferMemory
  4. vkMapMemory and vkUnmapMemory
  5. vkGetBufferDeviceAddress

Many titles build the TLAS again in each frame. Then each frame makes one new device memory allocation for each such TLAS. vkAllocateMemory is a slow call on most drivers. The device also has a limit, maxMemoryAllocationCount.

The lifetime is correct. The queue releases the stage when it uses the slot again. Thus the number of stages has a limit.

Suggestions

  1. Keep one staging buffer for each slot and use it again. Make it larger only when 8 * instance_count is larger than its size. Keep the memory mapped during the life of the buffer.
  2. Or get the bytes from the scratch buffer helper that the queue has for staged copies. It already records the submissions that use a range.
  3. Write the translated addresses directly into the mapped memory. Then the translated vector and the second copy are not necessary. Do the address checks in the same loop. If a check fails, release the stage as the code does now.

Test

Build the same TLAS in a loop of N frames. Count the vkAllocateMemory calls. With the buffer used again, the count does not increase after the first frames.

@Flyander Flyander 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.

The KHR path needs to distinguish CPU-authored descriptor data from GPU-produced inputs before staging it on the CPU.

Comment thread runtime/src/vulkan_backend.h Outdated
BufferVaIndex<ResourceId>::Hit hit{};
if (!buffer_vas_.find(va, hit)) return false;
const ResourceBinding* b = resolve(hit.id);
if (!b || !b->host) return false;

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.

[P2] Do not stage InstanceDescs by reading a CPU mirror when the list can be GPU-generated. D3D12 explicitly permits instance descriptions to be generated directly in GPU memory by compute shaders (https://learn.microsoft.com/en-us/windows/desktop/api/d3d12/ns-d3d12-d3d12_raytracing_instance_desc). This host_at mapping is also attached to arena/default-heap resources, and stage_khr_instances reads the list and pointed-to descriptors during CPU preparation, before this submission executes. If an earlier dispatch/copy in the same command list writes them, this sees stale pre-submit bytes and can reject the build or stage the wrong instances. Restrict this path to explicitly CPU-authored inputs and return unsupported for GPU-produced lists, or translate the pointer list on-GPU while preserving command ordering; add a dispatch-produced-list regression case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 77f00b6. host_at now distinguishes CPU-written from GPU-writable memory; non-CPU-writable instance lists return unsupported before submission. Added a GPU-copy-produced placed-DEFAULT regression with a negative control; per-frame builds reuse completed stages. RTX 3090/621.24: KHR and default XDXR full CTest 153 passed, 0 failed each, 0 validation errors.

AI-assisted (OpenCode)

@paratustra

Copy link
Copy Markdown

Code read at cc4b374 (not built: the machine is macOS). The new docs/d3d12_vulkan.md text says a pointer list "written by an earlier GPU command in the same submission is refused", but I can't find anything that enforces that:

  • stage_khr_instances (runtime/src/vulkan_backend.h) reads the list with vk.host_at. That lambda (vulkan_backend.h:886) only requires ResourceBinding::host != nullptr. Nothing looks at earlier writes in the submission, at host_readable, or at the heap type.
  • Committed DEFAULT buffers are refused, but only because they have no host.
  • Placed buffers always have host. create_placed_buffer (runtime/shims/d3d12_vulkan_device.cpp:1656) sets binding.host = device_.arena.mapped + va_ for every heap type, DEFAULT included, since the arena is mapped.

Failure scenario: a title keeps its pointer list in a buffer placed in a DEFAULT heap, which is the usual console layout. It fills the list on the GPU, with CopyBufferRegion from an upload buffer or with a compute pass, earlier in the same ExecuteCommandLists or in a submission that hasn't completed yet. Then it builds the TLAS. prepare_khr_instances runs at replay, on the CPU, before those GPU writes land, so it reads the arena's current bytes instead:

  • zeros (a new placement is memset) → refused with invalid_copy, which is at least visible;
  • last frame's pointers → translated without any error, so the TLAS is built from stale instances.

All the new tests use committed resources, so this path isn't reached. Two possible fixes:

  • Make host_at require host_readable (UPLOAD placements, i.e. CPU-written memory). DEFAULT placements would then be refused like committed DEFAULT buffers.
  • Or check that no command earlier in the submission writes the range, which is what the doc says happens.

Either way, a test with a placed-DEFAULT list would cover this path.

No game files. AI-assisted (Claude Code).

@AKA0GHOST

Copy link
Copy Markdown

Tested cc4b374 on NVIDIA GeForce RTX 5070, driver 621.14, Windows 11, MSVC (VS 2022 17.14), scripts\build_runtime.ps1 RelWithDebInfo: pass.

d3d12_indirect_rt_test: [PASS] BLAS + TLAS (ARRAY and ARRAY_OF_POINTERS) + RayQueryKHR (hit=1, miss=0) and [PASS] ARRAY_OF_POINTERS null entry refused before submission, with LEONIDA_VK_VALIDATION=1 and without; 0 validation errors.

ctest -R "d3d12_xdxr_rt|d3d12_indirect_rt|d3d12_vulkan_device": 4/4 passed in the default (XDXR) mode. With LEONIDA_RT_AS=khr: d3d12_vulkan_device and d3d12_indirect_rt passed, the two XDXR tests skipped as expected.

Small note: the null-entry refusal logs ExecuteCommandLists failed: invalid copy or resolve, which reads a little oddly for a null pointer (the XDXR gather in #3944 reports unresolved_resource for the same case), but the behaviour is right.

I had a second PR on D3D-425 (#4105); closing it in favour of this one, which claimed it first and doesn't depend on #3944. AI-assisted (Claude).

Only CPU-writable heaps expose a host pointer to replay, so DEFAULT and READBACK pointer lists are refused as unsupported instead of being read from possibly stale mapped bytes. Reuse completed staging buffers and write translated addresses directly. Adds a GPU-copy-produced placed-DEFAULT regression with a negative control.

AI-assisted (OpenCode)
@sds-228

sds-228 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Review fixes are in 77f00b6. GPU-written instance lists are now refused as unsupported before submission: default/placed DEFAULT and READBACK buffers expose no host pointer to replay, while CPU-written UPLOAD and write-back CUSTOM lists still stage. This covers pointer lists produced by an earlier copy or dispatch; a GPU-copy-produced placed-DEFAULT regression fails when the gate is removed. Stage buffers are reused per queue and translated addresses are written directly.

RTX 3090/621.24: KHR and default XDXR full CTest 153 passed, 0 failed each, 0 validation errors. One unrelated winhttp_offline transient passed 5/5 in isolation.

AI-assisted (OpenCode)

@paratustra

Copy link
Copy Markdown

Re-read at 77f00b6 (code read, not built). This resolves my placed-DEFAULT note. ResourceBinding::host is now set only for cpu_writable() heaps: UPLOAD, and CUSTOM write-back or write-combine. Placed and committed DEFAULT and READBACK buffers have no host pointer. host_at reports not_cpu_written for them, and the build is refused before staging. host_at is the only reader of that field, so leaving it null for READBACK changes nothing else. host_readable still drives the readback flushes. The new placed-DEFAULT case in d3d12_indirect_rt_test.cpp, seeded through CopyBufferRegion, covers the path I described.

No game files. AI-assisted (Claude Code).

@sds-228

sds-228 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Ready for re-review at 77f00b6. paratustra confirmed their placed-DEFAULT concern is resolved. RTX 3090/621.24: KHR and default XDXR full CTest 153 passed, 0 failed each, 0 validation errors; the negative control fails when the gate is removed.

AI-assisted (OpenCode)

@zNvsty

zNvsty commented Nov 23, 2026

Copy link
Copy Markdown

Tested the new head 77f00b60 (base main aa2988cc) on NVIDIA GeForce RTX 3060 Ti (Ampere), driver 621.14, Windows 11 26200, MSVC 19.44, Windows SDK 10.0.26100, Vulkan SDK / validation layer 1.4.363.0, Ninja RelWithDebInfo: pass. The RTX 5070 Ti run above was on cc4b3744, before 77f00b60.

  • Full ctest: 145/145 pass. That matches main aa2988cc on the same machine, with the same skips.
  • ctest -R 'indirect_rt|acceleration|xdxr_rt|d3d12_vulkan_device' --repeat until-fail:3 with LEONIDA_VK_VALIDATION=1: 4/4 pass, on the default (XDXR) path and again with LEONIDA_RT_AS=khr (the d3d12_xdxr_rt* tests skip there as designed). d3d12_indirect_rt on both paths:
    • [PASS] BLAS + TLAS (ARRAY and ARRAY_OF_POINTERS) + RayQueryKHR (hit=1, miss=0)
    • [PASS] ARRAY_OF_POINTERS null entry refused before submission
    • [PASS] ARRAY_OF_POINTERS GPU-written placed pointer list unsupported before submission
    • PASS (0 validation errors). There are 0 VUID- messages. The two ExecuteCommandLists failed ... nothing was submitted lines are the two refusal cases.

I ran #4301 (f309fbc4) on the same machine with the same steps: it passes too (details on #4301). This is a GPU run, not a code review. No game files. AI-assisted (Claude Code).

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.

7 participants