Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate WorkThreadPool for shader compiler. #103506

Merged
merged 1 commit into from
Mar 5, 2025

Conversation

bruvzg
Copy link
Member

@bruvzg bruvzg commented Mar 3, 2025

Fixes #102812
Supersede #103299

Creates dedicated thread pool for ShaderRD compilation.

Not a perfect solution, but seems to be working pretty reliably (I was not able to get it crash or deadlock a single time).

@bruvzg bruvzg added this to the 4.5 milestone Mar 3, 2025
@bruvzg bruvzg requested review from a team as code owners March 3, 2025 13:43
@akien-mga akien-mga added the cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release label Mar 3, 2025
@clayjohn clayjohn requested a review from DarioSamo March 3, 2025 15:41
Comment on lines +844 to +854
WorkerThreadPool *WorkerThreadPool::get_named_pool(const StringName &p_name) {
WorkerThreadPool **pool_ptr = named_pools.getptr(p_name);
if (pool_ptr) {
return *pool_ptr;
} else {
WorkerThreadPool *pool = memnew(WorkerThreadPool(false));
pool->init();
named_pools[p_name] = pool;
return pool;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would advocate for a separate API to create a named pool and have this API fail if the pool doesn't exist.

Copy link
Member Author

Choose a reason for hiding this comment

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

If it was a public API and intended for generic use, I would agree. But it's only intended for a single case only, and in general should be avoided.

@DarioSamo
Copy link
Contributor

I'm a fan of the concept of named pools, but does this implementation mean that it'll double up the available thread count from whatever the user configured? I've looked at the PR and didn't see if we have any logic to handle on how to respect that setting.

It's still an acceptable hotfix in my eyes as the workload for shader compilation doesn't come in that frequently.

Copy link
Contributor

@DarioSamo DarioSamo left a comment

Choose a reason for hiding this comment

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

Overall I think this does the job considering this is a hotfix for a critical issue that has presented itself with 4.4, but I think we should consider extending WorkerThreadPool to work with a design where we're just allowed to create pools locally to the context of what we want to use. This is not entirely necessary for this PR given the nature of the issue at the moment.

For example, instead of a name, we'd just make the object inside ShaderRD's global context instead, which would make it a bit more natural and it could allow us to specify a thread count specifically for shader compilation easily.

@bruvzg
Copy link
Member Author

bruvzg commented Mar 4, 2025

I'm a fan of the concept of named pools, but does this implementation mean that it'll double up the available thread count from whatever the user configured?

Yes, it is doubling the number of threads (we do not want to constantly spawn new threads, but simply having 2 threads per core should not cuase any problems).

@clayjohn
Copy link
Member

clayjohn commented Mar 4, 2025

I am a little bit unclear about the tradeoffs here. We are spawning a bunch of extra threads that will ultimately sit idle. Will they consume any resources once we finish compiling shaders? Are their any OS limitations we need to be wary of (especially on Mobile/Web)?

@bruvzg
Copy link
Member Author

bruvzg commented Mar 4, 2025

Web

We do not have WebGPU support (RenderingDevice), so it will have zero effect on web.

Are their any OS limitations we need to be wary of

There is a per-process handle limit, but as far as I know it's in the thousands or tens of thousands depending on platform.

Will they consume any resources once we finish compiling shaders?

512 KiB of memory for stack + metadata per thread. At least on macOS, Xcode profiler show completely flat line for idle threads with no detectable CPU activity at all.

Also, the new pool is only initialized if there are shaders to compile, as long as it's loaded from cache it won't, so it should affect only the first start or editor.

Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

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

Thanks for answering my questions! I think this makes sense as a workaround for now.

I am concerned that this named WTP system will get blindly expanded in the future into some sort of generic feature, but I think for now we should go forward with this PR

@bruvzg
Copy link
Member Author

bruvzg commented Mar 5, 2025

I am concerned that this named WTP system will get blindly expanded in the future into some sort of generic feature, but I think for now we should go forward with this PR

That's the main reason I do not want to create a nice API for it, and implemented it as a single function. It won't prevent it from being used, but at least have more chances for a comment advising not to use it to be noticed.

@Repiteo Repiteo merged commit 5c3e728 into godotengine:master Mar 5, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Mar 5, 2025

Thanks!

@akien-mga
Copy link
Member

Cherry-picked for 4.4.1.

@akien-mga akien-mga removed the cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release label Mar 12, 2025
RoyBerardo pushed a commit to RoyBerardo/godot that referenced this pull request Mar 29, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Use separate WorkThreadPool for shader compiler.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when clicking Play in TPS demo on macOS
6 participants