fix(vk): read RX_VK_SHADOW_BIAS once, safely across recording threads #661
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!661
Loading…
Reference in a new issue
No description provided.
Delete branch "jcabarca/Longshot:fix/vk-shadow-bias-init"
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 characters' shadow bias (
RX_VK_SHADOW_BIAS) is read into two function statics behind a plainif (units < 0)check, and parallel recording threads reach that code at once. That is a data race (undefined behaviour in C++): a draw can take the default units with a slope that is not read yet, or a half-written pair.How to reproduce
Seen on: not seen in the game: found by reading
dynamic_state()ingpu/vk/vk_draw.cpp; a race like this shows only now and then, with no fixed stepsSettings and switches: defaults (more likely with
RX_VK_SHADOW_BIASset, since then the two values differ from their first defaults)Steps: game-free:
python -I -B tests/test_shadow_bias_once.pyon main (copy the test from this PR); it reads the block that parsesRX_VK_SHADOW_BIASinvk_draw.cppExpected: the variable is parsed once, before any thread reads
unitsandslopeActual: on main it is parsed under a plain
if (units < 0)that every recording thread runs; the test failsWhat changed
unitsandslopenow start at their defaults, andRX_VK_SHADOW_BIASis parsed in a function-local static's initializer, which C++ runs exactly once and makes visible to every thread before they go on.RX_VK_CASCADE_BIAS, just below in the same function, is already read this way.dynamic_state()ingpu/vk/vk_draw.cpp:static float units = 250000.0f, slope = 0.0f;and astatic const bool bias_read = [] { ... }();that callsgetenv/sscanfonce; theif (units < 0)block is gone. What the bias does to a draw is unchanged.tests/test_shadow_bias_once.py(new) fails if the block that readsRX_VK_SHADOW_BIASstill usesif (units < 0), or does not read it in a static initializer.The bug, step by step:
dynamic_state()runs for every draw, on every recording thread (parallel recording records several command buffers at once).units < 0and enter the block together.units = 250000.0f, slope = 0.0f;and thensscanfwrites both again from the variable, while the other threads already past the check readunitsandslopefor their draw.unitsfrom the default andslopefrom the variable (or the reverse), so its shadow map gets a different bias than the others.Before:
After:
Start reading at the characters' shadow block in
dynamic_state()(the comment that namesRX_VK_SHADOW_BIAS=<units>,<slope>).Measurements
Verdict: it fixes the bug:
tests/test_shadow_bias_once.pyfails on main (read under a plainunits < 0check) and passes with this PR (read once, in a static initializer), in both interleaved runs. No speed change is expected or measured: after the first call a function-local static costs one already-initialized check, as the oldunits < 0comparison did.Hardware: AMD Radeon RX 9070 XT, Windows; the test itself is game-free (it reads source, it does not use the GPU); Python 3.12
Scene: none, game-free
Method:
tests/test_shadow_bias_once.pyfrom this PR against each side'sgpu/vk/vk_draw.cpp(a checkout of each commit, the PR's test copied into main's), 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_shadow_bias_once.pyin a checkout of each side (copy the test into main's)82886e89716586bctests/test_shadow_bias_once.py, run 1tests/test_shadow_bias_once.py, run 2What the test checks:
getenv("RX_VK_SHADOW_BIAS")invk_draw.cppand the block fromstatic float unitsto it.if (units < 0), and must holdstatic const boolwith a= [] {initializer.Flags and settings
None.
Testing
Tested: on this branch, Windows, Python 3.12:
test_shadow_bias_oncepasses (fails on main), and the other checks that read the changed code pass:test_equal_depth_margin,test_mesh_fallback,test_same_dyn,test_fallback_pipeline_state,test_recorder_sync.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: the race itself: it shows only now and then, with no steps that reach it on purpose. Linux was built, not run.
Must be tested: None: the change is the same on every GPU and OS (a C++ static initializer), and the game-free test covers it.
Risks
What could break: nothing in what the bias does: the defaults (250000 units, slope 0) and the parsing are the same, only when they are written changes.
RX_VK_SHADOW_BIASis still read once per process, as before.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_shadow_bias_once.pynew)longshot_vk.sowithout and with AMD FSR 4; a function-local static initializer is standard C++, thread-safe since C++11; no OS-specific code or paths)WIP: fix(vk): read RX_VK_SHADOW_BIAS once, safely across recording threadsto fix(vk): read RX_VK_SHADOW_BIAS once, safely across recording threadsRead the diff and
dynamic_state()around it. The fix is right:units/slopestart at their defaults, and the function-local static's initializer parsesRX_VK_SHADOW_BIASonce. C++11 guarantees other threads block until it finishes, so no thread reads a half-written pair. The behaviour and the default (250000, 0) are unchanged, and it matches howRX_VK_CASCADE_BIASis read just below. The test reads source text, but it bails with "needs updating" if the block moves, which is fine. I did not run anything. Approving.