fix(vk): stage ASTC layers at an aligned pitch, zero their padding and rebind after the decoder #920
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
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!920
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:fix/vk-astc-staging-align"
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
The GPU ASTC decoder staged a texture's layers back to back, so each layer after the first started at
k * tail, and that storage buffer offset breaks VUID-VkWriteDescriptorSet-descriptorType-00328 whenever the layer size is not a multiple of minStorageBufferOffsetAlignment. The driver may then read the layer's blocks from the wrong place: array and cube ASTC textures can show garbage in their later layers.How to reproduce
Seen on: not seen in the game: found by reading
upload_astc_gpu()ingpu/vk/vk_texture.cpp; it happens for any ASTC array or cube texture decoded on the GPU whose layer tail is not a multiple of the device's minStorageBufferOffsetAlignmentSettings and switches: defaults
Steps: game-free:
python -I -B tests/test_texture_staging_publication.py --cxx clang++ --headers <Vulkan-Headers include>with--sourcepointing at a main checkout (the test from this PR); it compilesupload_astc_gpu()'s staging out of the sourceExpected: each layer is staged at an offset that is a multiple of minStorageBufferOffsetAlignment, padded with zeros, and the draw state knows the decoder replaced its compute bindings
Actual: on main the layers sit at
k * tailand the decoder's bind does not mark the bound descriptors disturbed; the test failsWhat changed
upload_astc_gpu()now stages each layer at a pitch: the layer tail rounded up to minStorageBufferOffsetAlignment (at least 4). The bytes between a layer's end and the next pitch are zeroed, and the decoder's bind now callsdescriptors_disturbed().upload_astc_gpu()ingpu/vk/vk_texture.cpp:pitch = (tail + align - 1) / align * alignreplacestailas the stride everywhere a layer is placed or found: the staging size, the copy into staging,staged,pc.layer_stride(both decoder paths) and the descriptor offsetsfrom.pitch - nbytes after each layer, also for a layer past the end of the guest data (before, such a layer was skipped and kept the staging's old bytes).UINT32_MAX(it does not fitlayer_stride) fails the upload with "its layout", as other unusable layouts do.alignis computed once, before the staging, instead of after the bind.descriptors_disturbed()right aftervkCmdBindPipeline(..., astc_pipe), so the next draw or dispatch binds its own pipeline and descriptors again.tests/test_texture_staging_publication.pyandtests/texture_staging_publication_test.cpp(extended): a newastc-pitchcase and thedescriptors_disturbed()check (details under Measurements).Second commit, "clamp the decoders' storage buffer ranges to maxStorageBufferRange":
upload_astc_gpu()(two places) andupload_unswizzle_gpu()ingpu/vk/vk_texture.cpp: theVkDescriptorBufferInforange isstd::min<uint64_t>(..., dev.props.limits.maxStorageBufferRange).tests/test_storage_buffer_range.py(new) checks the three ranges.The range was the staged size with no upper bound; a large ASTC array can pass 2^27 bytes, the limit's required minimum. VUID-VkWriteDescriptorSet-descriptorType-00333: "If descriptorType is VK_DESCRIPTOR_TYPE_STORAGE_BUFFER or VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, and the shader64BitIndexing feature is not enabled, the range member of each element of pBufferInfo, or the effective range if range is VK_WHOLE_SIZE, must be less than or equal to VkPhysicalDeviceLimits::maxStorageBufferRange" (shader64BitIndexing is not enabled).
The bug, step by step:
upload_astc_gpu()copies the encoded blocks of every dirty layer into one staging buffer, layerkats.data + k * tail.s.offset + k * tail + offfor the decoder.std::max(256, minStorageBufferOffsetAlignment)), andoffmust already be a multiple ofalign(if (... || (off % align)) continue;), butk * tailis not: with a tail of 1552 bytes and an alignment of 256, layer 1 starts at 1552.astc_pipeand pushes its descriptors on the upload command buffer, which is also where the draws record (upload_cmd()returns the recording's command buffer). The draw state's push-descriptor cache (pushed()) still thought its own were bound.The rule, from Khronos
validusage.json:Before:
After:
pc.layer_stride(both the BC3 and the direct decoder paths) and the BC3 path'sfrom = k0 * pitch + offchange the same way. Start reading atbool upload_astc_gpu(invk_texture.cpp.Measurements
Verdict: it fixes the bug:
test_storage_buffer_rangefails on main and passes here;test_texture_staging_publication(the first commit's test) fails 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 sidec167ec671b210b6atests/test_storage_buffer_range.py, run 1tests/test_texture_staging_publication.py, run 1tests/test_storage_buffer_range.py, run 2tests/test_texture_staging_publication.py, run 2tests/test_storage_buffer_range.py, mediantests/test_texture_staging_publication.py, medianFlags and settings
None.
Testing
Tested: on this branch, Windows, Python 3.12, clang++ from llvm-mingw:
test_texture_staging_publicationpasses (fails on main), and the other checks that readupload_astc_gpu()pass:test_bc_upload_retry,test_mem_trim.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: an ASTC array texture with a layer size that is not a multiple of the alignment, decoded in the game and looked at layer by layer: not checked in the game. 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: the staging grows by at most
align - 1bytes per layer, so a texture right at the staging's limit could now fall back to its CPU path ("no staging memory for its ASTC blocks"), which already handles that case. A layer with a pitch over 4 GiB now fails with "its layout" instead of being truncated intolayer_stride. The next draw after a decode re-pushes its descriptors once.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:test_texture_staging_publication,test_bc_upload_retry,test_mem_trim, all pass)third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here. (tests/test_texture_staging_publication.pyand its fixture extended)tests/that reads changed source updated in this PR, not weakened. (tests/test_texture_staging_publication.py: one check and one fixture case added, none removed)longshot_vk.sowithout and with AMD FSR 4)WIP: fix(vk): stage ASTC layers at an aligned pitch, zero their padding and rebind after the decoderto fix(vk): stage ASTC layers at an aligned pitch, zero their padding and rebind after the decoderCode-only review of
22e276c725; informational findings, with final approval left to another reviewer.No introduced correctness defect found in the changes. I followed the aligned pitch through staging allocation, copies, flush ranges, both decoder push constants and descriptor offsets, including partial dirty-layer uploads. The descriptor-cache invalidation uses the existing mechanism, and the staging arena's submission lifetime remains unchanged.
Validation limit: the new fixture exercises actual staging bytes and padding, but stops before the decoder branches. It cannot catch a regression that changes either decoder's
layer_strideor descriptor offset back totail; descriptor invalidation is checked as source text rather than by observing a subsequent dispatch. Extending the fixture to capture descriptor offsets/push constants for direct and BC decoding, plus a same-layout dispatch after decoding, would strengthen regression coverage. The reported Windows RX 9070 XT playthrough and Windows/Linux builds are useful smoke evidence; the misaligned multilayer case has not been checked in-game, as the description acknowledges.Merge coordination: the ASTC batching work tracked in issue 813 names this fix as a dependency. Preserve this pitch and invalidation work when integrating that prototype; the sparse-unswizzle fix in PR 925 changes a different upload path.
Read the full diff, staging allocator/publication fixture, decoder shader addressing, descriptor cache invalidation and surrounding upload code. No tests, builds or game runs performed for this review.
New commit, "clamp the decoders' storage buffer ranges to maxStorageBufferRange" (
1b210b6a); the branch is rebased onto main @c167ec67.The range was the staged size with no upper bound; a large ASTC array can pass 2^27 bytes, the limit's required minimum. VUID-VkWriteDescriptorSet-descriptorType-00333: "If descriptorType is VK_DESCRIPTOR_TYPE_STORAGE_BUFFER or VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, and the shader64BitIndexing feature is not enabled, the range member of each element of pBufferInfo, or the effective range if range is VK_WHOLE_SIZE, must be less than or equal to VkPhysicalDeviceLimits::maxStorageBufferRange" (shader64BitIndexing is not enabled).
upload_astc_gpu()(two places) andupload_unswizzle_gpu()ingpu/vk/vk_texture.cpp: theVkDescriptorBufferInforange isstd::min<uint64_t>(..., dev.props.limits.maxStorageBufferRange).tests/test_storage_buffer_range.py(new) checks the three ranges.test_storage_buffer_rangefails on main and passes here;test_texture_staging_publication(the first commit's test) fails on main and passes here. Run twice each side, interleaved; What changed and Measurements in the description are updated.22e276c7251b210b6a14This 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.