fix(vk): give image descriptors a dummy view when nullDescriptor is off #403
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!403
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:fix/vk-dummy-views"
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
On a GPU without robustness2's
nullDescriptor, a sampled or storage image that does not resolve to a view was written to its descriptor asVK_NULL_HANDLE. That breaks VUID-VkWriteDescriptorSet-descriptorType-02997: the validation layers flag it, and a driver may read garbage or crash. Now it gets a 1x1 black dummy view of the right view type.How to reproduce
Seen on: not seen in the game: found by reading
make_dummies()andbuild_descriptors()ingpu/vk/vk_draw.cpp; it happens on any device that does not enablenullDescriptor(the RX 9070 XT enables it, so it never takes this path)Settings and switches: defaults
Steps: game-free:
python -I tests/test_dummy_views.py --cxx clang++ --headers <Vulkan-Headers v1.4.345>/include --source <main checkout>(copy the test from this PR); it readsvk_draw.cppand buildsmake_dummies()in a stubbed fixtureExpected: with
nullDescriptoroff, no image descriptor is written with a nullimageViewActual: on main
make_dummies()makes nothing and a storage image that does not resolve is written asVK_NULL_HANDLE; the test fails withAssertionError: storage image written as VK_NULL_HANDLEWhat changed
make_dummies()was an empty stub, so on a device withoutnullDescriptorevery dummy view stayed null and an image that does not resolve was written as a null view. It now makes three 1x1 black images once, clears them in the first recording and makes one view per view type;build_descriptors()uses those views for storage images too. Devices withnullDescriptorare unchanged.make_dummies()ingpu/vk/vk_draw.cppmakes, once and only withoutnullDescriptor, three 1x1 blackVK_FORMAT_R8G8B8A8_UNORMimages (1D; 2D cube compatible with 6 layers; 3D), clears them in the first recording (rec_cmd) with a barrier toGENERALbefore and after the clear, and makes one view perVkImageViewTypeindummies.view[](the cube array view only withimageCubeArray). If an image cannot be made, the ones made are destroyed, it logsvk: the dummy images (no nullDescriptor): not made(up to 4 times) and the views stay null, as before.build_descriptors()ingpu/vk/vk_draw.cpp: a storage image that does not resolve getsdummies.view[(int)vt]instead ofVK_NULL_HANDLE.tests/test_dummy_views.py(new) cutsmake_dummies()out ofvk_draw.cppinto a stubbed fixture and checks the storage image line.The bug, step by step:
make_dummies()on main returned at once (if (dummies.view[0] || dev.null_descriptor) return;) and made nothing, sodummies.view[]stayedVK_NULL_HANDLEon every device.build_descriptors(), a storage image whose handle does not resolve to a view keptview = VK_NULL_HANDLE(if (!view && !dev.null_descriptor) view = VK_NULL_HANDLE;), and a sampled one fell back to the nulldummies.view[].VkDescriptorImageInfo::imageViewand tovkUpdateDescriptorSetswithnullDescriptoroff.The rule, from Khronos
validusage.json(Vulkan-Headers v1.4.345):Before (
build_descriptors()):After:
Before (
make_dummies()):After (shortened; the full function is in the diff):
Start reading at
make_dummies()invk_draw.cpp: the images are made only when a recording is open (rec_cmd), so the clear goes in that command buffer.Measurements
Verdict: it fixes the bug:
tests/test_dummy_views.pyfails on main (a storage image is written asVK_NULL_HANDLE) and passes with this PR, in both interleaved runs. No speed change is expected: devices withnullDescriptordo nothing new; the others make three 1x1 images and up to 7 views once.Hardware: AMD Radeon RX 9070 XT, Windows; the test itself is game-free (it does not use the GPU); clang++ from llvm-mingw 20261104 (ucrt x86_64)
Scene: none, game-free
Method:
tests/test_dummy_views.pyfrom this PR against each side'sgpu/vk/vk_draw.cpp(--sourcepointed at a checkout of main), 2 runs each, interleaved (main, PR, main, PR); the result is pass/fail, so the median is the result both runs gaveHow to reproduce:
python -I tests/test_dummy_views.py --cxx clang++ --headers <Vulkan-Headers v1.4.345>/include(with--source <checkout>to point at main)93344a7c1b5553e0tests/test_dummy_views.py, run 1tests/test_dummy_views.py, run 2What the test checks:
made: withimageCubeArray, every one of the 7 view types gets a non-null dummy view; 3 images are made and cleared; a second call makes nothing more.no-cube-array: withoutimageCubeArray, no cube array view is made, the 2D one is.null-descriptor: withnullDescriptor, nothing is made (null views stay valid there).image-fails: if the second image cannot be made, the first is destroyed and the views stay null.vk_draw.cppno longer writes a storage image asVK_NULL_HANDLE.Flags and settings
None.
Testing
Tested: on this branch, Windows, clang++ from llvm-mingw 20261104:
test_dummy_viewspasses (fails on main), and the other checks that read the changed code pass:test_desc_clock,test_desc_counts,test_drawn_page_map,test_prof_symbols,test_draw_stream_visibility,test_fsr_uniform_ownership,test_recording_admission.test_drawn_page_mapfirst failed twice on this branch only in its temp-folder cleanup on Windows (WinError 32: its builtf.exewas still locked, after every check had passed), then passed in 2 of 2 runs interleaved with main (main: 2 of 2 too).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 device without
nullDescriptor: the RX 9070 XT has it, so in the game this path (and the dummy views) is never reached, and the validation layers were not run on such a device. Linux was built, not run.Must be tested: None: the game-free test covers the new path on every GPU and OS.
Risks
What could break: only devices without
nullDescriptortake the new path: three 1x1 images and up to 7 views made once, cleared in the first recording. A shader that reads an image that does not resolve now reads black instead of whatever a null view gave. If an image cannot be made, the views stay null, as on main.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_dummy_views,test_desc_clock,test_desc_counts,test_drawn_page_map,test_prof_symbols,test_draw_stream_visibility,test_fsr_uniform_ownership,test_recording_admission, 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_dummy_views.py, new)tests/that reads changed source updated in this PR, not weakened. (tests/test_dummy_views.py, new, readsgpu/vk/vk_draw.cpp)longshot_vk.sowithout and with AMD FSR 4; no OS-specific code or paths)Hardware named (AMD Radeon RX 9070 XT); Measured: 2 interleaved runs each side (main fails both, this PR passes both); run_all.py box: the checks that read the changed code, all passing; If applicable: tests/ check and builds boxes ticked; Needs other testers: None.
WIP: fix(vk): give image descriptors a dummy view when nullDescriptor is offto fix(vk): give image descriptors a dummy view when nullDescriptor is offDescription moved to the new PR template. Rebased onto main @
93344a7cwith no code change (same diff). test_dummy_views measured again, 2 interleaved runs each side: main @93344a7cfails both, this PR @1b5553e0passes both. The checks that read the changed code pass on this PR.6c55b3968a1b5553e069Testing: noted a temp-folder cleanup failure of test_drawn_page_map on Windows (WinError 32) and its two clean reruns.
First review, code only. I ran no tests here.
What holds up. On main, without
nullDescriptor, both the sampled and the storage path wroteVK_NULL_HANDLE, and the PR fixes that write. The images and views are valid as written: 1x1 1D, 2D cube-compatible with 6 layers, and 3D. Each view type's subresource range fits its image, and CUBE_ARRAY is made only withimageCubeArray.R8G8B8A8_UNORMstorage support is mandatory. The images go toGENERALbefore the descriptors use them inGENERAL. They are never destroyed, which matcheszero_buffer. The creation runs underParLockindraw_begin_recording, so other recording threads see the views. Devices that havenullDescriptorare untouched:make_dummiesreturns at once anddummies.view[]stays null, so the sampled fallback at vk_draw.cpp:3987 still binds null there.Who this reaches. Every Tier 1 and Tier 2 driver I know of reports robustness2
nullDescriptor: NVIDIA, AMD Windows, RADV/Steam Deck, Intel Arc and ANV. So this path runs only on hardware outside the tiers. That makes it low risk, but it also means no one on the team runs it by default.What's left:
The dummy views don't match every type a shader can declare (inline). Every fallback is an
R8G8B8A8_UNORMsingle-sample colour view, so the following still break:d.is_integer);d.is_depth), including the Lichii bd0171f7 case at :3943, which is meant to bind nothing;d.is_multisample);image_format(d.format): R8/R16/R32 UINT/SINT, R32G32_UINT, R32G32B32A32_UINT), and atomics on them.These trade the write-time VUID-02997 for draw-time VUIDs and undefined reads. It is still better than a null view, but it isn't "no invalid descriptor" yet.
"Must be tested: None" doesn't hold. The test stubs every Vulkan call (
vkCreateImageView, the barriers, the clear), so no driver has ever created or cleared these images. Before merge, someone needs to run it on a real driver:dev.null_descriptor = dev.null_descriptor && robust2.nullDescriptor;(vk_main.cpp:563) to= falselocally and don't commit it;VK_LAYER_KHRONOS_validation, from the title screen into a gameplay area;Please put that under Must be tested, or post a test request.
A follow-up, not for this PR: texel buffer descriptors (
texel_views, :3880-3899) are still written asVK_NULL_HANDLEwhen nothing resolves, and that is invalid withoutnullDescriptortoo.Verdict: comment. The code is correct for the view types it covers and can't reach Tier 1. Before merge it still needs one real-driver run with
nullDescriptorforced off, and ideally integer/depth dummies. I made no push: the fixes aren't small, and maintainer edits are off. The branch merges cleanly, so it needs no rebase.@ -4021,3 +4021,3 @@if (view && tex && d.is_written && (dump_on() || probe_on())) dump_written().push_back({tex, level});if (dump_on()) dump_input(d.is_written ? 'W' : 'R', (uint32_t)out.imgs.size(), handle, view ? tex : nullptr);if (!view && !dev.null_descriptor) view = VK_NULL_HANDLE;if (!view && !dev.null_descriptor) view = dummies.view[(int)vt];Storage images declare a format:
image_format(d.format)is R8/R16/R32 UINT/SINT, R32G32_UINT or R32G32B32A32_UINT, and some programs use atomics on them. AnR8G8B8A8_UNORMdummy breaks the numeric-type rule (VUID-vkCmdDraw-format-07753), and atomics on it break VUID-vkCmdDraw-None-02691, because RGBA8_UNORM has noSTORAGE_IMAGE_ATOMIC_BIT. Failure case: a compute program doesimageAtomicAddon an r32ui image whose handle doesn't resolve. WithoutnullDescriptorthat atomic now runs on an RGBA8 view, which is undefined, and on some drivers a fault.Fix: when
image_format(d.format) != VK_FORMAT_UNDEFINED, use a dummy of that format. Make them lazily, keyed by (view type, format), under theParLockalready held at :4019. Keep the RGBA8 one forUNDEFINED. R32_UINT can be a view of the existing images if they are created withVK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, since it is the same 32-bit size class.@ -4963,0 +4971,4 @@VkImageCreateInfo ci{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};ci.flags = i == 1 ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0;ci.imageType = type[i];ci.format = VK_FORMAT_R8G8B8A8_UNORM;One
R8G8B8A8_UNORMsingle-sample format also feeds the sampled fallback at :3987, so these textures get a view their shader doesn't expect:d.is_integer(isampler/usampler) gets a UNORM view, against VUID-vkCmdDraw-format-07753. The read is undefined.d.is_depthgets a colour view under the compare sampler fromsampler_for(..., true). RGBA8 has noSAMPLED_IMAGE_DEPTH_COMPARISON_BIT(VUID-vkCmdDraw-None-06479). It is also the exact case the Lichii bd0171f7 block at :3943 calls vendor-dependent and deliberately binds as nothing. WithoutnullDescriptor, "nothing" now becomes this view.d.is_multisamplegets a 1-sample view for an MS image.Suggested fix: add
VK_IMAGE_CREATE_MUTABLE_FORMAT_BITand makeR8G8B8A8_UINTand_SINTviews of the same images. They are the same size class, so this costs no extra memory. For the depth-compare case, add a 1x1D16_UNORM(orD32_SFLOAT) 2D/cube image; checkSAMPLED_IMAGE_DEPTH_COMPARISON_BITfirst. Then pick the view at :3987 fromd.is_integer/d.is_depth. If MS is rare, a 4-sample 2D one can wait for a follow-up.@ -4963,0 +4984,4 @@img[j] = VK_NULL_HANDLE, mem[j] = nullptr;}static int logged;if (logged++ < 4) logf("vk: the dummy images (no nullDescriptor): not made");Nit:
make_dummiesruns only once (insideif (!zero_buffer)), so the< 4cap never triggers. A plainlogfis enough. It also means a failure here is never retried: the views stay null for the whole session. That's fine, but say so in the doc comment.@ -4963,0 +5000,4 @@}VkDependencyInfo d{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};d.imageMemoryBarrierCount = 3, d.pImageMemoryBarriers = b;vkCmdPipelineBarrier2(rec_cmd, &d);Minor. The clear goes into whichever recording calls
draw_begin_recordingfirst. Parallel segment recordings also call it (vk_replay.cpp:2141). If a recording that begins after this one is submitted before it, its draws read the dummy while it is stillUNDEFINED. That is probably fine in practice, because the first recording at start-up is the serial one. Please either note why that holds, or do the clear in its own one-time submission and wait on it before returning. Then the views are ready whichever recording binds them first.