d3d12 layer: an Upscale command replayed through AMD's PC upscaler (FFX shim, part 1) #150

Closed
Andretuta wants to merge 17 commits from upstream-pr/150 into main
Andretuta commented 2026-11-24 02:29:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-24T02:29:23Z.


Carried over from old PR 9882 by AwkGrep256 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9882, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
AwkGrep256: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Resubmitted unchanged: this was #5139, closed at the repository's move to the organisation (the members-only switch, 22:25–22:49 UTC) with no review comment; reopening it is refused. Same branch and head (c75c03a98), which merges cleanly on main 7e39a81ea. The test reports from other contributors are on #5139.

Item: the FFX upscale shim (claim: https://github.com/ItsNotRexed/Leonida/issues/5032#issuecomment-7095090683), part 1 of 2: the layer side. Part 2 is the shim table that answers the game's five ffx* calls; it calls into this.

Depends on: #9877. This branch carries #9877's commits; this PR's own work is the last two commits, 8af9ed0b (the command) and 3c05c379 (its costs, see Performance).

Draft until #9877 is ready.

What

The game's ffxDispatch becomes one recorded command in its D3D12 command list. ExecuteCommandLists replays it through AMD's PC upscaler on the layer's own VkDevice. This follows the shape of the existing xdxr_record replay hook.

It is off unless LEONIDA_FFX_PC=1. With it off, the device and replay behave as before; nothing new runs.

  • command_list.h: leonida::Upscale holds FfxDispatchDescUpscale's fields, with the images as resource ids. CommandList::upscale records it and checks the required images and sizes.
  • vulkan_replay.h: two hooks, upscale_ready and upscale.
    • The prepare pass resolves the images. A command whose context is gone is skipped, not failed.
    • The emit pass moves the inputs to the NON_PIXEL_SHADER_RESOURCE layout and the output to the UAV layout through the layout tracker, with the same barriers detail::round_trip uses. It records the upscale, then moves every image back.
    • Per-command storage is a vector that stays empty for every other command.
  • vulkan_backend.h: an Upscale counts as shader work. set_upscaler() sets the hooks once, before the device is handed out, because replay reads them without a lock.
  • shims/ffx_upscale.{h,cpp}: the upscale contexts of a device (create, destroy, provider), the hooks, and the FfxApiSurfaceFormat for each VkFormat.
  • d3d12_vulkan_device.cpp, only when the switch is on:
    • enables #9877's wanted extensions, and the core features the upscaler's shaders declare, each where the GPU has it;
    • installs the hooks, and uninstalls them before teardown.
  • d3d12_com: rt::d3d12::record_upscale, which refuses bundles and copy lists.
  • ffx_vk_backend: getenv_s, the way the layer reads its variables (getenv warned under /W4).

Performance

With the upscaler off, which is the default:

  • Memory: nothing. A recorded Command stays 296 bytes, because Upscale (136 bytes) is smaller than Draw (288). A replay entry (detail::Prepared) stays 944 bytes.
  • Time: one more alternative in the existing per-command branch chains, and in runs_shaders. That's O(1) per command.
  • Measured: replay_cpu_bench's frame (37,888 commands, layer only) takes about 4,540 µs, the same as main within noise.
  • What 3c05c379 fixed:
    • 8af9ed0b put a std::vector in every Prepared (944 → 968 bytes, about 0.9 MB more touched per execute of that frame). The images now live in a PreparedList side table, kept between executions like draws. That brought the frame back from about 4,620 µs.
    • It also merged the per-piece barriers, below.

With it on, per Upscale command:

  • Record: a 136-byte copy into the list, O(1).
  • Prepare: at most 7 image resolves (the layer's resource lookup) into the reused side table, with no allocation after the first frame. One registry lookup, an uncontended mutex and a std::map over the contexts, of which there is normally one.
  • Emit:
    • 7 layout-tracker lookups;
    • one vkCmdPipelineBarrier before the upscale and one after, from fixed-size storage with no heap allocation (8af9ed0b issued up to 14);
    • one ffxDispatch.
  • Where the time goes: AMD's GPU work dominates. 720p → 1080p takes 0.49 ms per execute and wait through the layer, versus 0.42 ms calling the upscaler directly (#9877's test), on an RX 9070 XT.

Rare costs:

  • destroy() and device teardown wait for the device to go idle. That only happens on a resolution change or at exit.
  • Each context holds AMD's own internal resources, plus a registry entry of about 100 bytes.

Stability

  • Off by default.
  • destroy() and device teardown wait for the device under the submit lock before freeing a context. That's the pattern the bindless set uses, and it's needed because a VkQueue needs external synchronization. It costs a stall on a resolution change, never per frame.
  • Concurrency: each context has its own lock, so one replay at a time records through it. Lock order is submit lock, then context lock, so there's no cycle.
  • A recorded Upscale whose context was destroyed is skipped. The test covers this.
  • An Upscale without its required images or sizes, or in a bundle or copy list, is refused when recorded. The test covers this too.

Validation

Run with the host's VK_LAYER_KHRONOS_validation beneath Wine, with synchronization validation on and no duplicate limit.

  • Missing core features, found this way. The layer's device didn't enable some core features FSR 4.1.2 declares:
    • storageBuffer16BitAccess (RuntimeSpirv-storageBuffer16BitAccess-11161);
    • variablePointersStorageBuffer and variablePointers;
    • shaderInt8 and shaderFloat16;
    • vulkanMemoryModel and vulkanMemoryModelDeviceScope (RuntimeSpirv-vulkanMemoryModel-06265).
      These are now enabled when the switch is on.
  • iGPU (FSR 3.1.6): no errors and no sync hazards, apart from Wine's own vkCreateDevice-…-01387.
  • RX 9070 XT (FSR 4.1.2): no errors from the layer. The 160 remaining are FSR 4.1.2's own shader errors listed in #9877 (SPV_NV_cooperative_matrix2, which RADV doesn't offer; two spirv-val rejections; 3-component writes).
  • Warnings: storage-image format warnings on FSR's internal images only: rw_recurrent_0 on FSR 4, rw_accumulation, rw_spd_mip* and others on FSR 3.1.

