Skip to content

Runtime: apply backing-buffer offsets to CopyResource and CopyTexture - #1378

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/copy-buffer-offsets
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/copy-buffer-offsets

Conversation

@emirhan-karaca

Copy link
Copy Markdown
Contributor

Claimed in #1218: #1218 (comment)

What and why

ResourceBinding::buffer_offset locates a D3D12 buffer inside a shared backing VkBuffer (the GPU address arena). CopyBuffer, AtomicCopyBuffer, ClearUnorderedAccess, Predication, ResolveQueries, and WriteBufferImmediate all apply �uffer_offset, but CopyResource (buffer-to-buffer) and CopyTexture (footprint buffer <-> texture, including depth24_as_float) still emitted resource-relative offsets directly:

  • prepare_resource_copy (
    untime/src/vulkan_replay.h) emitted VkBufferCopy{0, 0, p.source.size} without adding p.source.buffer_offset / p.binding.buffer_offset, without checking �uffer_offset + size overflow or zero-size copies, and without rejecting overlapping ranges inside a shared backing VkBuffer (VUID-vkCmdCopyBuffer-pRegions-00117).
  • prepare_texture_copy (
    untime/src/vulkan_replay.h) set
    egion.bufferOffset = offset without adding �uffer.buffer_offset, checked texel/depth alignment only against the resource-relative offset, and did not check �uffer.buffer_offset + buffer.size overflow.
  • prepare()'s depth24_as_float path (
    untime/src/vulkan_replay.h) set job.app_size = buffer.size instead of �uffer.buffer_offset + buffer.size, causing Depth24Converter::prepare (whole.app_offset + span_words * 4 > whole.app_size) to reject suballocated buffers once
    egion.bufferOffset includes �uffer.buffer_offset.

This PR applies �uffer_offset with overflow, alignment, and same-buffer overlap checks in both paths, updates job.app_size = buffer.buffer_offset + buffer.size, adds a CPU regression test (
eplay_copy_offset_test, registered in
untime/CMakeLists.txt), and extends depth24_convert_test with a shared-arena GPU test covering CopyResource, RGBA8 CopyTexture, and depth24_as_float CopyTexture (including a non-256-aligned �uffer_offset exercising �pp_base_words != 0).

AI-assisted implementation and testing.

Tested

Compiler: w64devkit GCC 16.2.0 (-std=c++20 -O2 -Wall -Wextra -Wpedantic -Wno-missing-field-initializers), Khronos Vulkan-Headers 1.4.363.0, Windows 11 x64.

  • Negative control (unmodified c0ac5fd �ulkan_replay.h with new tests):

