Skip to content

runtime: order consecutive buffer copies on the same queue (RT-STREAM-TRANSFER-BARRIER) - #1099

Merged
QemmHD merged 1 commit into
ProjectLeonida:mainfrom
Moriz82:split/rt-stream-transfer-barrier
Nov 25, 2026
Merged

QemmHD merged 1 commit into
ProjectLeonida:mainfrom
Moriz82:split/rt-stream-transfer-barrier

Conversation

@Moriz82

@Moriz82 Moriz82 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: RT-STREAM-TRANSFER-BARRIER (claim).

Part of the split of #984 into small pull requests.

Depends on: none
Overlaps: #354 (closed without merging) supplies the transfer dependency. This child credits that contribution, adds the rollback switch and tests the emitted commands.

What and why

A D3D12 queue can submit overlapping buffer copies back to back. Without a Vulkan memory dependency, a later copy can read or overwrite bytes before the earlier transfer write finishes. Emit a transfer-stage memory barrier before each plain buffer copy, including the first one in a list. A preceding list on the same queue can have written those bytes. Real Vulkan contexts provide the barrier entry point. The existing query mock also exercises a context that omits it. Set LEONIDA_ORDER_BUFFER_COPIES=0 to restore the old behavior.

Tested

Cross-built with clang-cl 23 and the xwin SDK. Ran the command-recording fixtures under Proton Experimental (Wine 11) on CachyOS Linux 7.3, Ryzen 5 5600X, 16 GB.

cmake --build runtime/build/boot --target gta6rt replay_copy_offset_test replay_query_edges_test -j6
ctest --test-dir runtime/build/boot -j1 -R '^replay_(copy_offset|query_edges)$' --output-on-failure
(cd runtime/build/boot && LEONIDA_ORDER_BUFFER_COPIES=0 "$WINE" replay_copy_offset_test.exe --expect-unordered)
Configuration Single copy ordered Back-to-back copies ordered Result
Main plus tests 0/1 0/2 Both ordering assertions failed.
Child, default 1/1 2/2 Both replay fixtures passed.
Child, switch set to 0 0/1 0/2 The explicit unordered control passed.

GPUs tested: none. The fixtures record Vulkan commands and check the stage and access masks. Native Windows, AMD, Intel and a game boot were not tested.
AI-assisted: yes

Checklist

  • One commit on upstream main at the cut. Clean merge check.
  • One copy-ordering change with a regression test and a rollback control.
  • Changed-file game-data guard and whitespace check passed.
  • No translator rule changed.

🤖 Generated with Claude Code

…-TRANSFER-BARRIER)

Vulkan transfer writes can overlap a later read or write when D3D12 submits
plain buffer copies in sequence.

Emit the transfer memory dependency from natterman12's #354 (GPL-3.0) before
every plain buffer copy. Allow an explicit rollback for comparison, and
record the access masks in the synthetic replay fixture.

The base failed both copy-ordering assertions. The child passed both replay
fixtures, and the rollback control observed no ordering barrier.


AI-assisted (Codex and Claude Code agents).

Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Plaaasma

Copy link
Copy Markdown
Contributor

Native Windows check, AI-assisted (Claude Code). Windows 11, i9-13900K, 64 GB, RTX 4070 Ti 12 GB (NVIDIA 630.20), MSVC 2022 (VS 17 generator, Release), Vulkan.

Head ddcffa634. Built replay_copy_offset_test, then ctest -C Release -R "^replay_copy_offset$" --output-on-failure --timeout 300:

  • replay_copy_offset: passed, 0.06 s
  • replay_copy_offset_test --expect-unordered on the PR build: exits 1, as intended

Cross-check: the PR's test built against the base's runtime/src/vulkan_replay.h fails both new checks (a buffer copy is ordered after earlier transfer writes ..., each back-to-back buffer copy is ordered after the transfer write before it) and passes with --expect-unordered, so the test does catch the missing barrier.

1 passed, 0 failed.

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

