Skip to content

d3d12: compile Vulkan pipelines with a known stack, not the title thread's - #5545

Open
evilclanker9000 wants to merge 2 commits into
ItsNotRexed:mainfrom
evilclanker9000:cc/pipeline-stack
Open

evilclanker9000 wants to merge 2 commits into
ItsNotRexed:mainfrom
evilclanker9000:cc/pipeline-stack

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-PIPELINE-STACK (claim), for PeterGrieffindor's finding on pipelines compiled on game threads (the part RT-400 / #5468 does not cover). (Claude)

What and why

A driver compiles a pipeline's shaders on the thread that calls vkCreate*Pipelines. Its compiler can need several MB of stack: on AMD 26.11.2, more than 1 MB for 26 mesh modules and more than 2 MB for 10 of 28 large compute modules, while 4 MB was enough for all of them (#4979). #5468 gives gta6rt's own threads 8 MB. A thread the title starts with its own dwStackSize keeps that size, though, and the layer creates pipelines on whatever thread it is running on:

  • CreateComputePipelineState calls vkCreateComputePipelines inline (runtime/shims/d3d12_vulkan_device.cpp, create_compute);
  • a graphics pipeline variant is built on first use (variant → build → vkCreateGraphicsPipelines) on the thread that replays the command list.

New runtime/src/compile_stack.h: leonida::with_compile_stack(f) checks how much of the calling thread's stack is left (GetCurrentThreadStackLimits). With at least 4 MB left it runs f in place, as before. Otherwise it runs f on a short-lived thread with an 8 MB reservation (STACK_SIZE_PARAM_IS_A_RESERVATION, the same size #5468 uses) and waits for it. If that thread can't be created, it falls back to compiling in place. Both vkCreate*Pipelines calls go through it. On non-Windows builds it always calls f in place.

On the console nothing is compiled at that point, so a title has no reason to give such a thread a large stack. This is a precaution, not a reproduced crash: which threads the game uses can't be known without its executable.

Tested

NVIDIA GeForce RTX 5080, Windows 11, MSVC 2022 (RelWithDebInfo, Ninja).

  • New CPU test compile_stack_test (WIN32 only): on a 256 KB-reserve thread the callable sees ≥ 4 MB of stack on another thread and its result comes back; on a 16 MB thread it runs in place on the same thread.
    • with this change: compile_stack: ok, exit 0
    • negative control, the helper's check replaced by if (false) so it always runs in place (the behaviour before this PR): line 47: small.left >= leonida::compile_stack_need, exit 1
  • The D3D12 tests that create these pipelines, run on the RTX 5080: d3d12_compute_test, d3d12_draw_test, d3d12_geometry_test, d3d12_msaa_draw_test all pass (exit 0, all pixels and elements matched). Their threads have the 1 MB default stack, so they now go through the new thread.

Notes

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (no translator change)

…ad's

A driver compiles on the thread that calls vkCreate*Pipelines. CreateComputePipelineState and the graphics variant built during replay ran it on whichever game thread called, which may have a small explicit dwStackSize. with_compile_stack runs the call in place when 4 MB of stack are left and otherwise on a short-lived thread with an 8 MB reservation while the caller waits.
@SirKingg

Copy link
Copy Markdown
Contributor

Independent Windows review of head c06af4d8eccd50177abe77db54f2c255a4309913 (AI-assisted; no game files).

Risk: failure to allocate the worker reintroduces the stack-overflow scenario. runtime/src/compile_stack.h:59-67 deliberately falls back to f() on the small caller stack when CreateThread fails. I injected ERROR_NOT_ENOUGH_MEMORY for the worker creation on a 256 KiB-reserve caller: the callable ran on that same caller with only 260,800 bytes remaining. Thus both Vulkan pipeline paths can still enter the driver with insufficient stack under resource pressure; the code does not surface the failure. This does not claim a reproduced driver crash.

Windows 11 build 26300, MSVC 19.44.35230, Ninja RelWithDebInfo; NVIDIA GeForce RTX 4070 Ti, Windows driver 32.0.16.1088. Built this exact head without game data. ctest --test-dir <isolated-build> -C RelWithDebInfo -R '^(compile_stack|d3d12_compute|d3d12_draw|d3d12_geometry|d3d12_msaa_draw)$' --output-on-failure: 5/5 passed. Compute and draw reported the RTX 4070 Ti and matched their synthetic results. compile_stack covers small and large caller stacks; the Vulkan tests do not explicitly create a small-stack caller.

A separate hand-written CPU harness against the unchanged header ran 16 simultaneous 256 KiB callers: each completed on a different worker with >=4 MiB remaining and propagated the negative result -13; the failure injection above reproduced the fallback. On the success path, the caller waits, so the captured pipeline inputs and result remain live; graphics variants retain their mutex and the default Vulkan pipeline cache is driver-synchronized. This did not test real large game shaders or independently exercise Vulkan creation through an explicitly small-stack caller.

