fix(vk): validate texture blits before vkCmdBlitImage #1241
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1241
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:fix/vk-blit-format-checks"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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()ingpu/vk/vk_xfer.cpp; it happens whenever the game asks for a scaled or format-converting texture copy between formats the device cannot blitSettings and switches: defaults
Steps: game-free:
python -I -B tests/test_blit_format_checks.pywith this PR's test on a main checkoutExpected: 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.gpu/vk/vk_xfer.cpp:format_class()(new: float/normalized, unsigned or signed integer);copy_t2t()checks BLIT_SRC/BLIT_DST intiling_features(), the same format class on both sides, the same format when not colour, single-sampled images, andSAMPLED_IMAGE_FILTER_LINEAR(and a float class) before choosingVK_FILTER_LINEAR.tests/test_blit_format_checks.py(new): readscopy_t2t()and checks each condition is tested before the blit is recorded.The bug, step by step:
copy_t2t()takes the blit path for any scaled or converting copy (elseafter the same-sizevkCmdCopyImage).VkImageBlitand callsvkCmdBlitImagewithVK_FILTER_LINEARwhen the game asked for filtering, without looking at either format's features, class or sample count.The rules:
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.pyfails 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.pyon each sideac778751b5bd6ecctests/test_blit_format_checks.py, run 1tests/test_blit_format_checks.py, run 2Flags and settings
None.
Testing
Tested: on this branch, Windows, Python 3.12, clang++ from llvm-mingw:
tests/test_blit_format_checks.pypasses (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
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). (the check that reads the changed code, on this PR, Windows:tests/test_blit_format_checks.py)third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here. (tests/test_blit_format_checks.py)tests/that reads changed source updated in this PR, not weakened.Tested in game: Played on Windows, AMD Radeon RX 9070 XT: the game runs normally.
Possibly related, for context:
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.
WIP: fix(vk): validate texture blits before vkCmdBlitImageto fix(vk): validate texture blits before vkCmdBlitImageA scaled or converting texture copy is now recorded with
vkCmdBlitImageonly when the device allows that blit. I read headb5bd6ecc. The check matches the rules in the description, and the game was played on Windows with an RX 9070 XT. Approved.copy_t2talready refuses a depth copy between two formats before this branch. In the scaled branch it now also requiresBLIT_SRCandBLIT_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_classputs 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:
vkCmdBlitImageafter checking onlyBLIT_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.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.