Repository navigation
d3d12: compile Vulkan pipelines with a known stack, not the title thread's - #5545
evilclanker9000 wants to merge 2 commits into
Conversation
…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.
|
Independent Windows review of head Risk: failure to allocate the worker reintroduces the stack-overflow scenario. 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. 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 |
|
Tested Device-level check:
Results
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 AI-assisted (Claude Code). Synthetic fixtures only, no game files. |
Kam471
left a comment
There was a problem hiding this comment.
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.
|
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
|
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. |
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 owndwStackSizekeeps that size, though, and the layer creates pipelines on whatever thread it is running on:CreateComputePipelineStatecallsvkCreateComputePipelinesinline (runtime/shims/d3d12_vulkan_device.cpp,create_compute);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 runsfin place, as before. Otherwise it runsfon 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. BothvkCreate*Pipelinescalls go through it. On non-Windows builds it always callsfin 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).
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.compile_stack: ok, exit 0if (false)so it always runs in place (the behaviour before this PR):line 47: small.left >= leonida::compile_stack_need, exit 1d3d12_compute_test,d3d12_draw_test,d3d12_geometry_test,d3d12_msaa_draw_testall 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
vulkan_depth24.h,xdxr_build.h,vulkan_predicate*.h, the present compositor) use small built-in shaders, so I left them as they are.AI-assisted: yes
Checklist
docs/design.md, with how it was confirmed. (no translator change)