fix(vk): validate texture blits before vkCmdBlitImage #1241

Open
jcabarca wants to merge 1 commit from jcabarca/Longshot:fix/vk-blit-format-checks into main
Contributor

Type: fix · Risk: low · Tested on: Windows, AMD Radeon RX 9070 XT · Needs: none

Why

A scaled or converting texture copy records vkCmdBlitImage for any pair of formats, so a pair the device cannot blit (no BLIT_SRC or BLIT_DST, integer with float, multisampled, or linear filtering without filter support) gives an undefined result, from garbage to a lost device (VUID-vkCmdBlitImage-srcImage-01999 and its neighbours below).

The same fix was first sent as part of #287 together with other changes; this PR sends it alone, one fix per PR.

How to reproduce

Seen on: not seen in the game: found by reading copy_t2t() in gpu/vk/vk_xfer.cpp; it happens whenever the game asks for a scaled or format-converting texture copy between formats the device cannot blit
Settings and switches: defaults
Steps: game-free: python -I -B tests/test_blit_format_checks.py with this PR's test on a main checkout
Expected: the blit is recorded only when every rule below holds; otherwise it is skipped with a warning
Actual: on main copy_t2t() records the blit with no check; the test's 3 cases fail (2 failures, 1 error)

What changed

Before vkCmdBlitImage, copy_t2t() now checks what Vulkan requires of a blit and skips it with a warning (warn(13, ...)) when the device cannot make it; linear filtering is used only where the source format supports it.

  • vk: gpu/vk/vk_xfer.cpp: format_class() (new: float/normalized, unsigned or signed integer); copy_t2t() checks BLIT_SRC/BLIT_DST in tiling_features(), the same format class on both sides, the same format when not colour, single-sampled images, and SAMPLED_IMAGE_FILTER_LINEAR (and a float class) before choosing VK_FILTER_LINEAR.
  • tests: tests/test_blit_format_checks.py (new): reads copy_t2t() and checks each condition is tested before the blit is recorded.

The bug, step by step:

  1. copy_t2t() takes the blit path for any scaled or converting copy (else after the same-size vkCmdCopyImage).
  2. It fills VkImageBlit and calls vkCmdBlitImage with VK_FILTER_LINEAR when the game asked for filtering, without looking at either format's features, class or sample count.
  3. The formats come from the game's textures, so any pair can arrive; each rule below is then broken by the pairs it names.

The rules:

  • VUID-vkCmdBlitImage-srcImage-01999: "The format features of srcImage must contain VK_FORMAT_FEATURE_BLIT_SRC_BIT"
  • VUID-vkCmdBlitImage-dstImage-02000: "The format features of dstImage must contain VK_FORMAT_FEATURE_BLIT_DST_BIT"
  • VUID-vkCmdBlitImage-srcImage-00229: "If either of srcImage or dstImage was created with a signed integer VkFormat, the other must also have been created with a signed integer VkFormat"
  • VUID-vkCmdBlitImage-srcImage-00230: "If either of srcImage or dstImage was created with an unsigned integer VkFormat, the other must also have been created with an unsigned integer VkFormat"
  • VUID-vkCmdBlitImage-srcImage-00231: "If either of srcImage or dstImage was created with a depth/stencil format, the other must have exactly the same format"
  • VUID-vkCmdBlitImage-srcImage-00233: "srcImage must have been created with a samples value of VK_SAMPLE_COUNT_1_BIT"
  • VUID-vkCmdBlitImage-dstImage-00234: "dstImage must have been created with a samples value of VK_SAMPLE_COUNT_1_BIT"
  • VUID-vkCmdBlitImage-filter-02001: "If filter is VK_FILTER_LINEAR, then the format features of srcImage must contain VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT"

Format features come from vkGetPhysicalDeviceFormatProperties (tiling_features(), optimal tiling), so the check follows the device. A depth/colour pair is skipped because non-colour copies need identical formats (00231).

Measurements

Verdict: it fixes the bug: tests/test_blit_format_checks.py fails on main and passes with this PR, in both interleaved runs. No speed change is expected: the change is on a failure or check path.