Reviews

  • Kam471: at 8af9ed0b, native Windows 11, RTX 5070 Ti. Both d3d12_ffx_upscale_test and ffx_vk_dispatch_test PASS: FSR 3.1.6, 880/880 (1980/1980 at 4K), 0.41 ms through the layer. The destroyed-context case was skipped, both tests skip with 77 without the DLL, and "no blocking issue". (3c05c379 came after this run; it changes only how replay stores and batches, and gives the same results below.)

Tested

Built with clang-cl 23.1.5 + xwin, run under Wine 11.22 on CachyOS, Mesa 26.3.1 RADV, FidelityFX SDK 2.4.0 DLLs.

d3d12_ffx_upscale_test (new) goes end to end through the layer's own D3D12 objects. Its inputs:

  • committed textures filled by copies;
  • inputs moved to NON_PIXEL_SHADER_RESOURCE;
  • a UAV output.

It records the step with record_upscale 30 times, then reads the output back:

At 3c05c379:

GPU provider per execute + wait result
RX 9070 XT FSR 4.1.2 0.49 ms (0.42 ms called directly) PASS, 880/880, 0 non-finite
Ryzen 7 9800X3D iGPU FSR 3.1.6 10.1 ms PASS, 880/880, 0 non-finite
  • The test can fail: with the upscale call removed from replay, it reports 440/880 and FAIL.
  • Without the DLL it skips (77).
  • Full builds: Linux native and Windows (xwin) both build in full. Linux ctest: 48 of 50 pass, the other 2 skip (corpus, validation layer); that includes the 10 replay and command-list unit tests.
  • Windows d3d12_* tests under Wine, upscaler off: 62 of 68 pass. The same 6 fail when built from main (fence_timing, indirect_dispatch_count, private_data, subresource_copy, heap_properties, residency).
  • Not run yet:
    • a compute-queue list;
    • a depth-format depth input (the test uses R32_FLOAT);
    • native Windows;
    • the game's own DLL path, which comes with part 2.

