A bottom-level BuildRaytracingAccelerationStructure with MINIMIZE_MEMORY now
writes BOX16 inner nodes (64 bytes, half boxes) instead of BOX32 (128 bytes):
the same tree, about a fifth smaller. Prebuild info, the build and its refits
take the choice from the same flags (xdxr_box16), so the sizes stay exact.
Top-level builds keep BOX32 (world space: a half's step is 2 m at 2 km).
Each child box is rounded outward from the exact box kept in scratch, in
integer code (packHalf2x16's rounding is implementation-defined; AMD rounds
toward zero). Past the half range a bound goes to +-inf outward and +-65504
inward, so empty slots stay empty; below the smallest normal half it goes to
0 or +-2^-14, never a denormal a flushing decoder would shrink.
tools/xdxr_bvh.py half_round gets the same denormal rule, and check-build now
compares every BOX16 word bit for bit with encode_box16 of the exact box of
the triangles under it.
Tests: five blas_box16_* cases in vk_xdxr_build_test (radix and small sort,
an inactive triangle, values past 65504 and below 2^-14, a refit), and a
MINIMIZE_MEMORY BLAS plus its in-place update through the D3D12 API in
d3d12_xdxr_rt_test (prebuild and postbuild sizes).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#2483 made the render targets and vertex buffers inline arrays in Draw. That is 704 of its
976 bytes, and a recorded command is sized by its variant's largest alternative, so every
command in a list, barriers included, took 984 bytes.
Draw now holds a shared_ptr to a DrawViews block that the D3D12 shim rebuilds only when the
render targets or the bound vertex buffers change, so a run of draws still allocates nothing
per draw. replay_cpu_bench: one recorded Command 984 -> 296 bytes; frame 3890 -> 3824 us,
within run-to-run noise; allocations per command unchanged.
Measured against the host D3D12 runtime (RTX 5070, driver 621.14) with
d3d12_vulkan_device_test's compare_invalid_arguments:
- CreateCommandAllocator and CreateCommandList1 took any type (7, 99,
-1); the host refuses types past VIDEO_ENCODE with E_INVALIDARG.
- CreateCommandList and CreateCommandList1 took a node mask with bits
past node 0; the host answers E_INVALIDARG.
- A null output was E_POINTER in all three; the host answers
E_UNEXPECTED, after the argument checks.
compare_invalid_arguments gets 35 "command allocator:", "command list:"
and "command list1:" cases. Video list types are not compared: the host
hands out a video list (E_NOINTERFACE for ID3D12GraphicsCommandList),
while this layer creates a graphics list for them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Present is queued behind held work (QueueHolds::after_held) and submitted in order by the releasing thread.
- Held batches keep the pipeline states and root signatures of their lists; fences a queue waits on or signals are
kept until the submission has completed on the GPU (a D3D12 queue holds them).
- A held Signal released below an unheld one on the same fence leaves the Vulkan signal out (claimed under the submit lock).
- A queue destroyed with held Signals completes the CPU waits on their fences and reports it.
- A failed held batch is reported (take_held_failure, last_execute_result), its waits stay for the next submission,
and the Signal behind it still signals.
- d3d12_queue_handover_test: pairs, two-thread signalling stress, held Signal of a released fence, shared fence,
teardown with a CPU waiter; --two-queues and --shared-queue modes on a caller-owned device.
- Tests for each fix in vk_presenter_sync_test, vk_queue_query_sync_test and d3d12_m4_compute_test; docs section 4.4.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RSSetViewports, RSSetScissorRects, OMSetRenderTargets and ClearState had
no bundle check, so the layer accepted them in a bundle while the PC
runtime fails that bundle's Close (measured on an RTX 4060 Ti and WARP,
no debug layer). They now invalidate the bundle like the other calls a
bundle may not make.
New host-comparison test d3d12_bundle_forbidden: ten calls, each in a
bundle and, as a control, on a direct list, against the PC runtime on
the default adapter and WARP.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ID3D12Fence::Signal from the CPU called vkSignalSemaphore at once, also while a queue Signal of a lower value
was still pending on the GPU. Vulkan requires a host signal to stay below every pending signal
(VUID-VkSemaphoreSignalInfo-value-03259), and D3D12 allows the order. The fence now records the highest value a
queue will signal, under the same lock as the claim, and a CPU signal above a value the GPU has not reached yet
(or made while an earlier one is held) is handed to the fence dispatcher, which applies it once the GPU passes
that value, in the order the signals were made. The callback holds no pointer to the fence; when the fence is
released (cancel) or the device is lost before the queue value is reached, the held signal is dropped, since
signalling then would break the same rule. Until a held signal is applied, GetCompletedValue reads the GPU's
progress.
d3d12_fence_timing_test: a queue Signal kept pending by a Wait on a gate fence, a CPU Signal above it (held, then
applied), and two CPU signals held behind one queue signal (applied in order). Fails on main's Fence at the first
check: the event for the CPU value fired while the queue signal was still pending.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In `replay_cpu_bench` and real game execution, recording draws was the largest
remaining per-draw overhead (~155-160 ns and 4 heap allocations per draw call,
measured by @traeb0n in #2123 follow-up finding D3D-289).
Previously:
- `d3d12_com.cpp` heap-allocated a dynamic `std::vector<TargetView>`
for render targets and another `std::vector<VertexBufferView>` for vertex buffers
(copying from the list's 32-element array).
- `CommandList::draw(const Draw&)` then copied both vectors again into the `Command`
variant inside `commands_`, resulting in 4 heap allocations per draw.
This change:
- In `runtime/src/command_list.h` (`leonida::Draw`):
- Replaces `std::vector<TargetView>` with fixed inline `std::array<TargetView, 8> render_targets`
(matching `D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT` = 8) and tracks `render_target_count`.
- Replaces `std::vector<VertexBufferView>` with fixed inline `std::array<VertexBufferView, 32> vertex_buffers`
(matching `D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT` = 32).
- Adds rvalue `bool draw(Draw&&)` overload that moves the command into `append(std::move(draw))`.
- In `runtime/shims/d3d12_com.cpp`:
- Stores `render_targets_` as an inline `std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT>`,
eliminating heap reallocations on `OMSetRenderTargets`.
- Directly passes `vertex_buffers_` without copying into a temporary vector.
- Moves `draw` with `check(list_.draw(std::move(draw)))`.
- In `runtime/src/vulkan_replay.h`:
- Iterates over bound render targets using `slot < draw.render_target_count && slot < draw.render_targets.size()`.
- In `runtime/test/d3d12_com_test.cpp`:
- Adds static assertions verifying `Draw` move constructibility, move assignability,
and fixed inline array sizes.
- Updated `docs/d3d12_vulkan.md` with the contract details.
Result: 0 heap allocations per draw call (down from 4), eliminating ~155-160 ns
recording overhead per draw call.
Tested:
- `python -m compileall -q make_game.py tools tests`: OK
- `python -I make_game.py --help`: OK
- `static_assert` checks in `d3d12_com_test.cpp`: verified
Hardware: Tested on AMD Radeon RX 7800 XT (driver 24.20.19.05 / Vulkan 1.4.359 / Windows 11).
AI-assisted. Standard library / C++ only; no game content or carved assets.
Item: D3D-289
Depends on: #2123
A draw recorded in a bundle took the bundle's own viewport, targets, root
arguments and buffers. A bundle cannot set a viewport, so every bundle draw
was dropped without an error, and nothing of the parent's state reached it.
Only the stencil reference and the blend factor were handed back.
Inside a bundle, state calls, draws and Dispatch are now kept as deferred
calls, and ExecuteBundle plays them on the parent through the members a
direct call uses, between the bundle's state-free commands, in order. A
draw sees the parent's state plus what the bundle set, and what the bundle
set stays on the parent, as measured on the PC runtime. This replaces the
stencil-reference and blend-factor carry-back fields. Trace lines are
written once, when the bundle records the call.
ClearUnorderedAccessView is refused in a bundle, like the other clears.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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>
Device slot 9 (CreateCommandAllocator) now returns an allocator proxy,
the fourth command object after the queue and fence (BUILD-231) and the
list (D3D-136): the public chain (9 slots, Reset at 8) is forwarded, a
slot past it up to the end of a 64-slot table is logged and refused with
E_NOTIMPL, and a read past the table faults in its own guard region. The
console allocator's extra methods are not measured and not guessed.
A list takes the title's allocator in CreateCommandList and Reset, so a
proxy is only safe where the list side resolves it. Slot 9 therefore
wraps an allocator only when it answers kLeonidaCommandAllocatorTag (the
Vulkan layer's CommandAllocator, whose from() resolves the proxy for both
calls); a PC runtime allocator is handed out unchanged, logged once,
until a command-list proxy unwraps it at Reset. Allocator proxies are
unwrapped in the residency calls like heap, queue and fence proxies.
Device slot 12 is not touched.
Tests: allocator_proxy_test (WARP, a hardware adapter, the Vulkan device:
forwarding, refused tail, guard region, identity, D3D12's allocator rules,
a list created and Reset on the proxy with GPU copies through the queue
and fence proxies read back), mock cases for allocators with and without
the tag and their failure paths, and device_proxy_test's slot-9
expectations. No game files.
AI-assisted (Claude Code).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12_m4_compute_test, through the proxy with D3D12 API only and two
translator-output compute shaders (cs_m4_fill.asm, cs_m4_indirect.asm):
a COPY queue streams an input buffer in, a COMPUTE queue waits on it and runs a
compute PSO over a UAV table (structured buffer, raw buffers, an R32_UINT
RWTexture2D), then ExecuteIndirect of a second PSO from arguments the first
wrote (with a transition to INDIRECT_ARGUMENT and UAV barriers), and a COPY
queue behind a cross-queue fence reads the results back. 1,924 words match the
CPU reference with error 0.
Layer gaps the test found, fixed:
- CreateUnorderedAccessView of a buffer was a stub: structured and raw views are
now a V# in the slot.
- ExecuteIndirect of a dispatch ran whatever pipeline and root arguments the
command buffer had bound; the command now carries the list's, as Dispatch does.
- An image created in UNORDERED_ACCESS and first used by a shader was
zero-filled by the first barrier or copy that named it, losing the shader's
writes; images with a UAV view are now moved to GENERAL (zeroed) at the next
submission.
- COMPUTE and COPY command lists no longer log "unimplemented".
Test infrastructure: vk_test_device.h can create more than one VkQueue;
build.py gives a compute shader's FSSM program extra words (fssm-at/fssm-data
from 0x148) so two compute shaders with the same group size get distinct hashes,
and honours LEONIDA_SPIRV_TOOLS (as #1679) so the target builds under the Visual
Studio generator. docs/d3d12_vulkan.md section 6: M4 synthetic part met; M3 row
credits @whattheflop's #1584 measurement of fences in flight.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
Conflicts in d3d12_com.cpp and d3d12_vulkan_device_test.cpp: both sides kept.
d3d12_debug_markers_test recorded two lists into one allocator at once, which
this branch rejects as the PC runtime does; its second list now has its own
allocator. The device-removed wording in the docs and the test matches main.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Second merge round: main added rgb96 views, the device filter, the
present source-rect path and its own ctest registration of
d3d12_present_test. bind_image keeps both defaulted parameters and the
rgb96 view fallback; image_descriptor runs main's 96-bit family check
before the castable-list check; Resource keeps main's rgb96_ member;
register_samplers and the presentation plumbing follow main's positions,
and the duplicated d3d12_present_test block and stale 'surfaces' local
are dropped in favour of main's.