Hardware: AMD Radeon RX 9070 XT, Windows; the test itself is game-free (it runs on the CPU, compiled with clang++ from llvm-mingw where it builds a fixture); Python 3.12
Scene: none, game-free
Method: this PR's test files on each side's checkout, 2 runs each, interleaved (main, PR, main, PR); the result is pass/fail, so the median is the result both runs gave
How to reproduce: python -I -B tests/test_blit_format_checks.py on each side

Before: main @ ac778751 After: this PR @ b5bd6ecc
tests/test_blit_format_checks.py, run 1 fails (3 cases: 2 failures, 1 error) passes (3 of 3 pass)
tests/test_blit_format_checks.py, run 2 fails (3 cases: 2 failures, 1 error) passes (3 of 3 pass)
Median fails passes
run 1, main @ ac778751: exit 1: FAILED (failures=2, errors=1)
run 1, this PR @ b5bd6ecc: exit 0: OK
run 2, main @ ac778751: exit 1: FAILED (failures=2, errors=1)
run 2, this PR @ b5bd6ecc: exit 0: OK

Flags and settings

None.

Testing

Tested: on this branch, Windows, Python 3.12, clang++ from llvm-mingw: tests/test_blit_format_checks.py passes (fails on main).

Built and played: this commit builds on Windows (llvm-mingw back end) and on Linux (longshot_vk.so, without and with AMD FSR 4). Played on Windows, AMD Radeon RX 9070 XT: the game runs normally. (The build that was played had this change before a whitespace-only fix: indentation, no code change.)

Not tested: the failure path itself in the game (it needs a driver failure or a format pair the RX 9070 XT does not produce on demand). Linux was built, not run.

Must be tested: None: the change is the same on every GPU and OS, and the game-free test covers it.

Risks

What could break: a blit the device cannot make is now skipped, so the destination keeps its old contents instead of an undefined result; a game that relied on such a blit (only on drivers that happened to accept it) would see the old texture there.

Turning it off: revert the commit.

Compatibility: Nothing changes.

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched). (no issue fits)
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named). (the check that reads the changed code, on this PR, Windows: tests/test_blit_format_checks.py)
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here. (tests/test_blit_format_checks.py)
  • A check under tests/ that reads changed source updated in this PR, not weakened.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. (built on Windows with llvm-mingw and on Linux; no OS-specific code or paths)