Audited and verified. Emits TRANSFER_WRITE -> TRANSFER_READ | TRANSFER_WRITE transfer barrier before �kCmdCopyBuffer in
untime/src/vulkan_replay.h, matching D3D12's implicit queue ordering on back-to-back buffer copies. Configurable via LEONIDA_ORDER_BUFFER_COPIES. Tests in
untime/test/replay_copy_offset_test.cpp pass cleanly. Approved.

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

Clean-Room Audit — PR #1099 (RT-STREAM-TRANSFER-BARRIER)

Well-scoped transfer ordering barrier (TRANSFER_WRITE -> TRANSFER_READ | TRANSFER_WRITE) before queue buffer copies in vulkan_replay.h, preserving LEONIDA_ORDER_BUFFER_COPIES=0 escape hatch and adding regression tests in replay_copy_offset_test.cpp. 0 game data.

Verdict: APPROVED

@iFeyz

iFeyz commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

AMD run (the gap the description lists): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, MSVC 2022 RelWithDebInfo.

Head ddcffa634: replay_copy_offset built from it passes.

@Deedlyy

Deedlyy commented Nov 24, 2026

Copy link
Copy Markdown

Recommendation: [MERGE]

Reasoning: Emits a transfer-stage memory barrier between back-to-back buffer copies on the same queue, fixing a real RAW/WAW hazard in the Vulkan replay, with a rollback switch and a test that fails on the base header. Verified on NVIDIA (Plaaasma, including a negative control) and AMD RX 9070 XT (iFeyz). Synchronization changes affect every title copy, so the rollback switch is useful. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main. Merge once the queued checks are green.

Impact level: 5/10

@whoalin1

Copy link
Copy Markdown
Contributor

