feat(vk): write back DEPTH24 kept as D32_SFLOAT as 24-bit unorm #1051
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!1051
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:feat/vk-d24-readback"
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: feat · Risk: low · Tested on: Windows, AMD Radeon RX 9070 XT · Needs: none
Why
When a DEPTH24 texture is kept as
D32_SFLOAT(a GPU without 24-bit depth attachments), the depth the GPU draws is never written back to the game's memory, so a game that reads its depth buffer back gets stale data. This makesflushable()take that image, copy its depth aspect and hand the game its own 24-bit unorm words.How to reproduce
Seen on: reading
gpu/vk/vk_texture.cpp:flushable()returns false for any image whose aspect is notCOLOR. On mainvk_format()maps DEPTH24 toX8_D24_UNORM_PACK32on every GPU, so this path acts only where DEPTH24 is kept asD32_SFLOATSettings and switches: defaults
Steps: game-free:
python -I -B tests/test_d24_readback.py --source <main checkout> --headers <Vulkan headers> --cxx clang++(the test from this PR)Expected: a DEPTH24 kept as
D32_SFLOATis flushable, its depth aspect is copied out and each float becomes the game's 24-bit unorm wordActual: on main the test fails with "flushable takes colour images only: a DEPTH24 kept as D32_SFLOAT is never written back"
What changed
A DEPTH24 texture kept as
D32_SFLOATis written back to guest memory like a colour render target, converted to the game's 24-bit unorm layout.flushable()ingpu/vk/vk_texture.cpp: also takes an image withnvn_format == DEPTH24,format == D32_SFLOATand aspectDEPTH(DEPTH24_STENCIL8 stays out).flush_record(): theVkBufferImageCopyaspect isDEPTHfor a depth image,COLORotherwise.flush_finish(): after the mapped range is invalidated, each 32-bit float is clamped to [0, 1] (NaN as 0) and written back as(uint32_t)(v * 16777215.0 + 0.5); the top 8 bits, unused by DEPTH24, are zero.tests/test_d24_readback.py(new).How it works, step by step:
D32_SFLOATis drawn by the GPU.flushable()sawaspect != COLORand returned false; the guest memory kept what the game wrote before, never the depth drawn.flushable()takes it;flush_record()recordsvkCmdCopyImageToBufferwithaspectMask = DEPTH.D32_SFLOATdepth is copied asD32_SFLOAT), so the buffer holds floats.flush_finish()turns each float into the 24-bit unorm word the game expects, the inverse of the upload's(v & 0xffffff) / 16777215.0f. The conversion is done in double, since in float1.0f * 16777215.0f + 0.5frounds to1 << 24.The Vulkan rules for the copy this changes, from
validusage.json:DEPTHonly.D32_SFLOAThas the depth aspect.p.atis 16-byte aligned.Before (
flushable()andflush_record()):After:
Measurements
Verdict: it adds the write-back:
tests/test_d24_readback.pyfails on main and passes with this PR, in both interleaved runs. Colour textures are not converted (the test checks it).Hardware: AMD Radeon RX 9070 XT, Windows; the test itself is game-free (it reads source and runs
flush_finish()'s conversion, compiled out of the source); Python 3.12, clang++ (llvm-mingw)Scene: none, game-free
Method:
tests/test_d24_readback.pyfrom this PR with--sourceset to 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 gaveHow to reproduce:
python -I -B tests/test_d24_readback.py --source <checkout> --headers <Vulkan headers> --cxx clang++for each sidecefce0d1fd659584tests/test_d24_readback.py, run 1tests/test_d24_readback.py, run 2What the test checks:
flushable()takes a DEPTH24 kept asD32_SFLOATwith the depth aspect.flush_record()copies the depth aspect of a depth image.flush_finish()converts afterinvalidate_mapped(), not before.flush_finish(): 0, 1, 0.5, -0.25, 2, NaN, 1/16777215 and 0.75 become 0, 0xFFFFFF, 0x800000, 0, 0xFFFFFF, 0, 1 and 0xBFFFFF, over two parts; the bytes between parts are untouched, and a colour texture is left as it was.Flags and settings
None.
Testing
Tested: on this branch, Windows, Python 3.12, clang++ (llvm-mingw):
test_d24_readbackpasses (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.Not tested: a scene that reads its depth buffer back on the CPU was not compared with main; on main DEPTH24 is never kept as
D32_SFLOAT, so this path does not run there. Linux was built, not run.Must be tested: None.
Risks
What could break: a DEPTH24 kept as
D32_SFLOATis now copied back to guest memory like colour targets are, which costs one copy per flush for those images. A float outside [0, 1] is clamped, as a 24-bit unorm depth cannot hold it.Turning it off: revert the commit.
Compatibility: Nothing changes for colour images, DEPTH24_STENCIL8, or GPUs where DEPTH24 is
X8_D24_UNORM_PACK32(every GPU on main today).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:test_d24_readbackpasses)third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here. (tests/test_d24_readback.py)tests/that reads changed source updated in this PR, not weakened. (tests/test_d24_readback.pyreadsvk_texture.cpp; no other check reads the changed lines)longshot_vk.sowithout and with AMD FSR 4; no OS-specific code or paths)Tested in game: Windows 11 IoT Enterprise LTSC 24H2 (build 26100.9711), AMD Radeon RX 9070 XT, AMD Software: Adrenalin Edition 26.10.1: played with this change; the game runs normally, no rendering errors or crashes seen.
Possibly related, for context:
WIP: feat(vk): write back DEPTH24 kept as D32_SFLOAT as 24-bit unormto feat(vk): write back DEPTH24 kept as D32_SFLOAT as 24-bit unormView command line instructions
Checkout
From your project repository, check out a new branch and test the changes.