Commit Graph

19 Commits

Author SHA1 Message Date
9d47be3dec Runtime: no heap allocation per barrier, clear or copy in replay
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>
2026-11-22 09:19:29 -06:00
69b21a9427 Merge origin/main (c2967a5e) into d3d90-castable-placed
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.
2026-11-22 03:15:32 -08:00
41dccfb3e4 Merge pull request #1339 from mrb-eel 2026-11-22 05:37:48 -05:00
3b7cbf46c7 Resolve indirect arguments relative to Vulkan backing buffers 2026-11-22 11:22:14 +01:00
9c2c6918f1 fix(vulkan): synchronize physical constant-buffer reads
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.
2026-11-22 11:08:53 +01:00
2933767eac Merge origin/main into d3d90-castable-placed (resolve #1083 conflicts)
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).
2026-11-21 23:23:18 -08:00
6b8b2e5f6e Merge pull request #865 from Felix Croasdale 2026-11-21 23:15:36 -05:00
a03f0c5e13 d3d12_state_map_test: DiscardResource subresource ranges through prepare()
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>
2026-11-21 22:03:26 -06:00
Ryx
1267591716 Runtime: only allocate from a memory heap that can hold the allocation (arena without Resizable BAR)
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.
2026-11-22 05:01:36 +01:00
d868cba00f Runtime: barriers into PREDICATION cover conditional rendering's read
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>
2026-11-21 21:55:19 -06:00
2474686591 Merge pull request #815 from Felix Croasdale 2026-11-21 22:37:48 -05:00
7079108182 Runtime: query heaps on Vulkan (D3D-15)
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>
2026-11-21 21:30:47 -06:00
039df7d24f Runtime: upload heaps in Resizable BAR memory when the GPU has it (D3D-96)
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>
2026-11-21 21:26:20 -06:00
2cb28a4239 feat(runtime): clear selected slices of 3D render targets
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.
2026-11-22 02:29:34 +01:00
0b29a78949 Keep barrier state tests compatible with newer Windows SDKs
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.
2026-11-22 01:28:06 +01:00
44a94f35ca Runtime: widen enhanced-barrier stages so every access has a stage that can perform it
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>
2026-11-21 23:42:05 +01:00
52e1ac3cd7 Support mipmapped 3D texture transfers on the Vulkan device 2026-11-21 21:24:04 +01:00
Ryx
6052bf0bd5 Runtime: rectangle clears, layout tracking, enhanced barriers, COM list/queue/fence
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.
2026-11-21 19:19:31 +01:00
Ryx
7f6e8f059d Runtime: D3D12 barriers and depth-stencil clears in the command-list IR
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).
2026-11-21 18:58:48 +01:00