GPU verification (AI-assisted, overnight worker w4; independent run, not the author's).

Head tested: ddcffa6 on this machine's two GPUs.

AMD Radeon iGPU (gfx1035, RDNA2, driver 24.10.24, Vulkan 1.3.280) — env: VK_LOADER_DRIVERS_SELECT=*amd-vulkan* LEONIDA_VK_DEVICE=radeon VK_LOADER_LAYERS_DISABLE=~implicit~ LEONIDA_VK_VALIDATION=1 LEONIDA_PC_TAIL_UBO=1

NVIDIA RTX 4070 SUPER — env: VK_LOADER_DRIVERS_SELECT=*nv-vk* LEONIDA_VK_DEVICE=4070 VK_LOADER_LAYERS_DISABLE=~implicit~

  • ctest -R "d3d12|vk_" -j4: 141/142 passed; only d3d12_execute_indirect_validation failed with 5 parity cases — the same 5 failures reproduce on unmodified main (verified on the baseline build), so pre-existing.
  • run_gpu_tests.py: 203/252 passed, 49 skipped, 0 failed.

Verdict: no regressions found on either GPU; barrier ordering holds under validation on the AMD iGPU. LGTM from this hardware.

@moffington

Copy link
Copy Markdown
Contributor

Independent GPU verification at ddcffa634 on Linux, RTX 3060, NVIDIA 615.78.08.

Added a synthetic chained-copy/readback probe covering intra-list and same-batch cross-list dependencies on both direct and copy queues.

Configuration SyncVal errors Incorrect bytes
Cut base 3aa975421 10 Up to 65536/65536
PR HEAD 0 0
HEAD with LEONIDA_ORDER_BUFFER_COPIES=0 10 Corruption reproduced

This confirms actual GPU data corruption from the missing dependency, beyond the command-recording assertions. All ten cases pass with the fix enabled.

No blocking issues found. AI-assisted testing.

@QemmHD

QemmHD commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Merging.

Why merged: RT-STREAM-TRANSFER-BARRIER: a transfer-write → transfer-read/write barrier goes before every vkCmdCopyBuffer, so back-to-back streamed copies are ordered. moffington measured SyncVal at 10 errors on the base and 0 on the head. Plaaasma (4070 Ti), iFeyz (RX 9070 XT) and whoalin1 (gfx1035) passed it. LEONIDA_ORDER_BUFFER_COPIES=0 turns it off (documented in docs/d3d12_vulkan.md).

Why it's good: replay_, vk_bc_zero_init, vk_queue and d3d12_close_parity: 16/16 both plain and under validation. A negative control on the cumulative build checked that the test really catches a missing barrier: with the barrier on, --expect-unordered fails; with the env var at 0, the ordered check fails and --expect-unordered passes. It also passes on top of #1055's vulkan_replay.h change. It adds one barrier per buffer copy, a small sync-policy cost with an opt-out.

Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Merged alone onto main 0f98d19, its ctests pass both plain and with LEONIDA_VK_VALIDATION=1. Cumulative with the rest of this batch on main 507029d: the repo-consistency set gives 578 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 317/318 with the usual 11 skips; the only failure is main's pre-existing d3d12_depth_resolve. All d3d12_* tests under validation: 119/120 with 0 VUIDs. The semantic suite has only the known environment-only failure. git merge-tree against current main 9c5e706: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes.

AI-assisted (Claude Code, Claude Opus 5.5).

@QemmHD
QemmHD merged commit fec45f0 into ProjectLeonida:main Nov 25, 2026
1 check failed
clquwu pushed a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…984 LAYOUT_UNDEFINED, Wave-21b CMake & leonida-ce 525c44e1 ReadFile fast-path

- ProjectLeonida/mandy#1100 (RT-PLACED-IMAGE-DEFER): unify Resource deferred memory binding onto leonida::DeferredImageBinding (deduping duplicate flags/mutex), add LEONIDA_PLACED_IMAGE_DEFER=0 rollback, defer placed depth/UAV/RT attachments + cpu_copy bind + race-free memory_flags(), preserve MemoryPriority, add check_placed_target_backing test & docs/d3d12_vulkan.md note.
- ProjectLeonida/mandy#1099 (RT-STREAM-TRANSFER-BARRIER): deduped against existing ultimate CopyBuffer/CopyResource transfer barriers; add LEONIDA_ORDER_BUFFER_COPIES=0 opt-out toggle, --expect-unordered test mode, and docs/d3d12_vulkan.md note.
- ProjectLeonida/mandy#1097 (SHADER-BUFFER-BOUNDS): use OobSelect::structured_with_offset for structured buffer SRVs in d3d12_vulkan_device.cpp, add cross-record GPU readbacks in d3d12_compute_test.cpp + cs_user_data.comp (and align Xbox slot-17 CBV expectations with wave18b vector-row encoding).
- ProjectLeonida/mandy#1096 (RT-STARTUP-TRACE): add VirtualQuery memory region metadata on access violations in startup_trace.h & startup_trace_test.cpp.
- ProjectLeonida/mandy#1023 & #1007: update docs/directstorage.md (measured factory IID & slot 8) and docs/d3d12_vulkan.md (device slot 62 5-arg arity & slot 76 FFX-only note).
- Wave-21b deferred CMake: add loader TIMEOUT 180 (Reforge#169) and tiny_game_ga / run_tiny_game_ga_tls (ubiq#947 Patch 2).
- ProjectLeonida/mandy#984 (self-contained Windows-native port): accept enhanced TextureBarrier into LAYOUT_UNDEFINED without violating VUID-VkImageMemoryBarrier2-newLayout-01198 (command_list.h, vulkan_replay.h, command_list_test, replay_texture_barrier_test, d3d12_close_parity_test).
- IAmNotSiob/leonida-ce 525c44e1 (GPL-3.0, deduped): wire optional xmem::commit_physical_range_fn hook into file_system.cpp ReadFile ERROR_NOACCESS retry path to commit large console buffers without per-page VEH faults while keeping zero-cost fast path and touch_pages fallback.
- DesignWithTommy/Neonbridge eaf7e7e0: audited and skipped (ultimate has no refresh read-back byte/time cap skipping Bink R8/R8G8 planes).
@Gefik23

Gefik23 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tested ddcffa6 on NVIDIA GeForce RTX 4060 (Ada), driver 617.42, Windows 11 Pro build 26200, native Windows: pass.

Built at this head with scripts\build_runtime.ps1 (MSVC 19.44, VS 2022 Build Tools, Ninja, RelWithDebInfo, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0).

AI-assisted (Claude Code ran the build and tests). No game files.

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.

9 participants