**Type:** fix · **Risk:** low · **Tested on:** Windows, AMD Radeon RX 9070 XT · **Needs:** none ## Why A scaled or converting texture copy records vkCmdBlitImage for any pair of formats, so a pair the device cannot blit (no BLIT_SRC or BLIT_DST, integer with float, multisampled, or linear filtering without filter support) gives an undefined result, from garbage to a lost device (VUID-vkCmdBlitImage-srcImage-01999 and its neighbours below). The same fix was first sent as part of #287 together with other changes; this PR sends it alone, one fix per PR. ## How to reproduce **Seen on:** not seen in the game: found by reading `copy_t2t()` in `gpu/vk/vk_xfer.cpp`; it happens whenever the game asks for a scaled or format-converting texture copy between formats the device cannot blit **Settings and switches:** defaults **Steps:** game-free: `python -I -B tests/test_blit_format_checks.py` with this PR's test on a main checkout **Expected:** the blit is recorded only when every rule below holds; otherwise it is skipped with a warning **Actual:** on main `copy_t2t()` records the blit with no check; the test's 3 cases fail (2 failures, 1 error) ## What changed Before `vkCmdBlitImage`, `copy_t2t()` now checks what Vulkan requires of a blit and skips it with a warning (`warn(13, ...)`) when the device cannot make it; linear filtering is used only where the source format supports it. - vk: `gpu/vk/vk_xfer.cpp`: `format_class()` (new: float/normalized, unsigned or signed integer); `copy_t2t()` checks BLIT_SRC/BLIT_DST in `tiling_features()`, the same format class on both sides, the same format when not colour, single-sampled images, and `SAMPLED_IMAGE_FILTER_LINEAR` (and a float class) before choosing `VK_FILTER_LINEAR`. - tests: `tests/test_blit_format_checks.py` (new): reads `copy_t2t()` and checks each condition is tested before the blit is recorded. The bug, step by step: 1. `copy_t2t()` takes the blit path for any scaled or converting copy (`else` after the same-size `vkCmdCopyImage`). 2. It fills `VkImageBlit` and calls `vkCmdBlitImage` with `VK_FILTER_LINEAR` when the game asked for filtering, without looking at either format's features, class or sample count. 3. The formats come from the game's textures, so any pair can arrive; each rule below is then broken by the pairs it names. The rules: - VUID-vkCmdBlitImage-srcImage-01999: "The format features of srcImage must contain VK_FORMAT_FEATURE_BLIT_SRC_BIT" - VUID-vkCmdBlitImage-dstImage-02000: "The format features of dstImage must contain VK_FORMAT_FEATURE_BLIT_DST_BIT" - VUID-vkCmdBlitImage-srcImage-00229: "If either of srcImage or dstImage was created with a signed integer VkFormat, the other must also have been created with a signed integer VkFormat" - VUID-vkCmdBlitImage-srcImage-00230: "If either of srcImage or dstImage was created with an unsigned integer VkFormat, the other must also have been created with an unsigned integer VkFormat" - VUID-vkCmdBlitImage-srcImage-00231: "If either of srcImage or dstImage was created with a depth/stencil format, the other must have exactly the same format" - VUID-vkCmdBlitImage-srcImage-00233: "srcImage must have been created with a samples value of VK_SAMPLE_COUNT_1_BIT" - VUID-vkCmdBlitImage-dstImage-00234: "dstImage must have been created with a samples value of VK_SAMPLE_COUNT_1_BIT" - VUID-vkCmdBlitImage-filter-02001: "If filter is VK_FILTER_LINEAR, then the format features of srcImage must contain VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT" Format features come from `vkGetPhysicalDeviceFormatProperties` (`tiling_features()`, optimal tiling), so the check follows the device. A depth/colour pair is skipped because non-colour copies need identical formats (00231). ## Measurements **Verdict:** it fixes the bug: `tests/test_blit_format_checks.py` fails on main and passes with this PR, in both interleaved runs. No speed change is expected: the change is on a failure or check path. Hardware: AMD Radeon RX 9070 XT, Windows; the test itself is game-free (it runs on the CPU, compiled with clang++ from llvm-mingw where it builds a fixture); Python 3.12 Scene: none, game-free Method: this PR's test files on each side's checkout, 2 runs each, interleaved (main, PR, main, PR); the result is pass/fail, so the median is the result both runs gave How to reproduce: `python -I -B tests/test_blit_format_checks.py` on each side | | Before: main @ ac778751 | After: this PR @ b5bd6ecc | |---|---|---| | `tests/test_blit_format_checks.py`, run 1 | fails (3 cases: 2 failures, 1 error) | passes (3 of 3 pass) | | `tests/test_blit_format_checks.py`, run 2 | fails (3 cases: 2 failures, 1 error) | passes (3 of 3 pass) | | Median | fails | passes | ``` run 1, main @ ac778751: exit 1: FAILED (failures=2, errors=1) run 1, this PR @ b5bd6ecc: exit 0: OK run 2, main @ ac778751: exit 1: FAILED (failures=2, errors=1) run 2, this PR @ b5bd6ecc: exit 0: OK ``` ### Flags and settings None. ## Testing **Tested:** on this branch, Windows, Python 3.12, clang++ from llvm-mingw: `tests/test_blit_format_checks.py` passes (fails on main). **Built and played:** this commit builds on Windows (llvm-mingw back end) and on Linux (`longshot_vk.so`, without and with AMD FSR 4). Played on Windows, AMD Radeon RX 9070 XT: the game runs normally. (The build that was played had this change before a whitespace-only fix: indentation, no code change.) **Not tested:** the failure path itself in the game (it needs a driver failure or a format pair the RX 9070 XT does not produce on demand). Linux was built, not run. **Must be tested:** None: the change is the same on every GPU and OS, and the game-free test covers it. ## Risks **What could break:** a blit the device cannot make is now skipped, so the destination keeps its old contents instead of an undefined result; a game that relied on such a blit (only on drivers that happened to accept it) would see the old texture there. **Turning it off:** revert the commit. **Compatibility:** Nothing changes. ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). (no issue fits) - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). (the check that reads the changed code, on this PR, Windows: `tests/test_blit_format_checks.py`) - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here. (`tests/test_blit_format_checks.py`) - [x] A check under `tests/` that reads changed source updated in this PR, not weakened. - [x] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. (built on Windows with llvm-mingw and on Linux; no OS-specific code or paths)
fix(vk): validate texture blits before vkCmdBlitImage
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 6s
b5bd6ecc16
copy_t2t recorded a scaled or converting copy as vkCmdBlitImage without checking that Vulkan allows it: BLIT_SRC/BLIT_DST format features, one format class on both sides, matching depth/stencil formats, single-sampled images, and a linear filter only where the source format supports it. Such a blit is now skipped with a warning; the format features are asked once per format. tests/test_blit_format_checks.py checks it.