No game files. AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5464950643 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/150). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T02:29:23Z. --- > Carried over from **old PR 9882** by `AwkGrep256` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9882`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `AwkGrep256`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Resubmitted unchanged: this was `#5139`, closed at the repository's move to the organisation (the members-only switch, 22:25–22:49 UTC) with no review comment; reopening it is refused. Same branch and head (`c75c03a98`), which merges cleanly on `main` `7e39a81ea`. The test reports from other contributors are on `#5139`. Item: the FFX upscale shim (claim: https://github.com/ItsNotRexed/Leonida/issues/5032#issuecomment-7095090683), **part 1 of 2: the layer side**. Part 2 is the shim table that answers the game's five `ffx*` calls; it calls into this. Depends on: `#9877`. This branch carries `#9877`'s commits; this PR's own work is the last two commits, `8af9ed0b` (the command) and `3c05c379` (its costs, see Performance). **Draft until `#9877` is ready.** ### What The game's `ffxDispatch` becomes **one recorded command** in its D3D12 command list. `ExecuteCommandLists` replays it through AMD's PC upscaler on the layer's own `VkDevice`. This follows the shape of the existing `xdxr_record` replay hook. It is **off unless `LEONIDA_FFX_PC=1`**. With it off, the device and replay behave as before; nothing new runs. - **`command_list.h`:** `leonida::Upscale` holds `FfxDispatchDescUpscale`'s fields, with the images as resource ids. `CommandList::upscale` records it and checks the required images and sizes. - **`vulkan_replay.h`:** two hooks, `upscale_ready` and `upscale`. - The **prepare** pass resolves the images. A command whose context is gone is **skipped**, not failed. - The **emit** pass moves the inputs to the NON_PIXEL_SHADER_RESOURCE layout and the output to the UAV layout through the layout tracker, with the same barriers `detail::round_trip` uses. It records the upscale, then moves every image back. - Per-command storage is a vector that stays empty for every other command. - **`vulkan_backend.h`:** an Upscale counts as shader work. `set_upscaler()` sets the hooks once, before the device is handed out, because replay reads them without a lock. - **`shims/ffx_upscale.{h,cpp}`:** the upscale contexts of a device (`create`, `destroy`, `provider`), the hooks, and the FfxApiSurfaceFormat for each VkFormat. - **`d3d12_vulkan_device.cpp`**, only when the switch is on: - enables `#9877`'s wanted extensions, and the core features the upscaler's shaders declare, each where the GPU has it; - installs the hooks, and uninstalls them before teardown. - **`d3d12_com`:** `rt::d3d12::record_upscale`, which refuses bundles and copy lists. - **`ffx_vk_backend`:** `getenv_s`, the way the layer reads its variables (`getenv` warned under `/W4`). ### Performance **With the upscaler off,** which is the default: - **Memory: nothing.** A recorded `Command` stays 296 bytes, because `Upscale` (136 bytes) is smaller than `Draw` (288). A replay entry (`detail::Prepared`) stays 944 bytes. - **Time:** one more alternative in the existing per-command branch chains, and in `runs_shaders`. That's O(1) per command. - **Measured:** `replay_cpu_bench`'s frame (37,888 commands, layer only) takes about 4,540 µs, the same as `main` within noise. - **What `3c05c379` fixed:** - `8af9ed0b` put a `std::vector` in every `Prepared` (944 → 968 bytes, about 0.9 MB more touched per execute of that frame). The images now live in a `PreparedList` side table, kept between executions like `draws`. That brought the frame back from about 4,620 µs. - It also merged the per-piece barriers, below. **With it on, per Upscale command:** - **Record:** a 136-byte copy into the list, O(1). - **Prepare:** at most 7 image resolves (the layer's resource lookup) into the reused side table, with no allocation after the first frame. One registry lookup, an uncontended mutex and a `std::map` over the contexts, of which there is normally one. - **Emit:** - 7 layout-tracker lookups; - **one `vkCmdPipelineBarrier` before the upscale and one after**, from fixed-size storage with no heap allocation (`8af9ed0b` issued up to 14); - one `ffxDispatch`. - **Where the time goes:** AMD's GPU work dominates. 720p → 1080p takes 0.49 ms per execute and wait through the layer, versus 0.42 ms calling the upscaler directly (`#9877`'s test), on an RX 9070 XT. **Rare costs:** - `destroy()` and device teardown wait for the device to go idle. That only happens on a resolution change or at exit. - Each context holds AMD's own internal resources, plus a registry entry of about 100 bytes. ### Stability - **Off by default.** - **`destroy()` and device teardown** wait for the device under the **submit lock** before freeing a context. That's the pattern the bindless set uses, and it's needed because a `VkQueue` needs external synchronization. It costs a stall on a resolution change, never per frame. - **Concurrency:** each context has its own lock, so one replay at a time records through it. Lock order is submit lock, then context lock, so there's no cycle. - **A recorded Upscale whose context was destroyed** is skipped. The test covers this. - **An Upscale without its required images or sizes**, or in a bundle or copy list, is refused when recorded. The test covers this too. ### Validation Run with the host's `VK_LAYER_KHRONOS_validation` beneath Wine, with **synchronization validation** on and no duplicate limit. - **Missing core features, found this way.** The layer's device didn't enable some core features FSR 4.1.2 declares: - `storageBuffer16BitAccess` (`RuntimeSpirv-storageBuffer16BitAccess-11161`); - `variablePointersStorageBuffer` and `variablePointers`; - `shaderInt8` and `shaderFloat16`; - `vulkanMemoryModel` and `vulkanMemoryModelDeviceScope` (`RuntimeSpirv-vulkanMemoryModel-06265`). These are now enabled when the switch is on. - **iGPU (FSR 3.1.6):** no errors and no sync hazards, apart from Wine's own `vkCreateDevice-…-01387`. - **RX 9070 XT (FSR 4.1.2):** no errors from the layer. The 160 remaining are FSR 4.1.2's own shader errors listed in `#9877` (`SPV_NV_cooperative_matrix2`, which RADV doesn't offer; two spirv-val rejections; 3-component writes). - **Warnings:** storage-image format warnings on FSR's internal images only: `rw_recurrent_0` on FSR 4, `rw_accumulation`, `rw_spd_mip*` and others on FSR 3.1. ### Reviews - **Kam471:** at `8af9ed0b`, native **Windows 11**, **RTX 5070 Ti**. Both `d3d12_ffx_upscale_test` and `ffx_vk_dispatch_test` PASS: FSR 3.1.6, 880/880 (1980/1980 at 4K), 0.41 ms through the layer. The destroyed-context case was skipped, both tests skip with 77 without the DLL, and "no blocking issue". (`3c05c379` came after this run; it changes only how replay stores and batches, and gives the same results below.) ### Tested Built with clang-cl 23.1.5 + xwin, run under Wine 11.22 on CachyOS, Mesa 26.3.1 RADV, FidelityFX SDK 2.4.0 DLLs. **`d3d12_ffx_upscale_test`** (new) goes end to end through the layer's own D3D12 objects. Its inputs: - committed textures filled by copies; - inputs moved to NON_PIXEL_SHADER_RESOURCE; - a UAV output. It records the step with `record_upscale` 30 times, then reads the output back: At `3c05c379`: | GPU | provider | per execute + wait | result | |---|---|---|---| | RX 9070 XT | FSR 4.1.2 | 0.49 ms (0.42 ms called directly) | PASS, 880/880, 0 non-finite | | Ryzen 7 9800X3D iGPU | FSR 3.1.6 | 10.1 ms | PASS, 880/880, 0 non-finite | - **The test can fail:** with the upscale call removed from replay, it reports 440/880 and FAIL. - **Without the DLL** it skips (77). - **Full builds:** Linux native and Windows (xwin) both build in full. Linux `ctest`: 48 of 50 pass, the other 2 skip (corpus, validation layer); that includes the 10 replay and command-list unit tests. - **Windows `d3d12_*` tests under Wine, upscaler off:** 62 of 68 pass. The same 6 fail when built from `main` (`fence_timing`, `indirect_dispatch_count`, `private_data`, `subresource_copy`, `heap_properties`, `residency`). - **Not run yet:** - a compute-queue list; - a depth-format depth input (the test uses R32_FLOAT); - native Windows; - the game's own DLL path, which comes with part 2. No game files. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MoonEyes commented 2026-11-30 19:25:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T19:25:53Z.


This is not merge-ready: it remains a draft and mergeStateStatus is DIRTY; all current PR checks are cancelled. This layer-side shim carries and depends on the AMD Vulkan backend from #145, which is still blocked, so the stack order and current-main validation are unresolved. The body still references the old-repository dependency #9877; update it to the current PR reference (Depends on: #145) and rerun CI after rebasing/stacking before requesting merge.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7289379673 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/150#issuecomment-7289379673). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T19:25:53Z. --- This is not merge-ready: it remains a draft and `mergeStateStatus` is `DIRTY`; all current PR checks are cancelled. This layer-side shim carries and depends on the AMD Vulkan backend from #145, which is still blocked, so the stack order and current-main validation are unresolved. The body still references the old-repository dependency `#9877`; update it to the current PR reference (`Depends on: #145`) and rerun CI after rebasing/stacking before requesting merge.
aslan2142 commented 2026-12-01 16:26:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:26:10Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317601179 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/150#issuecomment-7317601179). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:26:10Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!150
No description provided.