DeviceState::allocate takes the next memory type when device-local memory is
full, so on a 4 GB GPU a texture could land in system memory without a word.
leonida::spilled_to_host (vulkan_memory.h) says when that happened: the
allocation preferred DEVICE_LOCAL, needed no host access, and got a type
without it. The arena and upload heaps in system memory without Resizable BAR
are the memory type policy and are not reported. The first spill is logged
once, with its size, memory type and heap. Part of #2375.
# Conflicts:
# runtime/shims/d3d12_vulkan_device.cpp
vulkan_topology (D3D12 primitive topology -> VkPrimitiveTopology) and
strip_topology (which Vulkan topologies a restart index cuts) had no test. A
wrong mapping draws the wrong primitive; a wrong strip classification restarts an
indexed strip that should not, or fails to restart one that should.
Pins both: every mapped D3D12 topology value to its VkPrimitiveTopology and strip
flag, the triangle-list fallback for list/undefined/unmapped values, and the
strip classification, with the D3D_PRIMITIVE_TOPOLOGY enum values SDK-asserted.
ctest -R d3d12_state_map passes (MSVC /W4 /permissive-, Windows SDK 10.0.26100,
Vulkan headers 1.4.363). Header-only, no GPU, no game files.
CopyTextureRegion between two block-compressed textures copies whole blocks
on the PC runtime. When a mip edge falls inside a block on one side only (a
4x4 box into a 3x3 mip, a 6x6 mip into a 12x12 one), no single Vulkan extent
is a block multiple or reaches the edge of both images, so the layer refused
the copy with invalid_copy (docs/d3d12_vulkan.md, section 3.2 known gaps).
Such a copy now goes through the transfer scratch that depth <-> colour
copies already use (vulkan_scratch.h): vkCmdCopyImageToBuffer with the
source's own cut extent into whole blocks, then vkCmdCopyBufferToImage with
the destination's. The box fits the source's padded mip, so the scratch
holds every block the destination reads. The staged emit path calls the
destination's copy_layout after the scratch write, so every earlier transfer
write is ordered before the destination's (the write-after-write @Vonsek
found under synchronization validation in the first version of #860).
- d3d12_copy_compare: the 8 BC mixed-mip cases that were printed as known
differences must now match the host; two more shapes (a source edge inside
the block, a destination offset to the edge).
- d3d12_state_map_test checks the staged regions on the CPU.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
block_format() handles 65 VkFormats; the test pinned 37 of them. A mistyped size
on one of the untested 28 -- the SINT/SNORM/UINT variants and the PACK16/PACK32
formats the DXGI map also produces -- would silently corrupt a copy without the
test catching it.
Adds those 28 to the block_cases table at their spec-derived (bytes, block):
each integer variant the size of its UNORM/SFLOAT sibling, the PACK16 formats 2
bytes and the PACK32 formats 4, so the test now asserts every format
block_format handles.
Built and ran on Windows (MSVC /W4 /permissive-, Windows SDK 10.0.26100, Vulkan
headers 1.4.363): ctest -R d3d12_state_map passes. Header-only, no GPU, no game files.
block_format in vulkan_replay.h -- the bytes-per-texel/per-4x4-block table that
copies use to lay a subresource out in a buffer -- had no direct test, though the
mapping functions and the subresource decoder next to it are covered here. A
mistyped byte count or a missed 4x4 block there would silently corrupt a copy
rather than fail.
Adds a block_format section pinning every VkFormat the DXGI format map can
produce (19 uncompressed and 14 block-compressed) to its spec-derived
(bytes, block), plus the depth- and stencil-aspect behaviour (the stencil plane
is one byte whatever the format value is) and the {0, 1} sentinel for a format
copies cannot lay out yet. No production code changes.
Built and ran on Windows (MSVC /W4 /permissive-, Windows SDK 10.0.26100, Vulkan
headers 1.4.363): ctest -R d3d12_state_map passes, no warnings. Header-only, no
GPU, no game files.
Three allocations replay_cpu_bench (#878) found on the hot path:
- detail::current returned a std::vector<LayoutPiece> for every
transition, clear and copy in emit, though the answer is nearly always
one piece (two with separate depth/stencil). LayoutPieces keeps two
inline and spills to the heap beyond that, with the same interface
(begin/end, size, push_back, clear), so `auto pieces = current(...)`
callers compile as is. LayoutTracker::pieces takes either container.
- detail::clip built a new vector with push_back (two allocations for two
rectangles); it now writes into the kept entry's p.rects.
- CommandList::append took const Command& and copied the temporary the
recording calls build, so a rectangle clear's vector was allocated
twice; an rvalue overload moves it.
replay_cpu_bench, i7-14700K, on top of #893: the frame goes from 194 to
173 ns per command, emit from 48 to 33 ns; emit makes no allocation per
command in the frame (0.43 before) and prepare 0.07 (0.26), and
recording makes one only for a rectangle clear's own rectangles.
d3d12_state_map_test checks LayoutPieces against the vector path,
including the spill at four pieces.
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.
A root CBV supplies a device address and shaders load through a physical storage buffer. Transfer writes must be visible to that access in both legacy and enhanced buffer transitions.
Keep-both resolutions per #313's conflict analysis: main's ExecuteIndirect /
acceleration-structure / debug-marker / depth24 / node-mask features alongside
the stack's arena, compute and draw paths. Main's indirect and AS offsets gain
the arena's buffer_offset / heap_base compensation; AS resources on default
heaps get their GENERIC structure over the arena range. Reset clears
stencil_ref_set_ next to reset_state, and OMSetStencilRef sets it. README takes
main's (the crypto-token lines stay deleted).
A CPU-only check of the loop renamed in the previous commit: two per-subresource
discards in one list each give their own ranges (mip and layer decoded), a
partial discard records none, and a range one past the last subresource is
refused. With the loop stopping one short, the test fails at first.size() == 3.
docs/d3d12_vulkan.md now lists this and the Resizable BAR detection the test
already had.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without Resizable BAR the only DEVICE_LOCAL | HOST_VISIBLE memory type sits in a heap of 256 MiB or less
(214 MiB on an RTX 3090, driver 616.56). The 256 MiB default arena preferred that type, so vkAllocateMemory
was asked for more than the heap holds (VUID-vkAllocateMemory-pAllocateInfo-01713). The NVIDIA driver still
returned VK_SUCCESS, so the fallback to plain host-visible memory never ran, and d3d12_compute_test and
d3d12_draw_test failed under validation. Reported by CharlyRien.
memory_type_order (vulkan_backend.h) now leaves out every type whose heap is smaller than the allocation,
so the preference falls through to system memory there. DeviceState::allocate uses it for every allocation.
d3d12_state_map_test checks the order on that card's heap layout and with Resizable BAR on.
D3D12_RESOURCE_STATE_PREDICATION is the same bit as INDIRECT_ARGUMENT
(and SYNC/ACCESS_PREDICATION the same as EXECUTE_INDIRECT /
INDIRECT_ARGUMENT), and the layer mapped that bit only to
INDIRECT_COMMAND_READ at DRAW_INDIRECT. vkCmdBeginConditionalRenderingEXT
reads the predicate with CONDITIONAL_RENDERING_READ at the
CONDITIONAL_RENDERING stage, so ResolveQueryData -> COPY_DEST to
PREDICATION barrier -> SetPredication had no dependency from the copy's
write to the predicate read (and none the other way before the next
resolve).
With the extension enabled (the only case those flags are valid),
vulkan_access and vulkan_barrier_scope now add the conditional-rendering
access and stage for that bit; GENERIC_READ, which contains it, gets
them too. The replay passes the flag from its conditional-rendering
entry points.
d3d12_vulkan_device_test now enables conditional rendering on its device
and runs an occlusion query resolved into a predicate: EQUAL_ZERO skips a
clear, NOT_EQUAL_ZERO runs one, and the predicate is resolved into again
after the read, once through legacy barriers and once through enhanced
buffer barriers. d3d12_state_map_test checks both mappings with and
without the extension.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CreateQueryHeap makes one VkQueryPool per heap: occlusion (OCCLUSION and
BINARY_OCCLUSION queries), timestamp and copy-queue timestamp, and pipeline
statistics (all eleven Vulkan counters, whose order is the field order of
D3D12_QUERY_DATA_PIPELINE_STATISTICS). Other heap types are logged and
refused with E_NOTIMPL.
BeginQuery, EndQuery and ResolveQueryData are recorded in the command-list
IR with D3D12's rules for the calls, and replayed as vkCmdResetQueryPool +
vkCmdBeginQuery/vkCmdEndQuery, vkCmdWriteTimestamp at BOTTOM_OF_PIPE and
vkCmdCopyQueryPoolResults (64-bit, WAIT). Heap types, index ranges and the
destination buffer are checked before anything is written. A query that was
never ended resolves to zeros instead of stalling the GPU on WAIT_BIT.
GetTimestampFrequency is 1e9 / timestampPeriod. The devices enable
occlusionQueryPrecise and pipelineStatisticsQuery where the GPU has them.
Overlapping queries of one kind (valid D3D12, one active query per type in
Vulkan) are refused as unsupported, and binary occlusion is not clamped to
0/1 yet; both are written down in docs/d3d12_vulkan.md section 4.6.
Tested on an RTX 5070 (driver 621.01) under VK_LAYER_KHRONOS_validation.
AI-assisted (Claude Code).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Upload-heap buffers asked for HOST_VISIBLE | HOST_COHERENT memory only, so the GPU read every vertex, index and
constant buffer a game uploads over PCIe from system memory. They now prefer DEVICE_LOCAL | HOST_VISIBLE |
HOST_COHERENT when the device has Resizable BAR: such a type in a device-local heap larger than the 256 MiB BAR
window (leonida::resizable_bar, runtime/src/vulkan_memory.h). Without Resizable BAR that type is a window of
256 MiB or less that every upload buffer would share, so they stay in system memory there. A full VRAM heap falls
back to system memory through DeviceState::allocate's existing order.
The device logs where upload heaps live, LEONIDA_UPLOAD_SYSMEM=1 keeps them in system memory for comparison, and
vulkan_memory_flags / device_upload_in_vram report the choice to tests. d3d12_state_map_test checks the detection
on the heap layouts of an RTX 5060 Ti with Resizable BAR, an RTX 3090 without it, a 256 MiB window and an
integrated GPU; d3d12_vulkan_device_test checks that its upload buffer is placed accordingly, and its GPU copies
then read that buffer in both placements.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A volume mip has a single tracked layout, but its color attachment may cover only part of its depth. Use the view to select W slices and keep barriers on the whole mip so clears preserve neighboring slices.
Windows SDK 10.0.28000.0 omits the legacy video queue layout
enumerator. Compare against the enum when the SDK exposes it and
check the established numeric value on newer SDKs.
D3D12 lets an access be named with a sync scope whose Vulkan stages cannot perform it:
SYNC_ALL_SHADING with ACCESS_VERTEX_BUFFER is valid D3D12, but VERTEX_ATTRIBUTE_READ needs
VERTEX_ATTRIBUTE_INPUT. Vulkan rejects such barriers (docs/d3d12_vulkan.md listed it as a known gap).
vulkan_barrier_scope maps each side of a global, buffer or texture barrier and adds the narrowest
stage that supports any access left without one (the synchronization2 "supported access types"
table). The access is never dropped, so the dependency still covers everything the application
named. CLEAR only writes, and ALL_GRAPHICS holds no transfer stage, so transfer reads get ALL_TRANSFER.
vk_command_replay_test part D records all 360 D3D12 sync/access pairs as global, buffer and texture
barriers: 462 validation errors before, 0 after, on Intel(R) Graphics. d3d12_state_map_test checks
that stages only grow, accesses never change, and pairs Vulkan already accepts are left alone.
AI-assisted (Claude Code).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closes the gaps left by the barrier/clear slice:
- Rectangle clears: ClearRenderTargetView/ClearDepthStencilView rects are
recorded and replayed with vkCmdClearAttachments in a dynamic-rendering
scope, clipped to the mip.
- LayoutTracker: the current Vulkan layout per image subresource, so barriers
start from the real layout. This honours implicit promotion and decay
(COMMON is GENERAL; simultaneous-access textures stay GENERAL) and, with
separateDepthStencilLayouts, lets one depth/stencil plane be cleared or
transitioned while the other stays in another state.
- Enhanced barriers (global, buffer, texture, split, discard) mapped to
vkCmdPipelineBarrier2.
- vulkan_backend.h: VulkanContext, VulkanQueue (ExecuteCommandLists checks
every list before writing any; Wait is attached to the next submission),
VulkanFence on timeline semaphores.
- d3d12_com.cpp: ID3D12GraphicsCommandList7, ID3D12CommandQueue,
ID3D12CommandAllocator and ID3D12Fence1. Unimplemented methods are stubs
generated at build time from the SDK's d3d12.h (tools/gen_d3d12_stubs.py),
so vtables follow the SDK order and nothing SDK-derived is checked in.
Tests: command_list_test and d3d12_state_map_test (every copied D3D12
constant checked against d3d12.h) in ctest; vk_command_replay_test and the new
d3d12_com_test on the GPU, clean under the Khronos validation layer.
The IR (command_list.h, was clear_commands.h) now records transition, UAV and
aliasing barriers (split barriers record their end half) and full-view
ClearDepthStencilView next to ClearRenderTargetView.
vulkan_replay.h (was vulkan_clear.h) maps D3D12 resource states to Vulkan
layout/access/stage (section 4.3 table), decodes subresource indices including
depth/stencil planes, validates the whole list before emitting anything, and
wraps clears in a layout round trip so images stay in their D3D12 state. The
caller no longer manages layouts by hand.
Tests: command_list_test (recording rules), d3d12_state_map_test (state table,
subresource decoding, constants checked against d3d12.h; needs Vulkan headers
only) and vk_command_replay_test (GPU readback of colour, depth and stencil;
LEONIDA_VK_VALIDATION=1 runs it under the Khronos validation layer).