Assisted-by: GitHub Copilot
Author
Contributor

Tested in game: Played on Windows, AMD Radeon RX 9070 XT: the game runs normally.

Tested in game: Played on Windows, AMD Radeon RX 9070 XT: the game runs normally.
Collaborator

Possibly related, for context:

  • PR #1160 WIP: fix(vk): six Vulkan calls checked against what the device allows (blits, ASTC clears, mesh ranges, texel views, sparse binds) — Open PR contains the same blit validation change
Possibly related, for context: - PR #1160 WIP: fix(vk): six Vulkan calls checked against what the device allows (blits, ASTC clears, mesh ranges, texel views, sparse binds) — Open PR contains the same blit validation change
Author
Contributor

I am juliotec, the original author of this fix. It was first sent as part of #287 together with other changes; this PR sends it alone, one fix per PR, rebased on current main.

I am juliotec, the original author of this fix. It was first sent as part of #287 together with other changes; this PR sends it alone, one fix per PR, rebased on current main.
jcabarca changed title from WIP: fix(vk): validate texture blits before vkCmdBlitImage to fix(vk): validate texture blits before vkCmdBlitImage 2026-11-16 21:48:40 +00:00
TrashPanda approved these changes 2026-11-16 23:25:30 +00:00
TrashPanda left a comment

A scaled or converting texture copy is now recorded with vkCmdBlitImage only when the device allows that blit. I read head b5bd6ecc. The check matches the rules in the description, and the game was played on Windows with an RX 9070 XT. Approved.

copy_t2t already refuses a depth copy between two formats before this branch. In the scaled branch it now also requires BLIT_SRC and BLIT_DST, one format class on both sides, single-sampled images, and linear filtering only for a non-integer source that advertises it. A refused blit warns and returns, so the destination keeps what it had. format_class puts the unsigned and signed formats it lists in their own classes and leaves normalized, float and depth formats in the other. The feature cache keeps bit 63 as "already asked" and returns the low bits, which is the 32-bit feature mask.

Two limits, neither a reason to hold it:

  • The test searches the source for those conditions. Removing them fails it. Reversing one of them still passes.
  • The block-compressed decompress path above this branch still calls vkCmdBlitImage after checking only BLIT_SRC.

#1160 is a work-in-progress bundle that also edits this function. This pull request is the blit check to take. The checklist's full-suite box names only tests/test_blit_format_checks.py.

A scaled or converting texture copy is now recorded with `vkCmdBlitImage` only when the device allows that blit. I read head `b5bd6ecc`. The check matches the rules in the description, and the game was played on Windows with an RX 9070 XT. Approved. `copy_t2t` already refuses a depth copy between two formats before this branch. In the scaled branch it now also requires `BLIT_SRC` and `BLIT_DST`, one format class on both sides, single-sampled images, and linear filtering only for a non-integer source that advertises it. A refused blit warns and returns, so the destination keeps what it had. `format_class` puts the unsigned and signed formats it lists in their own classes and leaves normalized, float and depth formats in the other. The feature cache keeps bit 63 as "already asked" and returns the low bits, which is the 32-bit feature mask. Two limits, neither a reason to hold it: - The test searches the source for those conditions. Removing them fails it. Reversing one of them still passes. - The block-compressed decompress path above this branch still calls `vkCmdBlitImage` after checking only `BLIT_SRC`. https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1160 is a work-in-progress bundle that also edits this function. This pull request is the blit check to take. The checklist's full-suite box names only `tests/test_blit_format_checks.py`.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 6s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/vk-blit-format-checks:jcabarca-fix/vk-blit-format-checks'
git switch 'jcabarca-fix/vk-blit-format-checks'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
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
MagicBeanDev/Longshot!1241
No description provided.