eplay_copy_offset_test: failed 15 assertions across CopyResource offsets/overlap/zero-size/overflow, RGBA8 CopyTexture upload/readback offsets/alignment/overflow, and depth24_as_float �pp_offset/�pp_size/alignment.

  • depth24_convert_test on Intel(R) UHD Graphics (101.7092) and NVIDIA GeForce RTX 5050 Laptop GPU (621.24): failed on both GPUs at line 440: arena_ptr[copy_off / 4 + i] == src_word.
  • With fix (�8bd9f2):
    • CPU tests (6/6 pass):
      eplay_copy_offset_test, predicated_copy_test,
      eplay_uav_clear_offset_test,
      eplay_clear_gate_test, command_list_test,
      eplay_cpu_bench --self-test.
    • GPU tests on Intel(R) UHD Graphics (

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested b8bd9f2 on an NVIDIA GeForce RTX 3070, driver 610.88, Windows 11 (26300), MSVC 19.44 (VS generator, RelWithDebInfo, /W4), Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1: pass. Your verification was with GCC, so this covers MSVC and a GPU run of the new arena case.

  • Build has 0 warnings. ctest (validation on): 61/61, including the new replay_copy_offset.
  • depth24_convert_test: exit 0, 0 validation messages, including shared-buffer offsets (CopyResource, RGBA8 and depth24 CopyTexture) ok. With synchronization validation on (VK_KHRONOS_VALIDATION_VALIDATE_SYNC=true): 3/3 runs, 0 hazards.
  • vk_command_replay_test A to G, d3d12_vulkan_device_test, d3d12_com_test: exit 0, 0 validation messages.

Negative control: I built both new tests against this commit's parent vulkan_replay.h (tests unchanged):

  • replay_copy_offset_test: exit 1, every CopyResource and CopyTexture offset check fails, starting with CopyResource applies src and dst buffer_offset.
  • depth24_convert_test: exit 1. The old CopyResource copied the arena onto itself at offset 0 -> 0. Validation reported VUID-vkCmdCopyBuffer-pRegions-00117 and a READ_AFTER_WRITE hazard, and the data check arena_ptr[copy_off / 4 + i] == src_word failed.

With the fix restored, both pass again.

Code read:

  • prepare_texture_copy still checks alignment on the resource-relative offset and now also on buffer_offset + offset, with the overflow check before the add.
  • The CopyResource overlap test matches the one CopyBuffer already uses.
  • Replay barriers are global (no per-buffer ranges), so nothing else needs the offset.
  • In the depth-24 path, app_size = buffer_offset + size keeps Depth24Converter's end check on this resource's slice. Its 256-aligned window may start before the slice, but the shader writes only from app_base_words within the span, so a neighbouring slice is never written. Your guard sentinels show that too.

Looks good to me.

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Please apply the adjusted backing-buffer end to the RGB96 conversion job too when updating this PR to current main. #1306 has merged and introduced a second job initializer for RGB96. It still sets job.app_size = buffer.size, while this PR puts an absolute backing-buffer byte position in job.app_offset.

I combined b8bd9f20 with #1306's e6f8478e locally (clean merge, tree b57dcd67). A source-built VulkanQueue case copied a 17×3 RGB96 texture from a 1,536-byte logical buffer into another. The footprint begins 512 bytes into each logical buffer and uses a 256-byte row pitch. The input and output backing buffers each have 8,192 bytes. I varied their suballocation bases independently:

Upload base Readback base Result on both RTX 5080 and integrated Radeon
0 0 All 51 texels and readback guards matched
2048 0 Upload returned ReplayResult::unsupported
0 4096 Upload succeeded, readback returned ReplayResult::unsupported

With the one-line change to job.app_size = buffer.buffer_offset + buffer.size in the RGB96 branch, all three cases matched on both GPUs, including all three words per texel and every untouched word in the readback buffer. This is a combined-branch result. The RGBA8 and D24 cases in your original test still passed.

@emirhan-karaca
emirhan-karaca force-pushed the fix/copy-buffer-offsets branch from b8bd9f2 to 4c9f41d Compare November 22, 2026 11:10
@emirhan-karaca

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (c2967a5) and updated the image.rgb96 path from #1306 as well (head 4c9f41d):

  • Thanks @Vonsek for catching the image.rgb96 branch — updated job.app_size = buffer.buffer_offset + buffer.size there too, and added both CPU (replay_copy_offset_test) and shared-arena GPU (depth24_convert_test, 17×3 RGB96 with 512-byte footprint offset, 256-byte pitch, independent nonzero upload/readback buffer_offsets including a non-256-aligned readback offset, and guard sentinels) regression coverage.
  • Thanks @Parakeet-Wheat for the MSVC /W4 and synchronization-validation check!

Verification of 4c9f41d (w64devkit GCC 16.2.0, C++20, Vulkan-Headers 1.4.363.0, Windows 11 x64):

  • Negative control (before the one-line rgb96 app_size fix): replay_copy_offset_test failed all 4 RGB96 offset assertions and depth24_convert_test failed with ReplayResult::unsupported on the shared-arena RGB96 copy.
  • With 4c9f41d: 8/8 CPU tests pass (replay_copy_offset_test, predicated_copy_test, replay_uav_clear_offset_test, replay_clear_gate_test, replay_query_predication_test, replay_write_immediate_predication_test, command_list_test, replay_cpu_bench --self-test), and 4/4 GPU tests pass on both Intel(R) UHD Graphics (101.7092) and NVIDIA GeForce RTX 5050 Laptop GPU (621.24): depth24_convert_test (shared-buffer offsets (CopyResource, RGBA8, depth24 and rgb96 CopyTexture) ok), vk_command_replay_test, vk_predicated_copy_test, vk_predicate_capture_test.

@Rainmaker33

Copy link
Copy Markdown

Tested b8bd9f2 on NVIDIA GeForce RTX 3090 (Ampere), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300: pass.

The PR's diff applied cleanly to main f85582e. VS 2022, Vulkan SDK 1.4.363.0. Runtime built with scripts\build_runtime.ps1 (CMake, RelWithDebInfo); ctest run with LEONIDA_VK_VALIDATION=1, no VUIDs.

@ianneobot-cyber

Copy link
Copy Markdown

Tested b8bd9f2 on Windows 11 (native, MSVC 17.14 with Ninja, Vulkan SDK 1.4.363), NVIDIA RTX 3070 Ti Laptop (621.14), LEONIDA_VK_VALIDATION=1: pass

Second NVIDIA/MSVC confirmation after @Parakeet-Wheat's RTX 3070 run.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): pass. ctest: 66/66 on the RTX 4080 SUPER, including the new replay copy offsets test. The Intel UHD 770 failures are the known ones (vk_binary_occlusion fixed by #1321, d3d12_vulkan_device on this older base, d3d12_descriptor min-LOD).

@bvtterbroot

Copy link
Copy Markdown

Tested 4c9f41d on AMD Radeon RX 7900 XTX (RDNA3, Navi 31), AMD driver 26.11.2, Vulkan 1.4.359, Windows 11: pass.

  • ctest -R "replay_copy_offset|depth24_convert": both passed (LEONIDA_VK_DEVICE=7900).
  • run_gpu_tests.py: 135/135.

Tools: llvm-mc 21.1.8 (WSL), Vulkan SDK 1.4.363.0, MSVC 14.44. main c2967a5 on the same machine: run_gpu_tests.py 135/135.

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested b8bd9f2 merged onto current main (c2967a5) (clean merge) on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200, MSVC 19.50, Vulkan SDK 1.4.363.0: pass.

  • ctest 80/80, including the new replay_copy_offset and depth24_convert (both passed on the GPU)

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

@emirhan-karaca I reran the original RGB96 reproduction on your published 4c9f41d77c1445bcaf0539121ea030b6c9d114d7, using the production replay and a separate set of resource IDs. The previously failing upload-only offset (2048, 0) and readback-only offset (0, 4096) both pass on RTX 5080 and integrated Radeon, as does the (0, 0) control. All 51 three-word texels and every untouched readback word matched in all six runs. This resolves the RGB96 issue I reported above.

@emirhan-karaca
emirhan-karaca force-pushed the fix/copy-buffer-offsets branch from 4c9f41d to 624f42e Compare November 22, 2026 11:31
@emirhan-karaca

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (bb2e1842, head 624f42e2):

  • The original commit b8bd9f2 already landed on main via ce29a787, so this PR now contains only the remaining image.rgb96 follow-up (job.app_size = buffer.buffer_offset + buffer.size) plus its CPU/GPU regression tests.
  • The previous windows CI failure was the unrelated d3d12_present headless-runner failure (replay_copy_offset itself passed 72/82), which main already fixed in de47dbd0 — this rebase picks up that fix so CI runs cleanly.
  • Re-verified 624f42e2 on CPU (replay_copy_offset_test) and both Intel(R) UHD Graphics (101.7092) and NVIDIA GeForce RTX 5050 Laptop GPU (621.24) (depth24_convert_test).

@Urban225

Copy link
Copy Markdown
Contributor

Tested 4c9f41d on AMD Radeon RX 7900 XTX (RDNA3), AMD proprietary 26.10.1, Windows 11, MSVC 19.44: pass.

  • ctest --test-dir runtime\build: 79/80. The new replay_copy_offset passes. The one failure, d3d12_r9g9b9e5_uav (line 399), is not from this PR: main at c2967a5 fails the same way on this card.
  • python -I scripts\run_gpu_tests.py: 135/135, same as main.

Not covered: this PR also changes depth24_convert_test.cpp, which is not a ctest entry, and I did not run it by hand.

AI-assisted (Claude wrote the test script and read the logs); the runs are from my machine.

@ItsNotRexed
ItsNotRexed merged commit 7e47b12 into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
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.

10 participants