fix(vk): read RX_VK_SHADOW_BIAS once, safely across recording threads #661

Merged
HumanBean merged 1 commit from jcabarca/Longshot:fix/vk-shadow-bias-init into main 2026-11-15 19:17:33 +00:00
Contributor

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 plain if (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() in gpu/vk/vk_draw.cpp; a race like this shows only now and then, with no fixed steps
Settings and switches: defaults (more likely with RX_VK_SHADOW_BIAS set, since then the two values differ from their first defaults)
Steps: game-free: python -I -B tests/test_shadow_bias_once.py on main (copy the test from this PR); it reads the block that parses RX_VK_SHADOW_BIAS in vk_draw.cpp
Expected: the variable is parsed once, before any thread reads units and slope
Actual: on main it is parsed under a plain if (units < 0) that every recording thread runs; the test fails

What changed

units and slope now start at their defaults, and RX_VK_SHADOW_BIAS is 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.

  • vk: dynamic_state() in gpu/vk/vk_draw.cpp: static float units = 250000.0f, slope = 0.0f; and a static const bool bias_read = [] { ... }(); that calls getenv/sscanf once; the if (units < 0) block is gone. What the bias does to a draw is unchanged.
  • tests: tests/test_shadow_bias_once.py (new) fails if the block that reads RX_VK_SHADOW_BIAS still uses if (units < 0), or does not read it in a static initializer.

The bug, step by step:

  1. dynamic_state() runs for every draw, on every recording thread (parallel recording records several command buffers at once).
  2. The first threads to arrive all see units < 0 and enter the block together.
  3. Each writes units = 250000.0f, slope = 0.0f; and then sscanf writes both again from the variable, while the other threads already past the check read units and slope for their draw.
  4. A plain read concurrent with a plain write of the same object is a data race, which is undefined behaviour; in practice a draw can use units from the default and slope from the variable (or the reverse), so its shadow map gets a different bias than the others.

Before:

static float units = -1, slope = 0;
if (units < 0) {
	units = 250000.0f, slope = 0.0f;
	if (const char* e = getenv("RX_VK_SHADOW_BIAS")) sscanf(e, "%f,%f", &units, &slope);
}

After:

// (read once, thread-safely: parallel recording threads reach here at once)
static float units = 250000.0f, slope = 0.0f;
static const bool bias_read = [] {
	if (const char* e = getenv("RX_VK_SHADOW_BIAS")) sscanf(e, "%f,%f", &units, &slope);
	return true;
}();
(void)bias_read;

Start reading at the characters' shadow block in dynamic_state() (the comment that names RX_VK_SHADOW_BIAS=<units>,<slope>).

Measurements

Verdict: it fixes the bug: tests/test_shadow_bias_once.py fails on main (read under a plain units < 0 check) 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 old units < 0 comparison 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.py from this PR against each side's gpu/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 gave
How to reproduce: python -I -B tests/test_shadow_bias_once.py in a checkout of each side (copy the test into main's)

Before: main @ 82886e89 After: this PR @ 716586bc
tests/test_shadow_bias_once.py, run 1 fails passes
tests/test_shadow_bias_once.py, run 2 fails passes
Median fails passes
run 1, main @ 82886e89:
RX_VK_SHADOW_BIAS is read under a plain `units < 0` check: threads race on units and slope
exit 1
run 1, this PR @ 716586bc:
RX_VK_SHADOW_BIAS read once, in a static initializer
exit 0
run 2, main @ 82886e89:
RX_VK_SHADOW_BIAS is read under a plain `units < 0` check: threads race on units and slope
exit 1
run 2, this PR @ 716586bc:
RX_VK_SHADOW_BIAS read once, in a static initializer
exit 0

What the test checks:

  • It finds getenv("RX_VK_SHADOW_BIAS") in vk_draw.cpp and the block from static float units to it.
  • That block must not hold if (units < 0), and must hold static const bool with a = [] { initializer.
  • It stops with "this test needs updating" if the variable or the statics move, so it cannot pass by the code moving elsewhere.

Flags and settings

None.

Testing

Tested: on this branch, Windows, Python 3.12: test_shadow_bias_once passes (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_BIAS is still read once per process, as before.

Turning it off: revert the commit.

Compatibility: Nothing changes.

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched). (no issue fits)
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named). (the checks that read the changed code, on this PR, Windows: all pass)
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here.
  • A check under tests/ that reads changed source updated in this PR, not weakened. (tests/test_shadow_bias_once.py new)
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. (built on Windows with llvm-mingw and on Linux, longshot_vk.so without and with AMD FSR 4; a function-local static initializer is standard C++, thread-safe since C++11; no OS-specific code or paths)
**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 plain `if (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()` in `gpu/vk/vk_draw.cpp`; a race like this shows only now and then, with no fixed steps **Settings and switches:** defaults (more likely with `RX_VK_SHADOW_BIAS` set, since then the two values differ from their first defaults) **Steps:** game-free: `python -I -B tests/test_shadow_bias_once.py` on main (copy the test from this PR); it reads the block that parses `RX_VK_SHADOW_BIAS` in `vk_draw.cpp` **Expected:** the variable is parsed once, before any thread reads `units` and `slope` **Actual:** on main it is parsed under a plain `if (units < 0)` that every recording thread runs; the test fails ## What changed `units` and `slope` now start at their defaults, and `RX_VK_SHADOW_BIAS` is 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. - vk: `dynamic_state()` in `gpu/vk/vk_draw.cpp`: `static float units = 250000.0f, slope = 0.0f;` and a `static const bool bias_read = [] { ... }();` that calls `getenv`/`sscanf` once; the `if (units < 0)` block is gone. What the bias does to a draw is unchanged. - tests: `tests/test_shadow_bias_once.py` (new) fails if the block that reads `RX_VK_SHADOW_BIAS` still uses `if (units < 0)`, or does not read it in a static initializer. The bug, step by step: 1. `dynamic_state()` runs for every draw, on every recording thread (parallel recording records several command buffers at once). 2. The first threads to arrive all see `units < 0` and enter the block together. 3. Each writes `units = 250000.0f, slope = 0.0f;` and then `sscanf` writes both again from the variable, while the other threads already past the check read `units` and `slope` for their draw. 4. A plain read concurrent with a plain write of the same object is a data race, which is undefined behaviour; in practice a draw can use `units` from the default and `slope` from the variable (or the reverse), so its shadow map gets a different bias than the others. Before: ```cpp static float units = -1, slope = 0; if (units < 0) { units = 250000.0f, slope = 0.0f; if (const char* e = getenv("RX_VK_SHADOW_BIAS")) sscanf(e, "%f,%f", &units, &slope); } ``` After: ```cpp // (read once, thread-safely: parallel recording threads reach here at once) static float units = 250000.0f, slope = 0.0f; static const bool bias_read = [] { if (const char* e = getenv("RX_VK_SHADOW_BIAS")) sscanf(e, "%f,%f", &units, &slope); return true; }(); (void)bias_read; ``` Start reading at the characters' shadow block in `dynamic_state()` (the comment that names `RX_VK_SHADOW_BIAS=<units>,<slope>`). ## Measurements **Verdict:** it fixes the bug: `tests/test_shadow_bias_once.py` fails on main (read under a plain `units < 0` check) 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 old `units < 0` comparison 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.py` from this PR against each side's `gpu/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 gave How to reproduce: `python -I -B tests/test_shadow_bias_once.py` in a checkout of each side (copy the test into main's) | | Before: main @ 82886e89 | After: this PR @ 716586bc | |---|---|---| | `tests/test_shadow_bias_once.py`, run 1 | fails | passes | | `tests/test_shadow_bias_once.py`, run 2 | fails | passes | | Median | fails | passes | ``` run 1, main @ 82886e89: RX_VK_SHADOW_BIAS is read under a plain `units < 0` check: threads race on units and slope exit 1 run 1, this PR @ 716586bc: RX_VK_SHADOW_BIAS read once, in a static initializer exit 0 run 2, main @ 82886e89: RX_VK_SHADOW_BIAS is read under a plain `units < 0` check: threads race on units and slope exit 1 run 2, this PR @ 716586bc: RX_VK_SHADOW_BIAS read once, in a static initializer exit 0 ``` What the test checks: - It finds `getenv("RX_VK_SHADOW_BIAS")` in `vk_draw.cpp` and the block from `static float units` to it. - That block must not hold `if (units < 0)`, and must hold `static const bool` with a `= [] {` initializer. - It stops with "this test needs updating" if the variable or the statics move, so it cannot pass by the code moving elsewhere. ### Flags and settings None. ## Testing **Tested:** on this branch, Windows, Python 3.12: `test_shadow_bias_once` passes (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_BIAS` is still read once per process, as before. **Turning it off:** revert the commit. **Compatibility:** Nothing changes. ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). (no issue fits) - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). (the checks that read the changed code, on this PR, Windows: all pass) - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here. - [x] A check under `tests/` that reads changed source updated in this PR, not weakened. (`tests/test_shadow_bias_once.py` new) - [x] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. (built on Windows with llvm-mingw and on Linux, `longshot_vk.so` without and with AMD FSR 4; a function-local static initializer is standard C++, thread-safe since C++11; no OS-specific code or paths)
fix(vk): read RX_VK_SHADOW_BIAS once, safely across recording threads
Some checks are pending
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 4s
Website / Website on Cloudflare (pull_request_target) Has been skipped
716586bc0a
dynamic_state() kept the characters' shadow bias in two function statics set by hand under 'if (units < 0)'. Parallel recording threads reach it at once, so one could read units and slope while another wrote them: a data race, and a draw could take the default units with a slope not read yet. Both now start at their defaults and the variable is parsed in a static initializer, which runs once and is visible to every thread, as RX_VK_CASCADE_BIAS just below already does.

tests/test_shadow_bias_once.py fails on main and passes with the fix.

Assisted-by: GitHub Copilot
jcabarca changed title from WIP: fix(vk): read RX_VK_SHADOW_BIAS once, safely across recording threads to fix(vk): read RX_VK_SHADOW_BIAS once, safely across recording threads 2026-11-15 03:02:14 +00:00
HumanBean approved these changes 2026-11-15 19:03:41 +00:00
HumanBean left a comment

Read the diff and dynamic_state() around it. The fix is right: units/slope start at their defaults, and the function-local static's initializer parses RX_VK_SHADOW_BIAS once. 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 how RX_VK_CASCADE_BIAS is 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.

Read the diff and `dynamic_state()` around it. The fix is right: `units`/`slope` start at their defaults, and the function-local static's initializer parses `RX_VK_SHADOW_BIAS` once. 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 how `RX_VK_CASCADE_BIAS` is 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.
HumanBean deleted branch fix/vk-shadow-bias-init 2026-11-15 19:17:33 +00:00
Some checks are pending
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 4s
Website / Website on Cloudflare (pull_request_target) Has been skipped
Some workflows are waiting to be reviewed.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!661
No description provided.