@Etr0p

Etr0p commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested c06af4d8 on AMD Radeon 610M (RDNA2 iGPU, AMD 26.11.2 / 32.0.21045.29003, the driver of the finding) and RTX 5070 Ti Laptop (NVIDIA 621.24), Windows 11 26200, MSVC 14.44 RelWithDebInfo (scripts\build_runtime.ps1), Vulkan SDK 1.4.363.0: pass. First GPU run, and a check through the device itself, which compile_stack_test doesn't reach.

Device-level check: 96eb93eb on Etr0p:review/5545-stack-probe, on top of this head. Take it if useful.

  • d3d12_compute and d3d12_draw wrap the vkGetInstanceProcAddr they pass in VulkanDeviceInfo. The device loads vkGetDeviceProcAddr through it (d3d12_vulkan_device.cpp:4225), so the vkCreateComputePipelines / vkCreateGraphicsPipelines it uses record the stack left on the thread that calls the driver (runtime/test/pipeline_stack_probe.h).
  • From a thread with 256 KB reserved, each case must reach the driver with at least 4 MB left:
    • a compute PSO;
    • a graphics PSO (variant 0);
    • ExecuteCommandLists of the 2-target PSO drawn with one target bound, which builds a variant during replay.

Results

  • compile_stack, d3d12_compute and d3d12_draw pass on both GPUs, with LEONIDA_VK_VALIDATION=0 and 1.
  • Negative control: with main's d3d12_vulkan_device.cpp and the same tests, all three checks fail on both GPUs, each on its own: d3d12_compute_test.cpp:359 (compute PSO), d3d12_draw_test.cpp:212 (graphics PSO), and :447 (replayed variant, with the creation check disabled for that run).
  • Full ctest on each GPU: 178 tests, 0 new failures. Only the two failures this machine shows on main remain: d3d12_heap_properties (the default D3D12 adapter is the shared-memory iGPU, d3d12_heap_properties: compare with the layer's adapter; L1 on a UMA host is not comparable #4877) and, on the Radeon only, d3d12_sparse_queue_family_contract (TEST-1089).

On @SirKingg's point: I had written the same fallback independently before seeing this PR. If the thread can't be created, compiling on the caller is what main does today, so it isn't a regression. Failing the PSO instead would turn a possible overflow into a certain E_FAIL, which looks like a maintainer call more than a defect of this PR.

AI-assisted (Claude Code). Synthetic fixtures only, no game files.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at head c06af4d8e (Windows 11, RTX 5070 Ti; RelWithDebInfo + Ninja + Vulkan 1.4.363).

Commands:

cmake ... -DBUILD_TESTING=ON -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON ...
cmake --build . --target compile_stack_test d3d12_compute_test
LEONIDA_VK_DEVICE='RTX 5070 Ti' ./compile_stack_test.exe
LEONIDA_VK_DEVICE='RTX 5070 Ti' ./d3d12_compute_test.exe
git diff --check 3faac557b..HEAD

Results: compile_stack: ok, exit 0; d3d12_compute_test exit 0 (CBV, buffer SRV raw/structured/typed and the three compute dispatches all matched). diff --check clean.

The helper checks GetCurrentThreadStackLimits, runs in place with >= 4 MB left, otherwise spawns an 8 MB-reservation thread and waits (result written before the wait returns); a failed CreateThread falls back in place. Both vkCreateComputePipelines and vkCreateGraphicsPipelines are wrapped. Only those two sites are covered, as stated.

Verdict: approve.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested c06af4d on Intel UHD Graphics 770, driver 32.0.101.7088: compile_stack_test exits 0 with "compile_stack: ok", and d3d12_compute_test, d3d12_draw_test, d3d12_geometry_test and d3d12_msaa_draw_test exit 0 with LEONIDA_VK_VALIDATION=1 ("4096 pixels matched" in the geometry and MSAA runs). Rerun with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer, the loader logs the layer being inserted and the four D3D12 tests show 0 validation errors. With the stack check in runtime/src/compile_stack.h replaced by if (false), compile_stack_test exits 1 with "line 47: small.left >= leonida::compile_stack_need", and it passes again after the header is restored. I did not measure the thread stack size the description mentions.

AI-assisted.

# Conflicts:
#	runtime/CMakeLists.txt
@evilclanker9000

Copy link
Copy Markdown
Contributor Author

Merged current main into the branch (one conflict in runtime/CMakeLists.txt: kept both the new vk_presenter_capture_colour test and compile_stack_test). compile_stack_test builds and passes on the merged head.

On the CreateThread-failure fallback raised in review: it keeps main's current behaviour (compile on the caller), so it is not a regression. Failing the PSO instead is a maintainer decision; happy to change it if wanted. AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants