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

Open
opened 2026-11-26 20:17:07 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/150
Author: @Andretuta
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: old-pr/9882


📝 Commits (10+)

  • c8d136d runtime: AMD's PC FidelityFX upscaler on the layer's Vulkan device (RT-344)
  • e312253 ffx_vk: enable the extensions the upscaler's shaders use; the test fails on validation errors (#4999 review)
  • be631db ffx_vk_dispatch_test: the console's 1080p configuration and a 4K case
  • 8af9ed0 d3d12 layer: an Upscale command replayed through AMD's PC upscaler (FFX shim, part 1)
  • 3c05c37 replay: Upscale images in a PreparedList side table, and one barrier each way
  • a2766d2 ffx_vk: the loader's choice depends on GPU and driver, not on architecture alone
  • a2fb835 Merge branch 'rt-344-ffx-vk' into ffx-upscale-shim
  • 8abab27 docs: FSR 4.1.2 on RDNA4 under AMD's Windows driver (RX 9060 XT), and the shader errors there too
  • 3e06827 Merge branch 'rt-344-ffx-vk' into ffx-upscale-shim
  • c1e976a ffx_vk: load the provider by full path instead of changing the process DLL search path

📊 Changes

15 files changed (+1533 additions, -8 deletions)

View changed files

📝 docs/d3d12_vulkan.md (+45 -0)
📝 runtime/CMakeLists.txt (+20 -0)
📝 runtime/shims/d3d12_com.cpp (+11 -0)
📝 runtime/shims/d3d12_com.h (+5 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+50 -0)
➕ runtime/shims/ffx_upscale.cpp (+235 -0)
➕ runtime/shims/ffx_upscale.h (+44 -0)
📝 runtime/src/command_list.h (+26 -1)
➕ runtime/src/ffx_vk_backend.cpp (+73 -0)
➕ runtime/src/ffx_vk_backend.h (+84 -0)
📝 runtime/src/vulkan_backend.h (+11 -1)
📝 runtime/src/vulkan_replay.h (+134 -1)
➕ runtime/test/d3d12_ffx_upscale_test.cpp (+307 -0)
📝 runtime/test/ffx_api_min.h (+25 -5)
➕ runtime/test/ffx_vk_dispatch_test.cpp (+463 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/150 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `old-pr/9882` --- ### 📝 Commits (10+) - [`c8d136d`](https://github.com/ubiquitarianism/leonida/commit/c8d136d45d5b40bbfeca9a2ecaf9ef744d090110) runtime: AMD's PC FidelityFX upscaler on the layer's Vulkan device (RT-344) - [`e312253`](https://github.com/ubiquitarianism/leonida/commit/e312253b950962d74afa6f8aa35b13b3b40db3a5) ffx_vk: enable the extensions the upscaler's shaders use; the test fails on validation errors (#4999 review) - [`be631db`](https://github.com/ubiquitarianism/leonida/commit/be631db9dc45a8c444fe33b951309fb7ddc2e511) ffx_vk_dispatch_test: the console's 1080p configuration and a 4K case - [`8af9ed0`](https://github.com/ubiquitarianism/leonida/commit/8af9ed0b783a6c9a37812a1168b19bf21e1e4525) d3d12 layer: an Upscale command replayed through AMD's PC upscaler (FFX shim, part 1) - [`3c05c37`](https://github.com/ubiquitarianism/leonida/commit/3c05c3793963c6e9bcf7eb51c3018bade9195ee7) replay: Upscale images in a PreparedList side table, and one barrier each way - [`a2766d2`](https://github.com/ubiquitarianism/leonida/commit/a2766d202e54eca1a66bd5f3b0e796e4937f59fa) ffx_vk: the loader's choice depends on GPU and driver, not on architecture alone - [`a2fb835`](https://github.com/ubiquitarianism/leonida/commit/a2fb8358a57132b0c5083050780f6bf4369480ba) Merge branch 'rt-344-ffx-vk' into ffx-upscale-shim - [`8abab27`](https://github.com/ubiquitarianism/leonida/commit/8abab278ba25ea99ed5ac7e38df6ccd26c2d1ee6) docs: FSR 4.1.2 on RDNA4 under AMD's Windows driver (RX 9060 XT), and the shader errors there too - [`3e06827`](https://github.com/ubiquitarianism/leonida/commit/3e06827b1f22a6d82174ebc1a6a2ab909d8316c5) Merge branch 'rt-344-ffx-vk' into ffx-upscale-shim - [`c1e976a`](https://github.com/ubiquitarianism/leonida/commit/c1e976a0e1c093f209c931e060848097556205c0) ffx_vk: load the provider by full path instead of changing the process DLL search path ### 📊 Changes **15 files changed** (+1533 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `docs/d3d12_vulkan.md` (+45 -0) 📝 `runtime/CMakeLists.txt` (+20 -0) 📝 `runtime/shims/d3d12_com.cpp` (+11 -0) 📝 `runtime/shims/d3d12_com.h` (+5 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+50 -0) ➕ `runtime/shims/ffx_upscale.cpp` (+235 -0) ➕ `runtime/shims/ffx_upscale.h` (+44 -0) 📝 `runtime/src/command_list.h` (+26 -1) ➕ `runtime/src/ffx_vk_backend.cpp` (+73 -0) ➕ `runtime/src/ffx_vk_backend.h` (+84 -0) 📝 `runtime/src/vulkan_backend.h` (+11 -1) 📝 `runtime/src/vulkan_replay.h` (+134 -1) ➕ `runtime/test/d3d12_ffx_upscale_test.cpp` (+307 -0) 📝 `runtime/test/ffx_api_min.h` (+25 -5) ➕ `runtime/test/ffx_vk_dispatch_test.cpp` (+463 -0) </details> ### 📄 Description > 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#202
No description provided.