fix(vk): make a sparse BC image with the usage its sparse residency was checked for #671
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!671
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:fix/vk-sparse-bc-usage"
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 2D array texture too big to keep whole is made as a sparse image, but for BC formats the driver is asked whether it supports sparse residency with transfer and sampled usage only, and the image is then made with storage and color attachment usage too. That is a combination the driver never said it supports sparse; a driver that refuses it loses the texture (
vk: sparse image ... not made).How to reproduce
Seen on: not seen in the game: found by reading
create()ingpu/vk/vk_texture.cpp; it can happen whenever a BC 2D array bigger than the texture budget's limit is made sparse on a GPU with sparse residencySettings and switches: defaults
Steps: game-free:
python -I -B tests/test_sparse_bc_usage.pyon main (copy the test from this PR); it reads the sparse residency query and the sparse image's creation invk_texture.cppExpected: the sparse image is made with the usage its sparse residency was asked with
Actual: on main a sparse BC image is made with
STORAGEandCOLOR_ATTACHMENTusage, which the query left out; the test failsWhat changed
A sparse BC image now drops
STORAGEandCOLOR_ATTACHMENTfrom its usage beforevkCreateImage, so it is made with the usage its sparse residency was asked with. A compressed image the driver refuses already loses these two usages a few lines below, and its raw block views then followx.usageand are not made; the sparse BC image now does the same from the start.create()ingpu/vk/vk_texture.cpp: insideif (x.sparse), beforevkCreateImage,if (is_bc(x.format)) ci.usage &= ~(VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), x.usage = ci.usage;. Non-BC sparse images and non-sparse images are unchanged.tests/test_sparse_bc_usage.py(new) fails if the sparse BC image is made without dropping these two usages.Second commit, "make a sparse image only within its format's limits for the sparse flags and usage":
create()ingpu/vk/vk_texture.cpp: beforevkCreateImagefor a sparse image,vkGetPhysicalDeviceImageFormatPropertiesis called with the image's format, type, tiling, usage and flags (the sparse ones included); the image is made only if the call succeeds and its extent, levels, layers and samples fit the answer; otherwise it takes the existing path for a sparse image that could not be made.tests/test_sparse_bc_usage.pychecks the query and the gate.The sparse path asked only
vkGetPhysicalDeviceSparseImageFormatProperties, which says nothing of the image limits for those flags and that usage. VUID-VkImageCreateInfo-imageCreateMaxMipLevels-02251: "Each of the following values (as described in Image Creation Limits) must not be undefined : imageCreateMaxMipLevels, imageCreateMaxArrayLayers, imageCreateMaxExtent, and imageCreateSampleCounts", and 02252-02256 for the extent, levels, layers and samples.The bug, step by step:
max_bytesis a candidate for a sparse image (dev.sparse_images).vkGetPhysicalDeviceSparseImageFormatPropertiesand the usageu:TRANSFER_SRC | TRANSFER_DST | SAMPLED, plusCOLOR_ATTACHMENT | STORAGEonlyif (!is_bc(x.format)). For a BC format the driver is never asked about storage or attachment usage.x.sparseis set when the driver returns any properties for that usage.ci.usage, built fromx.usage, which for BC formats can holdSTORAGEandCOLOR_ATTACHMENT(the raw block views written as uncompressed data).vkCreateImagegetsSPARSE_BINDING | SPARSE_RESIDENCYwith a usage the sparse query did not cover. A driver that does not support it fails the call, the code logsvk: sparse image %ux%u x%u levels %u format %d not madeand returnsnullptr: the texture is lost.The query, unchanged:
Before:
After:
Start reading at the comment "a 2D array too big to keep whole, in a format with sparse residency" in
create(), then theif (x.sparse)block below it.Measurements
Verdict: it fixes the bug:
test_sparse_bc_usagefails on main and passes here. Both interleaved runs gave the same result. No speed change is expected.Hardware: AMD Radeon RX 9070 XT, Windows; the tests are game-free (they read source or build a cut-out fixture, they do not use the GPU); Python 3.12
Scene: none, game-free
Method: the tests from this PR run against 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>.pywith--source <checkout>(and--cxx/--headerswhere the test asks for them) on each sidec167ec67d64920d0tests/test_sparse_bc_usage.py, run 1tests/test_sparse_bc_usage.py, run 2tests/test_sparse_bc_usage.py, medianFlags and settings
None.
Testing
Tested: on this branch, Windows, Python 3.12:
test_sparse_bc_usagepasses (fails on main), and the other checks that read texture creation pass:test_bc_raw_targets,test_astc_bc_policy,test_compressed_over_rt,test_image_view_retry,test_3d_view_create_flags,test_drawn_page_map,test_low_vram_texture_mips,test_native_astc_texture_count,test_target_image_pool,test_vram_compact,test_vulkan_clear_mip.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.Not tested: a sparse BC image being made in the game: not checked in the game (it needs a BC 2D array bigger than the texture budget's limit). 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 sparse BC texture no longer gets its raw block views (storage and attachment), so a draw or compute that writes BC blocks as raw data into such a texture would not have that view. Non-sparse BC textures refused by the driver already lose them the same way, and before this PR a driver that refused the sparse image lost the whole texture.
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 checks that read the changed code, on this PR, Windows: all pass)third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here.tests/that reads changed source updated in this PR, not weakened. (tests/test_sparse_bc_usage.pynew)longshot_vk.sowithout and with AMD FSR 4)WIP: fix(vk): make a sparse BC image with the usage its sparse residency was checked forto fix(vk): make a sparse BC image with the usage its sparse residency was checked forCode review only; I did not run the checks. A question and a suggestion, no hard block.
The mismatch is real: the sparse residency query leaves STORAGE and COLOR_ATTACHMENT out for BC, and
vkCreateImagethen usesx.usagewith them in. The raw views are gated ontex->usage(vk_texture.cpp:570-575), so dropping the bits inx.usageas well is consistent.Concern: the drop is unconditional for every sparse BC image on every driver. A driver that accepts the larger usage today, and so has the raw storage and attachment views on its sparse BC textures, loses them with this change. The PR says the bug was not seen in the game, and the "Risks" section concedes that the raw views are gone. The non-sparse path in
create()does it the other way: it creates the image with the usage and only strips the bits after the driver refuses (vk_texture.cpp:2229-2231).Suggestion: do the same here. Try
vkCreateImagewith the full usage, and on failure retry with STORAGE and COLOR_ATTACHMENT removed (or askvkGetPhysicalDeviceImageFormatPropertieswith the sparse flag and the full usage first). That fixes the loss where it happens and costs nothing where it works. If you would rather keep the unconditional drop, please say in the description why losing the raw block views on drivers that accept them does not matter (sparse BC 2D arrays that the game's BC encoders write into). A run on a driver that takes the full usage would settle it.Smaller point: the test docstring says "AMD's driver refused it, and the texture was lost", but the PR says it was never seen in the game. Please word them the same.
@ -2189,1 +2189,4 @@ci.flags |= VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT;// (a compressed one with the usage its sparse residency was asked with, above: no raw storage or attachment views,// which the driver did not say it can make sparse; as a compressed image refused without them, below)if (is_bc(x.format))This drops the raw views on every driver, including ones that accept the full usage for a sparse image. The non-sparse path below tries first and strips the bits only when the driver refuses; a retry without them here would do the same.
Code-only review of
cafd484b68; informational findings, with final approval left to another reviewer.No additional production defect found beyond the still-unanswered raw-view compatibility concern already raised. I checked the sparse eligibility query, usage construction, image creation and
make_view()guard; the mismatch exists on current main, and the new mask also consistently prevents storage/attachment views throughtex->usage.The remaining validation gap is specific: the reported RX 9070 XT play session did not exercise a sparse BC image. It therefore does not establish whether dropping the raw views preserves the affected texture's contents. A run that actually reaches sparse BC creation and checks its content, including any guest raw-block writes, would resolve that gap alongside the existing compatibility question.
One useful test improvement:
tests/test_sparse_bc_usage.py:29-30only searches for strings. Changingif (is_bc(x.format))toif (!is_bc(x.format))would still satisfy every assertion, although BC images would retain the unsupported usages and non-BC images would lose required usages. Add behavioral coverage of BC/non-BC usage selection and synchronization ofci.usagewithx.usage; a stubbed creation call can capture the actual flags without a game or GPU. The current source check does not prove the driver failure is fixed.PR 925 handles sparse upload residency in a different path; it complements this change rather than replacing it. No source changes, tests, builds or game runs performed in this review.
New commit, "make a sparse image only within its format's limits for the sparse flags and usage" (
d64920d0); the branch is rebased onto main @c167ec67.The sparse path asked only
vkGetPhysicalDeviceSparseImageFormatProperties, which says nothing of the image limits for those flags and that usage. VUID-VkImageCreateInfo-imageCreateMaxMipLevels-02251: "Each of the following values (as described in Image Creation Limits) must not be undefined : imageCreateMaxMipLevels, imageCreateMaxArrayLayers, imageCreateMaxExtent, and imageCreateSampleCounts", and 02252-02256 for the extent, levels, layers and samples.create()ingpu/vk/vk_texture.cpp: beforevkCreateImagefor a sparse image,vkGetPhysicalDeviceImageFormatPropertiesis called with the image's format, type, tiling, usage and flags (the sparse ones included); the image is made only if the call succeeds and its extent, levels, layers and samples fit the answer; otherwise it takes the existing path for a sparse image that could not be made.tests/test_sparse_bc_usage.pychecks the query and the gate.test_sparse_bc_usagefails on main and passes here. Run twice each side, interleaved; What changed and Measurements in the description are updated.cafd484b68d64920d050This commit (both commits of the PR) builds on Windows (llvm-mingw back end) and on Linux (
longshot_vk.so, without and with AMD FSR 4), and was played on Windows, AMD Radeon RX 9070 XT: the game runs normally. The description's Built and played line and the Host code builds box are updated.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.